<template>
  <div class="loyalty-suite-ranks-block" :class="{
    [`loyalty-suite-ranks-block_${style}`]: true
  }">
    <div class="loyalty-suite-ranks-block__header">
      <div class="loyalty-suite-ranks-block__title">
        {{ ranks.length > 1 ? $t('myRanks') : $t('myRank') }}
      </div>
      <router-link v-if="ranks.length" to="/ranks" class="loyalty-suite-ranks-block__link">
        {{ $t('viewAllRanks') }}
      </router-link>
    </div>
    <div class="loyalty-suite-ranks-block__wrapper" :class="{ 'loyalty-suite-swiper-ready': isSwiperReady }">
      <swiper v-if="ranks.length" :slidesPerView="1" :spaceBetween="20" :modules="modules" @init="onSwiperInit"
        :navigation="navigationOptions" class="loyalty-suite-ranks-block__slider">
        <swiper-slide class="loyalty-suite-ranks-block__slide" v-for="(rank, index) in ranks" :key="index">
          <Rank
            :rank="rank"
            :style="style"
            :inSlider="true"
            :myRanks="true"
            @share="onShare"
          />
        </swiper-slide>
      </swiper>
      <div v-else class="loyalty-suite-ranks-block__empty">
        <span class="loyalty-suite-material-icons">military_tech</span>
        {{ $t('noMyRanks') }}
      </div>
      <div v-if="ranks.length" class="loyalty-suite-ranks-block__prev">
        <div class="loyalty-suite-ranks-block__prev-arrow">
          <span class="loyalty-suite-material-icons">chevron_left</span>
        </div>
      </div>
      <div v-if="ranks.length" class="loyalty-suite-ranks-block__next">
        <div class="loyalty-suite-ranks-block__next-arrow">
          <span class="loyalty-suite-material-icons">chevron_right</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } 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';
import Rank from './Rank.vue';

export default defineComponent({
  components: {
    Swiper,
    SwiperSlide,
    Rank
  },
  props: {
    ranks: {
      type: Array,
      required: true,
    },
    style: {
      type: String,
      default: 'classic',
    },
  },
  emits: ['share'],
  setup(props, { emit }) {
    const navigationOptions = {
      nextEl: '.loyalty-suite-ranks-block__next',
      prevEl: '.loyalty-suite-ranks-block__prev',
    };

    const isSwiperReady = ref(false);

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

    const onShare = (link: string) => {
      emit('share', link);
    };

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

<style lang="scss" scoped>
.loyalty-suite-ranks-block {
  display: flex;
  flex-direction: column;
  width: 100%;

  &__wrapper {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;
    justify-content: center;
    position: relative;
  }

  &__header {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
    padding: 10px 10px 0 10px;
  }

  &__title {
    display: flex;
    font-size: 14px;
    font-weight: 700;
    margin-right: 5px;
    margin-bottom: 10px;
  }

  &__empty {
    display: flex;
    width: 200px;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    align-self: center;
    padding: 5px 10px;
    span {
      font-size: 72px;
      margin-bottom: 10px;
      color: var(--loyalty-suite-primary);
    }
  }

  a.loyalty-suite-ranks-block__link {
    text-decoration: none;
    transition: 0.3s;
    color: var(--loyalty-suite-dark);
    padding: 5px 10px;
    border: 1px solid var(--loyalty-suite-borders-60);
    background: var(--loyalty-suite-light);
    border-radius: 5px;
    margin-bottom: 10px;

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

  &__slider {
    width: 100%;
    height: 100%;
    padding: 10px;
    margin: 0;
  }

  &__slide {
    width: 100%;
    height: 100%;
  }

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

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

      &:hover {
        cursor: pointer;
        background: var(--loyalty-suite-primary-90);
      }
    }

    .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: 35px;
    height: 35px;
    position: absolute;
    z-index: 2;
    top: 50%;
    right: 0;
    transform: translateY(-50%);
    align-items: center;
    justify-content: center;

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

      &:hover {
        cursor: pointer;
        background: var(--loyalty-suite-primary-90);
      }
    }

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

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

.loyalty-suite-swiper-ready .loyalty-suite-ranks-block__prev,
.loyalty-suite-swiper-ready .loyalty-suite-ranks-block__next {
  display: flex;
}

@media (max-width: 576px) {
  .loyalty-suite-ranks-block {
    &__slider {
      width: 100%;
    }

    &__slide {
      width: 100%;
    }
    :deep(.loyalty-suite-rank__card) {
      &.loyalty-suite-rank__card_classic {
        .loyalty-suite-rank__card-header {
          height: 180px;
          &.loyalty-suite-rank__card-header_icon {
            height: 95px;
          }
        }
      }
      &.loyalty-suite-rank__card_modern {
        .loyalty-suite-rank__card-header {
          height: 180px;
          &.loyalty-suite-rank__card-header_icon {
            height: 95px;
          }
        }
      }
    }
  }
}
</style>
