<template>
  <div class="topic-switcher" :class="{ 'is-compact': compact }" role="tablist" aria-label="专题切换">
    <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"
        role="tab"
        :aria-selected="topic.key === modelValue"
        @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>
</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,
  },
  compact: {
    type: Boolean,
    default: false,
  },
})

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: inline-flex;
  align-items: center;
  justify-content: center;
  width: max-content;
  max-width: calc(100vw - 96px);
  min-height: 32px;
  padding: 2px;
  border: 1px solid rgba(91, 224, 244, 0.34);
  border-radius: 18px;
  background: linear-gradient(180deg, rgba(5, 34, 48, 0.92), rgba(2, 18, 29, 0.9));
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.05),
    0 10px 26px rgba(0, 7, 14, 0.32);
  box-sizing: border-box;
  overflow: hidden;
  pointer-events: all;

  &-menu {
    display: flex;
    gap: 2px;
    max-width: 100%;
    border-radius: 15px;
    overflow: hidden;
  }

  &-item {
    position: relative;
    flex: 0 0 var(--topic-item-width, 112px);
    width: var(--topic-item-width, 112px);
    min-height: 26px;
    padding: 0 10px;
    border: 1px solid transparent;
    border-radius: 15px;
    color: rgba(220, 246, 249, 0.82);
    background: rgba(5, 38, 52, 0.42);
    cursor: pointer;
    overflow: hidden;
    transition: color 160ms ease, border-color 160ms ease, background 160ms ease, box-shadow 160ms ease;

    &:hover {
      border-color: rgba(91, 224, 244, 0.38);
      color: #f5ffff;
      background: rgba(14, 78, 94, 0.54);
    }

    &.is-active {
      border-color: rgba(126, 251, 246, 0.64);
      color: #ffffff;
      background: linear-gradient(180deg, rgba(19, 101, 116, 0.9), rgba(5, 48, 62, 0.9));
      box-shadow:
        inset 0 0 0 1px rgba(126, 251, 246, 0.12),
        inset 0 -2px 0 var(--topic-color, #30dcff),
        0 0 14px rgba(48, 220, 255, 0.14);
    }

    &:focus-visible {
      outline: 2px solid #7efbf6;
      outline-offset: 2px;
    }
  }

  .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;
    color: inherit;
    font-size: 13px;
    font-weight: 600;
    line-height: 16px;
  }

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

  .topic-name small {
    display: block;
    max-width: 100%;
    overflow: hidden;
    font-size: 9px;
    font-weight: 500;
    line-height: 10px;
    opacity: 0.68;
    text-overflow: ellipsis;
  }

  &.is-compact {
    min-height: 29px;
    padding: 1px;
    border-radius: 16px;

    .topic-switcher-menu { gap: 1px; }

    .topic-switcher-item {
      flex-basis: min(var(--topic-item-width, 104px), 104px);
      width: min(var(--topic-item-width, 104px), 104px);
      min-height: 25px;
      padding: 0 2px;
      border-radius: 14px;
    }

    .topic-name,
    .topic-title {
      font-size: 12px;
      line-height: 15px;
    }
  }
}
</style>
