<template>
  <div class="loyalty-suite-rank__card" :class="{
    [`loyalty-suite-rank__card_${style}`]: true,
    'loyalty-suite-rank__card_slide': inSlider,
    'loyalty-suite-rank__card_no-rewards': !rank.rewards || !rank.rewards.length,
    'loyalty-suite-rank__card_my': myRanks,
    'loyalty-suite-rank__card_no-click': isSharePage
  }">
    <router-link :to="`/rank/${rank.id}`" class="loyalty-suite-rank__card-content">
      <div class="loyalty-suite-rank__card-wrapper">
        <span v-if="rank.id === rankSet.current_rank" class="loyalty-suite-rank__card-current">
          {{ $t('current') }}
        </span>
        <div v-if="(rank.image_type === 'icon' && rank.icon) || (rank.image_type === 'image' && rank.image)"
          class="loyalty-suite-rank__card-header"
          :class="{ 'loyalty-suite-rank__card-header_icon': rank.image_type === 'icon' }">
          <img v-if="rank.image_type === 'icon' && rank.icon" :src="rank.icon" class="loyalty-suite-rank__card-icon">
          <img v-if="rank.image_type === 'image' && rank.image" :src="rank.image"
            class="loyalty-suite-rank__card-image">
        </div>

        <div class="loyalty-suite-rank__card-name">
          <div v-if="myRanks && rank.set_show" class="loyalty-suite-rank__card-set">{{ rank.set_name }}:</div>
          {{ rank.name }}
        </div>

        <div v-if="rank.progress && rank.progress > 0" class="loyalty-suite-rank__card-progress">
          <div class="loyalty-suite-rank__card-progress-wrapper">
            <div class="loyalty-suite-rank__card-progress-bar" :style="{ width: rank.progress + '%' }"></div>
          </div>
          <div class="loyalty-suite-rank__card-progress-value">
            {{ rank.progress }}% {{ $t('complete') }}
          </div>
        </div>
        <div v-if="rank.rewards && rank.rewards.length" class="loyalty-suite-rank__card-reward">
          <span class="loyalty-suite-rank__card-reward-title">
            {{ $t('rewards') }}:
          </span>
          <div class="loyalty-suite-rank__card-reward-list">
            <div :class="{ 'loyalty-suite-rank__card-reward-list-wrapper_fix': rank.rewards.length > 3 }"
              class="loyalty-suite-rank__card-reward-list-wrapper" ref="rewardList">
              <div v-for="(reward, rewardIndex) in rank.rewards" :key="rewardIndex"
                class="loyalty-suite-rank__card-reward-item">
                <span v-if="rank.progress === 100" class="loyalty-suite-rank__card-reward-item-status"></span>
                <span class="loyalty-suite-rank__card-reward-item-name">
                  {{ formatRewardValue(reward) }} {{ $t(reward.label) }}
                </span>
                <span v-if="shouldShowRewardDetails(reward)" class="loyalty-suite-rank__card-reward-item-details"
                  @click.prevent="openDetails(rewardIndex)">
                  <span class="loyalty-suite-material-icons">notes</span>
                </span>
              </div>
            </div>
            <div v-if="showScrollButtons && canScrollUp" @click.prevent="scrollRewards(-1)"
              class="loyalty-suite-rank__card-reward-scroll-up">
              <span class="loyalty-suite-material-icons">chevron_right</span>
            </div>
            <div v-if="showScrollButtons && canScrollDown" @click.prevent="scrollRewards(1)"
              class="loyalty-suite-rank__card-reward-scroll-down">
              <span class="loyalty-suite-material-icons">chevron_right</span>
            </div>
          </div>
        </div>

        <div v-if="myRanks" @click.stop.prevent="onShareClick" class="loyalty-suite-rank__card-share">
          {{ $t('share') }}
          <span class="loyalty-suite-material-icons">share</span>
        </div>
      </div>
    </router-link>
  </div>
  <RewardDetails v-model="statusDetails" :reward="currentReward" @close="closeDetails" />
</template>

<script lang="ts">
import { defineComponent, ref, computed, onMounted, watch, nextTick } from 'vue';
import RewardDetails from './RewardDetails.vue';
import { formatRewardValue, shouldShowRewardDetails } from '../utils/formatters';

