<template>
  <div class="loyalty-suite-rewards-block" :class="{
    [`loyalty-suite-rewards-block_${style}`]: true
  }">
    <div class="loyalty-suite-rewards-block__header">
      <div class="loyalty-suite-rewards-block__title">
        {{ $t('myRewards') }}
      </div>
      <router-link v-if="rewards.length" to="/my-rewards" class="loyalty-suite-rewards-block__link">
        {{ $t('viewAll') }}
      </router-link>
    </div>
    <div class="loyalty-suite-rewards-block__wrapper" :class="{ 'loyalty-suite-swiper-ready': isSwiperReady }">
      <swiper v-if="rewards.length" :slidesPerView="1" :spaceBetween="20" :modules="modules" @init="onSwiperInit"
        :navigation="navigationOptions" class="loyalty-suite-rewards-block__slider">
        <swiper-slide class="loyalty-suite-rewards-block__slide" v-for="(reward, index) in rewards" :key="index">
          <Reward :reward="reward" :inSlider="true" :style="style" />
        </swiper-slide>
      </swiper>
      <div v-else class="loyalty-suite-rewards-block__empty">
        <span class="loyalty-suite-material-icons">card_membership</span>
        {{ $t('noMyRewards') }}
      </div>

      <div v-if="rewards.length" class="loyalty-suite-rewards-block__prev">
        <div class="loyalty-suite-rewards-block__prev-arrow">
          <span class="loyalty-suite-material-icons">chevron_left</span>
        </div>
      </div>
      <div v-if="rewards.length" class="loyalty-suite-rewards-block__next">
        <div class="loyalty-suite-rewards-block__next-arrow">
          <span class="loyalty-suite-material-icons">chevron_right</span>
        </div>
      </div>
      <RewardDetails v-model="statusDetails" :reward="currentReward" @close="closeDetails" />
    </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 RewardDetails from './RewardDetails.vue';
import type { Swiper as SwiperInstance } from 'swiper/types';
import Reward from '../components/Reward.vue';

export default defineComponent({
  props: {
    rewards: {
      type: Array,
      required: true,
    },
    style: {
      type: String,
      default: 'classic',
    },
  },
  components: {
    Swiper,
    SwiperSlide,
    RewardDetails,
    Reward
  },
  setup(props) {
    const navigationOptions = {
      nextEl: '.loyalty-suite-rewards-block__next',
      prevEl: '.loyalty-suite-rewards-block__prev',
    };

    const statusDetails = ref(false);
    const currentReward = ref('');

    const closeDetails = () => {
      statusDetails.value = false;
      currentReward.value = '';
    }

    const openDetails = (index) => {
      const reward = props.rewards?.[index];
      if (!reward) return;

      currentReward.value = reward;
      statusDetails.value = true;
    }

    const shouldShowButton = (index: number): boolean => {
      const reward = props.rewards?.[index];
      if (!reward || typeof reward !== 'object') return false;

      const details = reward.details ?? {};

      return Object.entries(details).some(([key, val]) => {
        if (val == null) return false;

        if (key === 'coupon' && typeof val === 'object' && val !== null) {
          const excludedKeys = ['amount', 'id', 'main_currency', 'type', 'type_label'];
          return Object.entries(val).some(([subKey, subVal]) => {
            if (excludedKeys.includes(subKey)) return false;
            if (subVal === null || subVal === '' || subVal === false || subVal === 0) return false;
            if (Array.isArray(subVal)) return subVal.length > 0;
            if (typeof subVal === 'object') return Object.keys(subVal).length > 0;
            return true;
          });
        }

        if (Array.isArray(val)) return val.length > 0;
        if (typeof val === 'object') return Object.keys(val).length > 0;
        return true;
      });
    };

    const isSwiperReady = ref(false);

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

    return {
      navigationOptions,
      modules: [Navigation],
      shouldShowButton,
      openDetails,
      closeDetails,
      currentReward,
      statusDetails,
      isSwiperReady,
      onSwiperInit
    };
  }
});
</script>

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

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

  &__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    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-rewards-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;

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

  &__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-rewards-block__prev,
.loyalty-suite-swiper-ready .loyalty-suite-rewards-block__next {
  display: flex;
}
</style>
