<script setup>
import { ref, onMounted, computed } from "vue"
import { useRouter } from 'vue-router'
import Popup from './Popup.vue'
import { globalStore } from "../globalstore.js";
import globalToastEvent, { ToastType } from '../globalToastEvent';
import { Storage, generateQR } from "../libs/utils";
import Haibao from "@/libs/haibao";
import mask from "../assets/images/haibao-mask.webp";
import haibaoCoverBorder from "../assets/images/haibao-cover.webp";
import bg from "../assets/images/haibao-bg.webp"

defineProps({
  show: true
})

const isPopupVisible = ref(false);


const chartsBattle = ()=> {
  globalStore.chartsBattle = true;
  isPopupVisible.value = true;
}

const router = useRouter();
const navigateToHome = () => {
  router.push({
    name: 'home'
  })
}

const handleLottery = () => {
  globalToastEvent.emit(ToastType.SHOW_LOTTERY)
}

// 海报
const haibaoShow = ref(false);
const haibaoUrl = ref('');
const userHaibaoUrl = ref('');
const userhaibaoCover = computed(() => {
  return { backgroundImage: `url(${userHaibaoUrl.value})` }
})

const openHaibao = (e) => {
  haibaoShow.value = true
  handleHaibao()
}
onMounted(openHaibao)

const loadImage = (src)=> {
  return new Promise((resolve, reject) => {
    const img = new Image();
    img.crossOrigin = "Anonymous";
    img.onload = () => resolve(img);
    img.onerror = () => reject(new Error(`无法加载图片: ${src}`));
    img.src = src;
  });
}
const handleHaibao = async () => {
  if (haibaoUrl.value) {
    return
  }

  const loading = weui.loading()
  const infos = Storage.get("userinfos")
  const haibaoCover = new Haibao(951, 1607)
  if (!globalStore.result_url) {
    router.push({
      name: 'home'
    });
  }
  const userPicture = await loadImage(globalStore.result_url)
  haibaoCover.add(userPicture, 0, 0)
  haibaoCover.add(mask, 10, 100)
  haibaoCover.add(haibaoCoverBorder, 0, 0)
  haibaoCover.draw('destination-in').then(() => {
    haibaoCover.generate({ mimeType: 'image/png' }).then(async (url) => {
      userHaibaoUrl.value = url

      const haibaoSave = new Haibao(1080, 2160)
      const qrcode = await generateQR(`fromid=${infos.invite_code}&merge_id=${infos.merge_id}`, 200, 200)
      haibaoSave.add(url, 62, 350)
      haibaoSave.add(bg, 0, 0)
      haibaoSave.add(qrcode, 115, 1875)
      haibaoSave.draw().then(() => {
        haibaoSave.text(infos.nickname, haibaoSave.canvas.width / 2, 200, { font: 'bold 50px Arial', color: '#ffee6f' })
        haibaoSave.generate({ mimeType: 'image/png' }).then(url => {
          haibaoUrl.value = url
          loading.hide()
        }).catch(err => {
          console.log(err)
          weui.alert("海报生成失败，请重新生成")
          loading.hide()
        })
      }).catch(err => {
        console.log(err)
        weui.alert("海报生成失败，请重新生成")
        loading.hide()
      })
    })
  })
}
// let userPictureVal = userPicture;
// const getBackgroundImage = () => {
//   if (globalStore.result_url) {
//     return userPictureVal = globalStore.result_url;
//   } else {
//     return userPictureVal;
//   }
// };
</script>

<template>
  <div>
    <div class="home-wrapper">
      <div class="scene-item item-1">
        <img src="../assets/images/go-home-btn.webp" @click="navigateToHome" alt="回到首页">
      </div>

      <!-- <div class="mask-background image-container" :style="{ backgroundImage: `url(${getBackgroundImage()})` }">
      </div>
      <img src="../assets/images/mengban-border.webp" class="mengban-border" alt="蒙版"> -->
      <div class="fullsection" v-show="haibaoShow">
        <div class="haibao" :style="userhaibaoCover">
          <img :src="haibaoUrl" alt="">
          <!-- <div class="close" @click="haibaoShow = false"></div> -->
        </div>
      </div>

      <div class="scene-item item-2">
        <img src="../assets/images/charts-battle.webp" @click="chartsBattle" alt="参与打榜">
      </div>

      <div class="scene-item item-3" @click="handleLottery">
        <img src="../assets/images/instant-win.webp" alt="立即抽奖">
      </div>
    </div>
  </div>

  <Popup v-model:show="isPopupVisible" />
</template>

<style scoped>
.mask-background {
  -webkit-mask-image: url("../assets/images/my-photo-mengban.webp");
  mask-image: url("../assets/images/my-photo-mengban.webp");
  -webkit-mask-size: contain;
  mask-size: contain;
  -webkit-mask-position: center;
  mask-position: center;
  -webkit-mask-repeat: no-repeat;
  mask-repeat: no-repeat;
}

.mengban-border {
  width: 81vw;
  position: absolute;
  top: 28vw;
}

.image-container {
  width: 80vw;
  top: 5.2vw;
  margin-bottom: 3vw;
  background-image: url('../assets/images/home-bg.webp');
  background-size: cover;
  background-repeat: no-repeat;
  min-height: -webkit-fill-available;
  position: absolute;
  cursor: pointer;
}

.fullsection {
  position: fixed;
  top: 25vw;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  /* background-color: rgba(0, 0, 0, .7); */
}

.haibao {
  position: relative;
  top: -25vw;
  width: 70vw;
  height: 119.444444vw;
  background-image: url("../assets/images/haibao-cover.webp");
  background-repeat: no-repeat;
  background-size: 100%;
}

.haibao img {
  width: 100%;
  height: 100%;
  display: block;
  opacity: 0;
}

.close {
  position: absolute;
  width: 8.148148vw;
  height: 8.148148vw;
  right: 5vw;
  top: 15vw;
  background-image: url("../assets/images/close-btn.webp");
  background-repeat: no-repeat;
  background-size: 100%;
}

.haibao .close {
  left: 50%;
  bottom: -5vw;
  top: initial;
  transform: translate3d(-50%,0,0);
}

.home-wrapper {
  width: 100%;
  height: 92vh;
  background-image: url('../assets/images/img-result-bg.webp');
  background-size: cover;
  background-repeat: no-repeat;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: relative;
  min-height: -webkit-fill-available;
}

.scene-item {
  position: absolute;
  cursor: pointer;
  transition: all 0.4s ease;
  border-radius: 8px;
  overflow: hidden;
  border: 3px solid transparent;
  animation: float 4s ease-in-out infinite;
}

.scene-item:hover {
  transform: scale(1.05);
}

.scene-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.item-1 {
  width: 16vw;
  top: 6.7%;
  left: 5%;
  animation-delay: 0s;
}
.item-2 {
  width: 45vw;
  bottom: 3%;
  left: 5%;
}
.item-3 {
  width: 45vw;
  bottom: 3%;
  right: 5%;
}
</style>