<template>
  <ModalTransition class="game-swiper" :show="show">
    <div class="scene-item item-1" @click="handleGoBack">
      <img src="../assets/images/new/back.webp" alt="后退">
    </div>

    <!-- 隐藏的圆形点击区域 -->
    <div 
      class="hidden-circular-area" 
      @click="handleShowLottery"
      :style="{
        top: circularArea.top,
        left: circularArea.left,
        width: circularArea.size,
        height: circularArea.size
      }"
    ></div>
    <div class="game-page" :style="gameBackgroundImg"></div>
  </ModalTransition>
</template>

<script setup>
import { ref, onMounted, onUnmounted, computed } from 'vue'
import ModalTransition from "./ModalTransition.vue"
import globalToastEvent, { ToastType } from '../globalToastEvent';
import game1 from '../assets/images/new/game1.webp';
import game2 from '../assets/images/new/game2.webp';
import game3 from '../assets/images/new/game3.webp';
import game4 from '../assets/images/new/game4.webp';
import game5 from '../assets/images/new/game5.webp';
import { globalStore } from "../globalstore.js";
import { Request } from "../libs/utils"

const props = defineProps({
  show: false,
  gameSlideId: 1
})

const emit = defineEmits(['close'])
const handleGoBack = () => {
  // globalToastEvent.emit(ToastType.SHOW_GAMEPAGE)
  emit('close')
}

const gameImgMap = {
  1: game1,
  2: game2,
  3: game3,
  4: game4,
  5: game5
}

const gameBackgroundImg = computed(() => {
  const id = Number(props.gameSlideId) || 1;
  const img = gameImgMap[id] || game1;
  return { backgroundImage: `url(${img})` };
})

const circularAreaList = {
  1: {
    top: '62vw',
    left: '55vw',
    size: '35vw'
  },
  2: {
    top: '62vw',
    left: '80vw',
    size: '35vw'
  },
  3: {
    top: '88vw',
    left: '48vw',
    size: '35vw'
  },
  4: {
    top: '96vw',
    left: '36vw',
    size: '35vw'
  },
  5: {
    top: '96vw',
    left: '40vw',
    size: '35vw'
  }
}

// 隐藏圆形点击区域的配置（可调整坐标和大小）
const circularArea = computed(() => {
  const id = Number(props.gameSlideId) || 1;
  return circularAreaList[id] || circularAreaList[1];
})

// 点击隐藏区域显示抽奖弹窗
const handleShowLottery = async () => {
  const loading = weui.loading()
  globalToastEvent.emit(ToastType.SHOW_LOTTERY);

  // 点击隐藏区域后，游戏结束，再次调用game log接口
  const logId = globalStore.current_game_log_id;
  let gameLogRes = await Request("game/log", { source: globalStore.game_id, log_id: logId }, "POST");
  if (!gameLogRes || !gameLogRes.res || (gameLogRes.res.status !== 200 && gameLogRes.res.status !== 201)) {
    emit('close')
    loading.hide()
    return
  } else {
    console.log(gameLogRes)
  }
}

// 轮播图数据
const slides = ref([
  {
    image: new URL('../assets/images/new/slider1.webp', import.meta.url).href,
    title: '轮播图1',
    link: '/home' // 点击跳转的路由
  },
  {
    image: new URL('../assets/images/new/slider1.webp', import.meta.url).href,
    title: '轮播图2',
    link: '/lottery'
  },
  {
    image: new URL('../assets/images/new/slider1.webp', import.meta.url).href,
    title: '轮播图3',
    link: '/address'
  }
])

const currentIndex = ref(0)
const startX = ref(0)
const moveX = ref(0)
const isDragging = ref(false)
const isTransitioning = ref(true)
let autoPlayTimer = null

// 触摸开始
const handleTouchStart = (e) => {
  startX.value = e.touches[0].clientX
  isDragging.value = true
  isTransitioning.value = false
  stopAutoPlay()
}

// 触摸移动
const handleTouchMove = (e) => {
  if (!isDragging.value) return
  moveX.value = e.touches[0].clientX - startX.value
}

// 触摸结束
const handleTouchEnd = () => {
  if (!isDragging.value) return
  
  isTransitioning.value = true
  const threshold = 50 // 滑动阈值
  
  if (moveX.value > threshold) {
    // 向右滑动 - 上一张
    prevSlide()
  } else if (moveX.value < -threshold) {
    // 向左滑动 - 下一张
    nextSlide()
  }
  
  isDragging.value = false
  moveX.value = 0
//   startAutoPlay()
}