export default defineComponent({
  components: {
    RewardDetails
  },
  props: {
    rankSet: {
      type: Array,
      default: []
    },
    rank: {
      type: Object,
      required: true,
    },
    inSlider: {
      type: Boolean,
      default: false,
    },
    style: {
      type: String,
      default: 'classic'
    },
    myRanks: {
      type: Boolean,
      default: false,
    }
  },
  emits: ['share'],
  setup(props, { emit }) {
    const statusDetails = ref(false);
    const currentReward = ref('');

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

    const openDetails = (rewardIndex: number) => {
      if (!Array.isArray(props.rank.rewards)) return;

      const reward = props.rank.rewards[rewardIndex];
      if (!reward) return;

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

    const rewardList = ref<HTMLElement | null>(null);
    const showScrollButtons = computed(() => props.rank.rewards.length > 3);
    const scrollTop = ref(0);
    const scrollHeight = ref(0);
    const clientHeight = ref(0);

    const updateScroll = () => {
      if (!rewardList.value) return;
      scrollTop.value = rewardList.value.scrollTop;
      scrollHeight.value = rewardList.value.scrollHeight;
      clientHeight.value = rewardList.value.clientHeight;
    };

    const canScrollUp = computed(() => scrollTop.value > 0);

    const canScrollDown = computed(() => {
      return Math.ceil(scrollTop.value + clientHeight.value) < scrollHeight.value;
    });

    const scrollRewards = (direction: number) => {
      const el = rewardList.value;
      if (!el) return;
      const itemHeight = el.scrollHeight / props.rank.rewards.length;
      el.scrollTop += direction * itemHeight;
      updateScroll();
    };

    const isSharePage = !!window.loyalty_suite_account_data.isSharePage;

    const onShareClick = () => {
      const shareBase = window.loyalty_suite_account_data.shareBase;
      const userId = window.loyalty_suite_account_data.userId;
      const id = props.rank?.id;

      if (!userId || !id) {
        return;
      }

      const url = new URL(shareBase);

      url.searchParams.set('user', String(userId));
      url.searchParams.set('type', 'rank');
      url.searchParams.set('id', String(id));

      emit('share', url.toString());
    };

    onMounted(() => {
      if (rewardList.value) {
        rewardList.value.addEventListener('scroll', updateScroll);
        updateScroll();
      }
    });

    watch(() => props.rank.rewards, () => {
      nextTick(updateScroll);
    });

    return {
      statusDetails,
      openDetails,
      closeDetails,
      currentReward,
      rewardList,
      showScrollButtons,
      scrollRewards,
      canScrollUp,
      canScrollDown,
      formatRewardValue,
      shouldShowRewardDetails,
      onShareClick,
      isSharePage
    }
  }
});
</script>

<style lang="scss" scoped>
.loyalty-suite-rank__card {
  display: flex;
  padding: 10px;

  &.loyalty-suite-rank__card_slide {
    width: 100%;
    height: 100%;
    padding: 0;
  }

  &.loyalty-suite-rank__card_classic {

    &.loyalty-suite-rank__card_my {
      .loyalty-suite-rank__card {
        &-wrapper {
          justify-content: center;
        }

        &-share {
          display: flex;
          border: 1px solid var(--loyalty-suite-borders-60);
          border-radius: 4px;
          justify-content: center;
          align-items: center;
          order: 3;
          margin-bottom: 30px;
          padding: 5px 10px;
          font-size: 14px;
          color: var(--loyalty-suite-dark);
          transition: 0.3s;
          cursor: pointer;
          span {
            color: var(--loyalty-suite-primary);
            font-size: 16px;
            margin-left: 6px;
            transition: 0.3s;
          }
          &:hover {
            background: var(--loyalty-suite-primary);
            color: var(--loyalty-suite-light);
            span {
              color: var(--loyalty-suite-light);
            }
          }
        }

        &-name {
          padding: 20px 30px;
        }
      }
    }

    &.loyalty-suite-rank__card_no-click {
      pointer-events: none;
    }

    a.loyalty-suite-rank__card-content {
      display: flex;
      width: 100%;
      flex-direction: column;
      align-items: center;
      text-decoration: none;
      border-radius: 8px;

      &:hover {
        color: var(--loyalty-suite-dark);
      }
    }

    .loyalty-suite-rank__card {
      &-wrapper {
        display: flex;
        width: 100%;
        height: 100%;
        flex-direction: column;
        align-items: center;
        background: var(--loyalty-suite-light);
        box-shadow: 0 3px 10px 0 rgba(0, 0, 0, 0.2);
        border-radius: 8px;
        padding: 0;
      }

      &-current {
        display: flex;
        justify-content: center;
        align-items: center;
        padding: 5px 8px 6px 8px;
        color: var(--loyalty-suite-light);
        font-size: 12px;
        font-weight: 700;
        line-height: 1;
        border-radius: 4px;
        background: var(--loyalty-suite-positive);
        position: absolute;
        top: 7px;
        left: 7px;
        z-index: 2;
      }

      &-progress {
        display: flex;
        flex-direction: column;
        width: 70%;
        padding: 5px 0 20px 0;

        &-value {
          display: flex;
          font-size: 12px;
          color: var(--loyalty-suite-dark);
          justify-content: center;
          text-transform: lowercase;
        }

        &-wrapper {
          display: flex;
          width: 100%;
          height: 5px;
          border-radius: 8px;
          background: var(--loyalty-suite-borders-40);
          position: relative;
          margin: 0 0 5px 0;
        }

        &-bar {
          display: flex;
          position: absolute;
          left: 0;
          top: 0;
          height: 5px;
          border-radius: 8px;
          background: var(--loyalty-suite-positive);
        }
      }

      &-name {
        display: flex;
        width: 100%;
        justify-content: center;
        font-size: 16px;
        font-weight: 700;
        color: var(--loyalty-suite-primary);
        line-height: normal;
        text-align: center;
        padding: 20px;
      }

      &-set {
        display: inline-flex;
        text-transform: capitalize;
        font-size: 16px;
        font-weight: 400;
        color: var(--loyalty-suite-dark);
        line-height: normal;
        margin-right: 5px;
      }

      &-header {
        display: flex;
        width: 100%;
        justify-content: center;
        align-items: center;
        border-radius: 8px;
        border-bottom-right-radius: 0;
        border-bottom-left-radius: 0;

        &.loyalty-suite-rank__card-header_icon {
          height: 95px;
        }
      }

      &-icon {
        display: flex;
        flex-shrink: 0;
        width: 70px;
        height: 70px;
        object-fit: cover;
        margin-top: 25px;
      }

      &-image {
        display: flex;
        flex-shrink: 0;
        width: 100%;
        aspect-ratio: 16 / 9;
        object-fit: cover;
        border-radius: 8px;
        border-bottom-right-radius: 0;
        border-bottom-left-radius: 0;
      }

      &-reward {
        display: flex;
        width: 100%;
        flex-direction: column;
        padding: 0 20px 20px 20px;

        &-title {
          display: flex;
          width: 100%;
          justify-content: center;
          font-size: 14px;
          color: var(--loyalty-suite-dark);
          line-height: normal;
          margin-bottom: 10px;
        }

        &-list {
          display: flex;
          width: 100%;
          flex-direction: column;
          position: relative;

          &-wrapper {
            display: flex;
            width: 100%;
            flex-direction: column;

            &.loyalty-suite-rank__card-reward-list-wrapper_fix {
              height: 123px;
              overflow-y: auto;
              scroll-behavior: smooth;
              scrollbar-width: none;

              &::-webkit-scrollbar {
                width: 0;
                height: 0;
              }
            }
          }
        }

        &-item {
          display: flex;
          width: 100%;
          height: 36px;
          flex-shrink: 0;
          align-items: center;
          background: var(--loyalty-suite-light);
          padding: 5px 6px 5px 8px;
          border-radius: 7px;
          margin-bottom: 5px;
          border: 1px dashed var(--loyalty-suite-borders);

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

          &-name {
            font-size: 13px;
            color: var(--loyalty-suite-dark);
            line-height: 15px;
            margin-right: 5px;
          }

          &-status {
            display: flex;
            flex-shrink: 0;
            width: 20px;
            height: 20px;
            margin-right: 7px;
            background: var(--loyalty-suite-positive);
            border-radius: 50%;
            padding-bottom: 1px;

            &::after {
              display: flex;
              justify-content: center;
              align-items: center;
              content: '\2714';
              width: 100%;
              height: 100%;
              color: var(--loyalty-suite-light);
              font-size: 10px;
              font-weight: 700;
              line-height: normal;
            }
          }

          &-details {
            display: flex;
            flex-shrink: 0;
            margin-left: auto;
            padding: 5px 6px;
            background: var(--loyalty-suite-primary);
            border-radius: 5px;
            transition: 0.3s;

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

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

          &-empty {
            display: flex;
            width: 100%;
            height: 36px;
            flex-shrink: 0;
            text-align: center;
            justify-content: center;
            align-items: center;
            background: var(--loyalty-suite-light);
            border: 1px dashed var(--loyalty-suite-borders-60);
            padding: 5px 8px;
            border-radius: 5px;
            font-size: 13px;
            line-height: 15px;
          }
        }

        &-scroll-up {
          display: flex;
          flex-shrink: 0;
          width: 22px;
          height: 22px;
          justify-content: center;
          align-items: center;
          position: absolute;
          top: -10px;
          left: 50%;
          transform: translateX(-50%);
          z-index: 2;
          border-radius: 50%;
          background: var(--loyalty-suite-primary-80);
          transition: 0.3s;

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

          .loyalty-suite-material-icons {
            font-size: 20px;
            color: var(--loyalty-suite-light);
            transform: rotate(-90deg);
          }
        }

        &-scroll-down {
          display: flex;
          flex-shrink: 0;
          width: 22px;
          height: 22px;
          justify-content: center;
          align-items: center;
          position: absolute;
          bottom: -10px;
          left: 50%;
          transform: translateX(-50%);
          z-index: 2;
          border-radius: 50%;
          background: var(--loyalty-suite-primary-80);
          transition: 0.3s;

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

          .loyalty-suite-material-icons {
            font-size: 20px;
            color: var(--loyalty-suite-light);
            transform: rotate(90deg);
          }
        }
      }
    }
  }

  &.loyalty-suite-rank__card_modern {
    &.loyalty-suite-rank__card_no-rewards {
      .loyalty-suite-rank__card {
        &-wrapper {
          padding-bottom: 30px;
        }
      }
    }

    &.loyalty-suite-rank__card_my {
      .loyalty-suite-rank__card {
        &-wrapper {
          justify-content: center;
        }

        &-share {
          display: flex;
          border: 1px solid var(--loyalty-suite-borders-60);
          border-radius: 4px;
          justify-content: center;
          align-items: center;
          order: 3;
          margin-top: 30px;
          padding: 5px 10px;
          font-size: 14px;
          color: var(--loyalty-suite-dark);
          transition: 0.3s;
          cursor: pointer;
          span {
            color: var(--loyalty-suite-primary);
            font-size: 16px;
            margin-left: 6px;
            transition: 0.3s;
          }
          &:hover {
            background: var(--loyalty-suite-primary);
            color: var(--loyalty-suite-light);
            span {
              color: var(--loyalty-suite-light);
            }
          }
        }

        &-name {
          padding: 20px 30px;
        }
      }
    }

    &.loyalty-suite-rank__card_no-click {
      pointer-events: none;
    }

    a.loyalty-suite-rank__card-content {
      display: flex;
      width: 100%;
      flex-direction: column;
      align-items: center;
      text-decoration: none;
      border-radius: 8px;

      &:hover {
        color: var(--loyalty-suite-dark);
      }
    }

    .loyalty-suite-rank__card {
      &-wrapper {
        display: flex;
        width: 100%;
        height: 100%;
        flex-direction: column;
        align-items: center;
        background: var(--loyalty-suite-light);
        box-shadow: 0 3px 10px 0 rgba(0, 0, 0, 0.2);
        border-radius: 8px;
        padding: 0;
      }

      &-current {
        display: flex;
        justify-content: center;
        align-items: center;
        padding: 4px 7px 5px 7px;
        color: var(--loyalty-suite-light);
        font-size: 11px;
        font-weight: 700;
        line-height: 1;
        border-radius: 4px;
        background: var(--loyalty-suite-positive);
        position: absolute;
        top: -10px;
        left: 50%;
        transform: translateX(-50%);
        z-index: 2;
      }

      &-progress {
        display: flex;
        flex-direction: column;
        width: 70%;
        padding: 20px 0 0 0;
        order: 3;

        &-value {
          display: flex;
          font-size: 12px;
          color: var(--loyalty-suite-dark);
          justify-content: center;
          text-transform: lowercase;
        }

        &-wrapper {
          display: flex;
          width: 100%;
          height: 5px;
          border-radius: 8px;
          background: var(--loyalty-suite-borders-40);
          position: relative;
          margin: 0 0 5px 0;
        }

        &-bar {
          display: flex;
          position: absolute;
          left: 0;
          top: 0;
          height: 5px;
          border-radius: 8px;
          background: var(--loyalty-suite-positive);
        }
      }

      &-name {
        display: flex;
        width: 100%;
        justify-content: center;
        font-size: 16px;
        font-weight: 700;
        color: var(--loyalty-suite-primary);
        line-height: normal;
        text-align: center;
        padding: 15px 20px;
        order: 1;
      }

      &-set {
        display: inline-flex;
        text-transform: capitalize;
        font-size: 16px;
        font-weight: 400;
        color: var(--loyalty-suite-dark);
        line-height: normal;
        margin-right: 5px;
      }

      &-header {
        display: flex;
        width: 100%;
        justify-content: center;
        align-items: center;
        border-radius: 0;
        order: 2;

        &.loyalty-suite-rank__card-header_icon {
          height: 95px;
        }
      }

      &-icon {
        display: flex;
        flex-shrink: 0;
        width: 70px;
        height: 70px;
        object-fit: cover;
      }

      &-image {
        display: flex;
        flex-shrink: 0;
        width: 100%;
        aspect-ratio: 16 / 9;
        object-fit: cover;
        border-radius: 0;
      }

      &-reward {
        display: flex;
        width: 100%;
        flex-direction: column;
        padding: 15px 20px 20px 20px;
        order: 4;

        &-title {
          display: flex;
          width: 100%;
          justify-content: center;
          font-size: 14px;
          color: var(--loyalty-suite-dark);
          line-height: normal;
          margin-bottom: 10px;
        }

        &-list {
          display: flex;
          width: 100%;
          flex-direction: column;
          position: relative;

          &-wrapper {
            display: flex;
            width: 100%;
            flex-direction: column;

            &.loyalty-suite-rank__card-reward-list-wrapper_fix {
              height: 123px;
              overflow-y: auto;
              scroll-behavior: smooth;
              scrollbar-width: none;

              &::-webkit-scrollbar {
                width: 0;
                height: 0;
              }
            }
          }
        }

        &-item {
          display: flex;
          width: 100%;
          height: 36px;
          flex-shrink: 0;
          align-items: center;
          background: var(--loyalty-suite-light);
          padding: 5px 6px 5px 8px;
          border-radius: 7px;
          margin-bottom: 5px;
          border: 1px dashed var(--loyalty-suite-borders);

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

          &-name {
            font-size: 13px;
            color: var(--loyalty-suite-dark);
            line-height: 15px;
            margin-right: 5px;
          }

          &-status {
            display: flex;
            flex-shrink: 0;
            width: 20px;
            height: 20px;
            margin-right: 7px;
            background: var(--loyalty-suite-positive);
            border-radius: 50%;
            padding-bottom: 1px;

            &::after {
              display: flex;
              justify-content: center;
              align-items: center;
              content: '\2714';
              width: 100%;
              height: 100%;
              color: var(--loyalty-suite-light);
              font-size: 10px;
              font-weight: 700;
              line-height: normal;
            }
          }

          &-details {
            display: flex;
            flex-shrink: 0;
            margin-left: auto;
            padding: 5px 6px;
            background: var(--loyalty-suite-primary);
            border-radius: 5px;
            transition: 0.3s;

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

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

          &-empty {
            display: flex;
            width: 100%;
            height: 36px;
            flex-shrink: 0;
            text-align: center;
            justify-content: center;
            align-items: center;
            background: var(--loyalty-suite-light);
            border: 1px dashed var(--loyalty-suite-borders-60);
            padding: 5px 8px;
            border-radius: 5px;
            font-size: 13px;
            line-height: 15px;
          }
        }

        &-scroll-up {
          display: flex;
          flex-shrink: 0;
          width: 22px;
          height: 22px;
          justify-content: center;
          align-items: center;
          position: absolute;
          top: -10px;
          left: 50%;
          transform: translateX(-50%);
          z-index: 2;
          border-radius: 50%;
          background: var(--loyalty-suite-primary-80);
          transition: 0.3s;

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

          .loyalty-suite-material-icons {
            font-size: 20px;
            color: var(--loyalty-suite-light);
            transform: rotate(-90deg);
          }
        }

        &-scroll-down {
          display: flex;
          flex-shrink: 0;
          width: 22px;
          height: 22px;
          justify-content: center;
          align-items: center;
          position: absolute;
          bottom: -10px;
          left: 50%;
          transform: translateX(-50%);
          z-index: 2;
          border-radius: 50%;
          background: var(--loyalty-suite-primary-80);
          transition: 0.3s;

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

          .loyalty-suite-material-icons {
            font-size: 20px;
            color: var(--loyalty-suite-light);
            transform: rotate(90deg);
          }
        }
      }
    }
  }
}
</style>
