<template>
  <div v-if="hasVisibleSections" class="loyalty-suite-navi" :class="{ 'loyalty-suite-swiper-ready': isSwiperReady }">
    <swiper slidesPerView="auto" :spaceBetween="10" :modules="modules" :navigation="navigationOptions"
      @init="onSwiperInit" class="loyalty-suite-navi__slider">
      <swiper-slide class="loyalty-suite-navi__block">
        <router-link class="loyalty-suite-navi__item" :to="'/'" exact>
          <span class="loyalty-suite-navi__text">{{ $t('profileMenu') }}</span>
        </router-link>
      </swiper-slide>
      <swiper-slide v-if="data.sections.ranks" class="loyalty-suite-navi__block">
        <router-link class="loyalty-suite-navi__item" :to="'/ranks'" exact>
          <span class="loyalty-suite-navi__text">{{ $t('ranksMenu') }}</span>
        </router-link>
      </swiper-slide>
      <swiper-slide v-if="data.sections.achievements" class="loyalty-suite-navi__block">
        <router-link class="loyalty-suite-navi__item" :to="'/achievements'" exact>
          <span class="loyalty-suite-navi__text">{{ $t('achievementsMenu') }}</span>
        </router-link>
      </swiper-slide>
      <swiper-slide v-if="data.sections.challenges" class="loyalty-suite-navi__block">
        <router-link class="loyalty-suite-navi__item" :to="'/challenges'" exact>
          <span class="loyalty-suite-navi__text">{{ $t('challengesMenu') }}</span>
        </router-link>
      </swiper-slide>
    </swiper>
    <div class="loyalty-suite-navi__prev">
      <div class="loyalty-suite-navi__prev-arrow">
        <span class="loyalty-suite-material-icons">chevron_left</span>
      </div>
    </div>
    <div class="loyalty-suite-navi__next">
      <div class="loyalty-suite-navi__next-arrow">
        <span class="loyalty-suite-material-icons">chevron_right</span>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { ref, defineComponent, PropType, computed } from 'vue';
import { Swiper, SwiperSlide } from 'swiper/vue';
import 'swiper/css';
import 'swiper/css/navigation';
import { Navigation } from 'swiper/modules';
import type { Swiper as SwiperInstance } from 'swiper/types';

interface NavigationData {
  sections: {
    ranks: boolean;
    achievements: boolean;
    challenges: boolean;
  };
}

export default defineComponent({
  props: {
    data: {
      type: Object as PropType<NavigationData>,
      required: true,
    }
  },
  components: {
    Swiper,
    SwiperSlide,
  },
  setup(props) {
    const navigationOptions = {
      nextEl: '.loyalty-suite-navi__next',
      prevEl: '.loyalty-suite-navi__prev',
    };

    const hasVisibleSections = computed(() => {
      const s = props.data.sections;
      return s.ranks || s.achievements || s.challenges;
    });

    const isSwiperReady = ref(false);

    const onSwiperInit = (swiper: SwiperInstance) => {
      isSwiperReady.value = true;
    };

    return {
      navigationOptions,
      modules: [Navigation],
      hasVisibleSections,
      isSwiperReady,
      onSwiperInit
    };
  }
});
</script>

<style lang="scss" scoped>
.loyalty-suite-navi {
  display: flex;
  width: 100%;
  position: relative;
  margin-bottom: 20px;

  a.loyalty-suite-navi__item {
    display: flex;
    align-items: center;
    height: 40px;
    margin: 0;
    padding: 12px 20px;
    border-radius: 5px;
    font-size: 14px;
    font-weight: 400;
    line-height: 1;
    background: var(--loyalty-suite-light);
    border: 1px solid var(--loyalty-suite-borders-60);
    color: var(--loyalty-suite-dark);
    transition: 0.3s;
    text-decoration: none !important;

    &.router-link-exact-active {
      color: var(--loyalty-suite-light);
      background: var(--loyalty-suite-primary);
      border-color: var(--loyalty-suite-primary);

      &:hover {
        cursor: default;
      }
    }

    &:last-child {
      margin-right: 0;
    }

    &:hover {
      color: var(--loyalty-suite-light);
      background: var(--loyalty-suite-primary);
      border-color: var(--loyalty-suite-primary);
    }
  }

  &__slider {
    width: 100%;
    margin: 0;

    .swiper-slide:last-child {
      margin-right: 0;
    }
  }

  &__block {
    display: flex;
    width: auto;
  }

  &__prev {
    display: none;
    flex-shrink: 0;
    width: 30px;
    height: 30px;
    position: absolute;
    z-index: 2;
    top: 50%;
    left: 10px;
    transform: translateY(-50%);
    align-items: center;
    justify-content: center;

    &-arrow {
      display: flex;
      flex-shrink: 0;
      width: 30px;
      height: 30px;
      border-radius: 50%;
      justify-content: center;
      align-items: center;
      transition: 0.3s;
      background: var(--loyalty-suite-dark);

      &:hover {
        cursor: pointer;
      }
    }

    .loyalty-suite-material-icons {
      font-size: 26px;
      color: var(--loyalty-suite-light);
      margin-right: 2px;
    }

    &.swiper-button-disabled {
      display: none !important;
    }
  }

  &__next {
    display: none;
    flex-shrink: 0;
    width: 30px;
    height: 30px;
    position: absolute;
    z-index: 2;
    top: 50%;
    right: 10px;
    transform: translateY(-50%);
    align-items: center;
    justify-content: center;

    &-arrow {
      display: flex;
      flex-shrink: 0;
      width: 30px;
      height: 30px;
      border-radius: 50%;
      justify-content: center;
      align-items: center;
      transition: 0.3s;
      background: var(--loyalty-suite-dark);

      &:hover {
        cursor: pointer;
      }
    }

    .loyalty-suite-material-icons {
      font-size: 26px;
      color: var(--loyalty-suite-light);
    }

    &.swiper-button-disabled {
      display: none !important;
    }
  }
}

.loyalty-suite-swiper-ready .loyalty-suite-navi__prev,
.loyalty-suite-swiper-ready .loyalty-suite-navi__next {
  display: flex;
}
</style>