// 上一张
const prevSlide = () => {
  currentIndex.value = currentIndex.value > 0 
    ? currentIndex.value - 1 
    : slides.value.length - 1
}

// 下一张
const nextSlide = () => {
  currentIndex.value = currentIndex.value < slides.value.length - 1 
    ? currentIndex.value + 1 
    : 0
}

// 跳转到指定幻灯片
const goToSlide = (index) => {
  isTransitioning.value = true
  currentIndex.value = index
  stopAutoPlay()
  startAutoPlay()
}

// 点击幻灯片处理
const handleSlideClick = (slide) => {
  
}

// 自动播放
const startAutoPlay = () => {
  autoPlayTimer = setInterval(() => {
    nextSlide()
  }, 3000) // 每3秒切换一次
}

// 停止自动播放
const stopAutoPlay = () => {
  if (autoPlayTimer) {
    clearInterval(autoPlayTimer)
    autoPlayTimer = null
  }
}

// 生命周期
onMounted(() => {
//   startAutoPlay()
})

onUnmounted(() => {
  stopAutoPlay()
})
</script>

<style scoped>
.scene-item {
  position: fixed;
  cursor: pointer;
  transition: all 0.4s ease;
  overflow: hidden;
  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: 14vw;
  height: 14vw;
  top: 8vw;
  left: 2vw;
  animation-delay: 0s;
}
.item-2 {
  width: 12vw;
  height: 12vw;
  top: 8vw;
  right: 2vw;
  animation-delay: 0s;
}

.item-3 {
  width: 46vw;
  bottom: 14vw;
  animation-delay: 0s;
}

.confirm-layout {
  display: flex;
  justify-content: center;
}

/* 隐藏的圆形点击区域 */
.hidden-circular-area {
  position: fixed;
  border-radius: 50%;
  cursor: pointer;
  transform: translate(-50%, -50%);
  /* 隐藏样式 - 完全透明且不可见 */
  background: transparent;
  opacity: 0;
  z-index: 100;
  /* 开发调试时可以取消注释下面这行来查看区域位置 */
  /* background: rgba(255, 0, 0, 0.3); */
}

.game-page {
  width: 100%;
  min-height: 100vh;
  background: #f5f5f5;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  box-sizing: border-box;
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
}

.carousel-container {
  width: 100%;
  max-width: 100vw;
  position: relative;
  padding: 0 20px;
  box-sizing: border-box;
}

.carousel-wrapper {
  width: 80vw;
  overflow: visible;
  position: relative;
  touch-action: pan-y;
}

.carousel-track {
  display: flex;
  flex-direction: row;
  width: 100%;
  padding: 20px 0;
}

.carousel-slide {
  min-width: calc(100% - 160px);
  width: calc(100% - 160px);
  margin: 0 20px;
  flex-shrink: 0;
  cursor: pointer;
  user-select: none;
  transition: all 0.3s ease;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  transform: scale(0.85);
  opacity: 0.5;
  filter: grayscale(60%) brightness(0.7);
}

.carousel-slide.active {
  transform: scale(1);
  opacity: 1;
  filter: grayscale(0%) brightness(1);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
  z-index: 2;
}

.carousel-slide img {
  width: 100%;
  height: auto;
  display: block;
  object-fit: cover;
  pointer-events: none;
}

/* 指示器 */
.carousel-indicators {
  position: absolute;
  bottom: -8vw;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 2vw;
  z-index: 10;
  padding: 2vw 4vw;
  background: rgba(0, 0, 0, 0.3);
  border-radius: 4vw;
  backdrop-filter: blur(4px);
}

.indicator {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.6);
  cursor: pointer;
  transition: all 0.3s ease;
  border: 2px solid transparent;
}

.indicator:hover {
  background: rgba(255, 255, 255, 0.8);
  transform: scale(1.2);
}

.indicator.active {
  background: #fff;
  width: 28px;
  border-radius: 5px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

/* 移动端适配 */
@media (max-width: 768px) {
  .game-page {
    padding: 10px;
  }
  
  .carousel-container {
    padding: 0 10px;
  }
  
  .carousel-slide {
    min-width: calc(100% - 12vw);
    width: calc(100% - 12vw);
    margin: 0 10px;
    border-radius: 8px;
  }
  
  .carousel-track {
    padding: 10px 0;
  }
}
</style>
