<script setup lang="ts">
// P16 下载页
import { onMounted, computed, watch, ref } from 'vue'
import { storeToRefs } from 'pinia'
import { useAppVersionStore } from '@/stores/appVersion'
import { useI18nStore } from '@/stores/i18n'
import { useI18n } from 'vue-i18n'
import { qrCodeImageUrl } from '@/utils/qr'
import heroVisual  from '@/assets/download/hero-visual.png'
import iconApple   from '@/assets/download/icon-apple.png'
import iconAndroid from '@/assets/download/icon-android.png'

const { t } = useI18n()
const i18nStore = useI18nStore()
const { current: locale } = storeToRefs(i18nStore)

const versionStore = useAppVersionStore()
const { android, ios, loading } = storeToRefs(versionStore)

onMounted(() => {
  versionStore.fetchAll()
})
watch(locale, () => {
  versionStore.fetchAll()
})

const iosUrl     = computed(() => ios.value?.downloadUrl || '')
const androidUrl = computed(() => android.value?.downloadUrl || '')
const latestInfo = computed(() => android.value ?? ios.value)

const iosQr     = ref('')
const androidQr = ref('')

watch(iosUrl, async (url) => {
  iosQr.value = url ? await qrCodeImageUrl(url, 160) : ''
}, { immediate: true })

watch(androidUrl, async (url) => {
  androidQr.value = url ? await qrCodeImageUrl(url, 160) : ''
}, { immediate: true })

function handleDownload(e: MouseEvent, url: string) {
  if (!url) e.preventDefault()
}
</script>

<template>
  <main class="page-main">
    <section class="download-hero">
      <div class="page-container hero-inner">
        <div class="hero-content">
          <h1 class="hero-title" v-html="t('download.heroTitle').replace('\n', '<br>')"></h1>
          <p class="hero-desc">{{ t('download.appDesc') }}</p>

          <p v-if="latestInfo?.newVersion" class="version-tag">
            {{ t('download.latestVersion') }} v{{ latestInfo.newVersion }}
            <span v-if="latestInfo.publishTime" class="version-time">· {{ latestInfo.publishTime }}</span>
          </p>

          <div class="download-buttons">
            <div class="dl-btn-wrap" :class="{ 'has-qr': !!iosUrl && !!iosQr }">
              <a
                :href="iosUrl || '#'"
                :class="['dl-btn', 'dark-card', { 'is-disabled': !iosUrl }]"
                :target="iosUrl ? '_blank' : undefined"
                rel="noopener"
                @click="handleDownload($event, iosUrl)"
              >
                <img :src="iconApple" alt="iOS" class="dl-icon" />
                <span class="dl-text">{{ loading ? t('common.loading') : (iosUrl ? t('download.ios') : t('download.iosNotReleased')) }}</span>
              </a>
              <div v-if="iosUrl && iosQr" class="dl-qrcode-popover">
                <div class="dl-qrcode-frame">
                  <img :src="iosQr" :alt="t('download.scanToDownload')" class="dl-qrcode-img" />
                </div>
                <p class="dl-qrcode-hint">{{ t('download.scanToDownload') }}</p>
              </div>
            </div>
            <div class="dl-btn-wrap" :class="{ 'has-qr': !!androidUrl && !!androidQr }">
              <a
                :href="androidUrl || '#'"
                :class="['dl-btn', 'dark-card', { 'is-disabled': !androidUrl }]"
                :target="androidUrl ? '_blank' : undefined"
                rel="noopener"
                @click="handleDownload($event, androidUrl)"
              >
                <img :src="iconAndroid" alt="Android" class="dl-icon" />
                <span class="dl-text">{{ loading ? t('common.loading') : (androidUrl ? t('download.android') : t('download.androidNotReleased')) }}</span>
              </a>
              <div v-if="androidUrl && androidQr" class="dl-qrcode-popover">
                <div class="dl-qrcode-frame">
                  <img :src="androidQr" :alt="t('download.scanToDownload')" class="dl-qrcode-img" />
                </div>
                <p class="dl-qrcode-hint">{{ t('download.scanToDownload') }}</p>
              </div>
            </div>
          </div>

          <div v-if="latestInfo?.updateDescription" class="update-notes">
            <h3 class="update-title">{{ t('download.updateNotes') }}</h3>
            <p class="update-text">{{ latestInfo.updateDescription }}</p>
          </div>
        </div>

        <div class="hero-visual">
          <img :src="heroVisual" alt="iBit APP" class="hero-img" />
        </div>
      </div>
    </section>
  </main>
</template>

<style scoped>
.download-hero {
  min-height: calc(100vh - var(--header-height));
  display: flex;
  align-items: center;
  background: var(--color-bg-primary);
  position: relative;
  overflow: hidden;
}
.download-hero::before {
  content: '';
  position: absolute;
  right: -10%;
  top: 50%;
  transform: translateY(-50%);
  width: 720px;
  height: 720px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(240,185,11,0.12) 0%, rgba(240,185,11,0.02) 45%, transparent 70%);
  pointer-events: none;
}

