<script setup>
import { ref, computed, watch, onMounted } from "vue"
import { useRouter } from 'vue-router'
import { Request, Storage } from "../libs/utils"
import faceFamily from "../assets/audio/faceFamily.mp3"
import MyPhoto from './MyPhoto.vue'
import PhotoSquare from './PhotoSquare.vue'
import globalToastEvent, { ToastType } from '../globalToastEvent';
import { globalStore } from "../globalstore.js";

defineProps({
  show: true
})
const isMusicOn = ref(false);
const audioElement = ref(null);
onMounted(() => {
  // 创建音频
  audioElement.value = new Audio(faceFamily);
  // 尝试自动播放
  tryAutoPlay();
  //TODO 判断登录之后获取浏览器参数fromid, mergeid
})

// 尝试自动播放
const tryAutoPlay = () => {
  // 先加载音频
  audioElement.value.load();
  // 尝试播放
  const playPromise = audioElement.value.play();
  if (playPromise !== undefined) {
    playPromise.then(() => {
      // 自动播放成功
      isMusicOn.value = true;
      console.log("自动播放成功");
    })
    .catch(error => {
      // 自动播放被阻止
      console.log("自动播放被阻止，需要用户交互:", error);
      isMusicOn.value = false;
      audioElement.value.pause();
    });
  }
};

// 播放/暂停切换
const toggleMusicState = () => {
  isMusicOn.value = !isMusicOn.value;
  if (!isMusicOn.value) {
      audioElement.value.pause();
  } else {
    audioElement.value.play().catch(error => {
      console.log("播放失败:", error);
    });
  }
};

const navigateTodoList = () => {
  globalToastEvent.emit(ToastType.SHOW_TODO)
}
const handlePrizeList = async () => {
  globalToastEvent.emit(ToastType.SHOW_PRIZELIST)
}
const handleRule = () => {
  globalToastEvent.emit(ToastType.SHOW_RULE)
}
const handleLottery = () => {
  globalToastEvent.emit(ToastType.SHOW_LOTTERY)
  if (globalStore.draw_chances <= 0) return;
  globalStore.reducerDrawChances();
}

const router = useRouter();
const navigateSelectTemplatePage = () => {
  if (globalStore.game_chances <= 0) return;
  globalStore.reducerGameChances();
  router.push({
    name: 'selectTemplateV2'
  })
}

const isMyPhotoVisible = ref(false);
const isPhotoSquareVisible = ref(false);


const activeTab = ref('my-photo')

const showMyPhoto = () => { activeTab.value = 'my-photo', isMyPhotoVisible.value = true; }
const showPhotoSquare=()=>{
  activeTab.value='photo-square',
  isPhotoSquareVisible.value = true;
}
</script>

<template>
  <div :show="show">
    <div class="home-wrapper">
      <div class="scene-item item-1" @click="handleLottery" :class="{ 'disabled': globalStore.draw_chances <= 0 }">
        <img src="../assets/images/lottery.png" alt="抽奖">
        <div class="lottery-main">
          <p class="lottery-value">{{ globalStore.draw_chances }}</p>
        </div>
      </div>
      <div class="scene-item item-2" @click="navigateSelectTemplatePage" :class="{ 'disabled': globalStore.game_chances <= 0 }">
        <img src="../assets/images/join.png" alt="立即参与">
        <div class="join-main">
          <p class="join-value">{{ globalStore.game_chances }}</p>
        </div>
      </div>
      <div class="scene-item item-3" @click="navigateTodoList">
        <img src="../assets/images/task.png" alt="任务">
      </div>

      <div @click="toggleMusicState">
        <div v-if="isMusicOn" key="on" class="scene-item item-4">
          <img src="../assets/images/music-on.png" alt="音乐开">
        </div>
        <div v-else key="off" class="scene-item item-5">
          <img src="../assets/images/music-off.png" alt="音乐关">
        </div>
      </div>
      <div class="scene-item item-6" @click="handleRule">
        <img src="../assets/images/rule.png" alt="规则">
      </div>
      <div class="scene-item item-7" @click="handlePrizeList">
        <img src="../assets/images/award.png" alt="奖励">
      </div>
      <div class="scene-item item-8" @click="showMyPhoto">
        <img src="../assets/images/my-photo.png" alt="我的照片">
      </div>
      <div class="scene-item item-9" @click="showPhotoSquare">
        <img src="../assets/images/photos.png" alt="照片广场">
      </div>
    </div>
  </div>

  <MyPhoto v-if="activeTab === 'my-photo'"  @go-photo-square="showPhotoSquare" v-model:show="isMyPhotoVisible" />
  <PhotoSquare v-else  @go-my-photo="showMyPhoto" v-model:show="isPhotoSquareVisible" />
</template>

<style scoped>
.home-wrapper {
  width: 100%;
  height: 92vh;
  background-image: url('../assets/images/home-bg.png');
  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;
  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: 21vw;
  bottom: 0;
  left: 0;
  animation-delay: 0s;
}

.lottery-main {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 4vw;
  height: 3.5vw;
  position: absolute;
  top: 1.5vw;
  right: 6.6vw;
  color: #fff;
}

.lottery-main .lottery-value {
  font-size: 10px;
}

.item-2 {
  width: 48vw;
  bottom: 0;
  animation-delay: 0s;
}

.join-main {
  position: absolute;
  top: 3.4vw;
  right: 2.6vw;
  width: 7vw;
  line-height: 7vw;
  text-align: left;
}

.join-main .join-value {
  margin: 0;
  color: white;
  text-stroke: 4px #ff0000;
  -webkit-text-stroke: 1px #ff0000;
  font-size: 24px;
  font-weight: 900;
}

.item-3 {
  width: 21vw;
  bottom: 0;
  right: 0;
  animation-delay: 0s;
}
.item-4 {
  width: 11vw;
  top: 1.5%;
  right: 1.5%;
  animation-delay: 0s;
}
.item-5 {
  width: 11vw;
  top: 1.5%;
  right: 1.5%;
  animation-delay: 0s;
}
.item-6 {
  width: 14.5vw;
  top: 8%;
  right: 0;
  animation-delay: 0s;
}
.item-7 {
  width: 14.5vw;
  top: 13.5%;
  right: 0;
  animation-delay: 0s;
}
.item-8 {
  width: 11vw;
  bottom: 32%;
  right: 1%;
  animation-delay: 0s;
}
.item-9 {
  width: 11vw;
  bottom: 23%;
  right: 1%;
  animation-delay: 0s;
}

@keyframes loginloading {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}
</style>