<template>
  <div class="topic-switcher">
    <div class="topic-switcher-arrow">
      <img src="@/assets/images/bottom-menu-arrow-big.svg" alt="" />
      <img src="@/assets/images/bottom-menu-arrow-small.svg" alt="" />
    </div>
    <div class="topic-switcher-menu">
      <button
        v-for="topic in switcherTopics"
        :key="topic.key"
        class="topic-switcher-item"
        :class="{ 'is-active': topic.key === modelValue }"
        :style="{
          '--topic-color': topic.color,
          '--topic-item-width': topic.width ? `${topic.width}px` : undefined,
        }"
        type="button"
        @pointerup="handleTopicClick($event, topic.key)"
        @click="handleTopicClick($event, topic.key)"
      >
        <span class="topic-name" :class="{ 'has-sub-name': topic.subName }">
          <span class="topic-title">{{ topic.name }}</span>
          <small v-if="topic.subName">{{ topic.subName }}</small>
        </span>
      </button>
    </div>
    <div class="topic-switcher-arrow is-reverse">
      <img src="@/assets/images/bottom-menu-arrow-big.svg" alt="" />
      <img src="@/assets/images/bottom-menu-arrow-small.svg" alt="" />
    </div>
  </div>
</template>

<script setup>
import { computed } from "vue"
import { forestGrassWetTopics } from "@/config/layers"

const props = defineProps({
  modelValue: {
    type: String,
    required: true,
  },
  topics: {
    type: Array,
    default: () => forestGrassWetTopics,
  },
})

const emit = defineEmits(["update:modelValue"])
let lastPointerTopic = ""
let lastPointerAt = 0

const switcherTopics = computed(() => (props.topics?.length ? props.topics : forestGrassWetTopics))

function handleTopicClick(event, topicKey) {
  event.currentTarget?.blur?.()
  if (topicKey === props.modelValue) return
  const now = Date.now()
  if (event.type === "click" && lastPointerTopic === topicKey && now - lastPointerAt < 450) return
  if (event.type === "pointerup") {
    lastPointerTopic = topicKey
    lastPointerAt = now
  }
  emit("update:modelValue", topicKey)
}
</script>

<style lang="scss">
.topic-switcher {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  width: 600px;
  height: 52px;
  padding-top: 10px;
  background: url("~@/assets/images/bottom-menu-bg.png") no-repeat center bottom;
  background-size: 100% 52px;
  pointer-events: all;

  &-arrow {
    display: flex;
    align-items: center;
    height: 30px;
    margin: 0 12px;

    &.is-reverse {
      transform: scaleX(-1);
    }

    img {
      animation: arrowAnimate 2s ease-in-out infinite;
    }

    img:last-child {
      animation: arrowAnimate2 2s ease-in-out infinite;
    }
  }

  &-menu {
    display: flex;
    gap: 4px;
  }

  &-item {
    position: relative;
    width: var(--topic-item-width, 112px);
    height: 32px;
    padding: 0;
    border: 0;
    background: url("~@/assets/images/bottom-menu-btn.png") no-repeat;
    background-size: 100% 100%;
    color: #fff;
    cursor: pointer;
    overflow: hidden;

    &:hover,
    &.is-active {
      background: url("~@/assets/images/bottom-menu-btn-hover.png") no-repeat;
      background-size: 100% 100%;
    }
  }

  .topic-name {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    height: 100%;
    padding: 0 5px;
    text-align: center;
    white-space: nowrap;
  }

  .topic-name,
  .topic-title,
  .topic-name small {
    padding-top: 0;
    font-size: 16px;
    font-weight: 700;
    line-height: 18px;
    background: -webkit-linear-gradient(rgba(117, 232, 255, 1), rgba(255, 255, 255, 1));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
  }

  .topic-name.has-sub-name {
    gap: 1px;
  }

  .topic-name small {
    display: block;
    max-width: 100%;
    overflow: hidden;
    font-size: 10px;
    font-weight: 600;
    line-height: 11px;
    text-overflow: ellipsis;
  }
}
</style>