.hero-inner {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.1fr);
  gap: var(--space-12);
  align-items: center;
  padding-top: var(--space-12);
  padding-bottom: var(--space-12);
  position: relative;
  z-index: 1;
}

.hero-title {
  font-size: 48px;
  font-weight: var(--fw-bold);
  color: var(--color-text-primary);
  line-height: 1.25;
  margin-bottom: var(--space-3);
}
.hero-desc {
  font-size: var(--text-body);
  color: var(--color-text-secondary);
  margin-bottom: var(--space-10);
}

.download-buttons {
  display: flex;
  gap: var(--space-4);
  flex-wrap: wrap;
  overflow: visible;
}
.dl-btn-wrap {
  position: relative;
}
.dl-btn-wrap.has-qr:hover {
  z-index: 2;
}
.dl-qrcode-popover {
  position: absolute;
  top: calc(100% + 12px);
  left: 50%;
  transform: translateX(-50%) translateY(-6px);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  width: max-content;
  padding: var(--space-4);
  border-radius: var(--radius-md);
  background: var(--color-bg-secondary);
  border: 1px solid var(--color-border);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  transition: opacity var(--transition), transform var(--transition), visibility var(--transition);
  z-index: 10;
  text-align: center;
}
.dl-qrcode-popover::before {
  content: '';
  position: absolute;
  top: -6px;
  left: 50%;
  transform: translateX(-50%) rotate(45deg);
  width: 12px;
  height: 12px;
  background: var(--color-bg-secondary);
  border-top: 1px solid var(--color-border);
  border-left: 1px solid var(--color-border);
}
.dl-btn-wrap.has-qr:hover .dl-qrcode-popover {
  opacity: 1;
  visibility: visible;
  transform: translateX(-50%) translateY(0);
}
.dl-qrcode-frame {
  width: 160px;
  height: 160px;
  padding: 8px;
  border-radius: var(--radius-sm);
  background: #fff;
  box-sizing: border-box;
}
.dl-qrcode-img {
  width: 100%;
  height: 100%;
  max-width: none;
  object-fit: contain;
  aspect-ratio: 1 / 1;
}
.dl-qrcode-hint {
  margin-top: var(--space-3);
  font-size: var(--text-caption);
  color: var(--color-text-secondary);
  white-space: nowrap;
}
.dl-btn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-8);
  border-radius: var(--radius-md);
  min-width: 180px;
  cursor: pointer;
  transition: border-color var(--transition), transform var(--transition);
}
.dl-btn:hover {
  border-color: var(--color-primary);
  transform: translateY(-2px);
}
.dl-icon {
  width: 24px;
  height: 24px;
  object-fit: contain;
}
.dl-text {
  font-size: var(--text-body);
  font-weight: var(--fw-semibold);
  color: var(--color-text-primary);
}
.dl-btn.is-disabled {
  opacity: 0.5;
  cursor: not-allowed;
  pointer-events: auto;
}
.dl-btn.is-disabled:hover {
  border-color: inherit;
  transform: none;
}

.version-tag {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-1) var(--space-3);
  margin-bottom: var(--space-4);
  border-radius: var(--radius-sm);
  background: rgba(240, 185, 11, 0.12);
  color: var(--color-primary);
  font-size: var(--text-caption);
  font-weight: var(--fw-semibold);
}
.version-time {
  color: var(--color-text-secondary);
  font-weight: var(--fw-regular);
}

.update-notes {
  margin-top: var(--space-8);
  padding: var(--space-5) var(--space-6);
  border-radius: var(--radius-md);
  background: var(--color-bg-secondary);
  max-width: 520px;
}
.update-title {
  font-size: var(--text-body);
  font-weight: var(--fw-semibold);
  color: var(--color-text-primary);
  margin-bottom: var(--space-2);
}
.update-text {
  font-size: var(--text-caption);
  color: var(--color-text-secondary);
  line-height: 1.6;
  white-space: pre-line;
}

/* Hero visual */
.hero-visual {
  display: flex;
  justify-content: center;
  align-items: center;
}
.hero-img {
  width: 100%;
  max-width: 640px;
  object-fit: contain;
  filter: drop-shadow(0 20px 80px rgba(240,185,11,0.2));
}

/* ── 移动端 ── */
@media (max-width: 960px) {
  .hero-title { font-size: 36px; }
}
@media (max-width: 768px) {
  .download-hero { min-height: unset; padding: var(--space-10) 0; }
  .hero-inner {
    grid-template-columns: 1fr;
    padding-top: var(--space-6);
    padding-bottom: var(--space-6);
    gap: var(--space-6);
  }
  .hero-title { font-size: 28px; }
  .download-buttons { flex-wrap: wrap; }
  .dl-btn { flex: 1; min-width: 140px; }
  .hero-img { max-width: 380px; }
}
</style>
