<template>
  <div class="long-text-guide-card w-full">
    <div v-if="mode === 'list'" class="long-text-guide-card__list w-full">
      <div v-for="item in cards" :key="item.id"
        class="long-text-guide-card__summary-card bg-white rounded-24 border-box" :class="{ 'is-disabled': disabled }"
        @click="openDetail(item)">
        <div class="long-text-guide-card__badge font-size-12 font-900" :class="`is-${item.badgeTone}`">
          {{ item.badge }}
        </div>
        <div class="long-text-guide-card__summary-title color-1E293B font-size-18 font-900">
          {{ item.title }}
        </div>
        <div class="long-text-guide-card__summary-text color-94A3B8 font-size-14 font-500 ellipsis-2">
          {{ item.summary }}
        </div>
        <div class="long-text-guide-card__summary-footer flex flex-items-center flex-justify-between">
          <text class="color-CBD5E1 font-size-12 font-800">{{ item.footer }}</text>
          <uni-icons type="right" size="16" color="#CBD5E1"></uni-icons>
        </div>
      </div>
    </div>

    <div v-else class="long-text-guide-card__detail-card bg-white rounded-24 border-box overflow-hidden">
      <div class="long-text-guide-card__detail-header flex flex-items-center border-bottom-F1F5F9">
        <div class="long-text-guide-card__back flex flex-items-center flex-justify-center rounded-full font-700"
          @click="closeDetail">
          <uni-icons type="left" size="16" color="#CBD5E1"></uni-icons>
        </div>
        <div class="long-text-guide-card__detail-title color-1E293B font-size-18 font-900 ellipsis-1">
          {{ activeItem.title }}
        </div>
        <div class="long-text-guide-card__badge long-text-guide-card__detail-badge font-size-12 font-900"
          :class="`is-${activeItem.badgeTone}`">
          {{ activeItem.badge }}
        </div>
      </div>

      <div class="long-text-guide-card__rich-body">
        <div v-for="(paragraph, paragraphIndex) in paragraphsBeforeImage" :key="`before-${paragraphIndex}`"
          class="long-text-guide-card__paragraph color-475569 font-size-15 font-500">
          <template v-for="(segment, segmentIndex) in getSegments(paragraph)"
            :key="`${paragraphIndex}-${segmentIndex}`">
            <span v-if="segment.highlight" class="long-text-guide-card__highlight font-800">
              {{ segment.text }}
            </span>
            <span v-else>{{ segment.text }}</span>
          </template>
        </div>

        <image v-if="activeItem.image" class="long-text-guide-card__main-image w-full rounded-14 block"
          :src="activeItem.image" mode="aspectFill" />

        <div class="long-text-guide-card__section-title color-1E293B font-size-16 font-900">
          {{ activeItem.sectionTitle }}
        </div>
        <div class="long-text-guide-card__tip-block bg-ECFDF5">
          <div v-for="(tip, index) in activeItem.tips || []" :key="tip"
            class="long-text-guide-card__tip color-047857 font-size-14 font-800">
            {{ index + 1 }} {{ tip }}
          </div>
        </div>

        <div v-for="(paragraph, paragraphIndex) in paragraphsAfterTips" :key="`after-${paragraphIndex}`"
          class="long-text-guide-card__paragraph color-475569 font-size-15 font-500">
          <template v-for="(segment, segmentIndex) in getSegments(paragraph)"
            :key="`${paragraphIndex}-${segmentIndex}`">
            <span v-if="segment.highlight" class="long-text-guide-card__highlight font-800">
              {{ segment.text }}
            </span>
            <span v-else>{{ segment.text }}</span>
          </template>
        </div>
      </div>

      <div v-if="activeItem.action?.type === 'map'" class="long-text-guide-card__action-zone">
        <div class="long-text-guide-card__action-label color-94A3B8 font-size-11 font-800">
          {{ activeItem.action.label }}
        </div>
        <div class="long-text-guide-card__map-card rounded-16 overflow-hidden">
          <div class="long-text-guide-card__map-image-wrap relative">
            <img class="long-text-guide-card__map-image w-full block" :src="activeItem.action.image"
              mode="aspectFill" />
            <div class="long-text-guide-card__map-tag color-white font-size-10 font-900">
              {{ activeItem.action.tag }}
            </div>
          </div>
          <div class="long-text-guide-card__map-bar flex flex-items-center bg-white">
            <div class="flex-full">
              <div class="color-1E293B font-size-15 font-900">{{ activeItem.action.title }}</div>
              <div class="color-94A3B8 font-size-12 font-800 mt-2">{{ activeItem.action.subtitle }}</div>
            </div>
            <div class="long-text-guide-card__nav-btn color-white bg-0F172A font-size-14 font-900"
              @click="handleAction(activeItem)">
              {{ activeItem.action.buttonText }}
            </div>
          </div>
        </div>
      </div>

      <div v-else-if="activeItem.action?.type === 'image'" class="long-text-guide-card__action-zone">
        <div class="long-text-guide-card__action-label color-94A3B8 font-size-11 font-800">
          {{ activeItem.action.label }}
        </div>
        <div class="long-text-guide-card__photo-card relative" @click="handleAction(activeItem)">
          <img class="long-text-guide-card__photo-image w-full block" :src="activeItem.action.image"
            mode="aspectFill" />
          <div
            class="long-text-guide-card__expand flex flex-items-center flex-justify-center rounded-full color-white font-size-18 font-900">
            ↗
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed, ref } from "vue";

const props = defineProps({
  data: {
    type: Object,
    default: () => ({}),
  },
  disabled: {
    type: Boolean,
    default: false,
  },
});

const emit = defineEmits(["select", "back", "action"]);

const selected = ref(null);
const cards = computed(() => props.data.cards || []);
const activeItem = computed(() => selected.value || cards.value[0] || {});
const mode = computed(() => (selected.value ? "detail" : "list"));
const paragraphsBeforeImage = computed(() => activeItem.value.paragraphs?.slice(0, 1) || []);
const paragraphsAfterTips = computed(() => activeItem.value.paragraphs?.slice(1) || []);

const openDetail = (item) => {
  if (props.disabled) return;
  selected.value = item;
  emit("select", item);
};

const closeDetail = () => {
  selected.value = null;
  emit("back");
};

const handleAction = (item) => {
  if (props.disabled) return;
  emit("action", item);
};

const getSegments = (paragraph) => {
  const text = paragraph?.text || "";
  const highlights = paragraph?.highlights || [];
  if (!highlights.length) return [{ text, highlight: false }];

  const segments = [];
  let cursor = 0;

  highlights.forEach((highlight) => {
    const start = text.indexOf(highlight, cursor);
    if (start < 0) return;
    if (start > cursor) {
      segments.push({ text: text.slice(cursor, start), highlight: false });
    }
    segments.push({ text: highlight, highlight: true });
    cursor = start + highlight.length;
  });

  if (cursor < text.length) {
    segments.push({ text: text.slice(cursor), highlight: false });
  }

  return segments.length ? segments : [{ text, highlight: false }];
};
</script>

<style scoped lang="scss">
@import "./styles/index.scss";
</style>
