<script setup>
import { ref, onMounted } from "vue";
import { onShow } from "@dcloudio/uni-app";
import AppBackground from "@/components/AppBackground.vue";
import Header from "@/components/Header.vue";
import ScreenHint from "@/components/ScreenHint.vue";
import BackToGame from "@/components/BackToGame.vue";
import { getCurrentGameAPI, laserAimAPI } from "@/apis";
import { debounce } from "@/util";
const props = defineProps({
  title: {
    type: String,
    default: "",
  },
  bgType: {
    type: Number,
    default: 0,
  },
  onBack: {
    type: Function,
    default: null,
  },
  overflow: {
    type: String,
    default: "auto",
  },
  isHome: {
    type: Boolean,
    default: false,
  },
  showBackToGame: {
    type: Boolean,
    default: false,
  },
  bgColor: {
    type: String,
    default: "#050b19",
  },
  whiteBackArrow: {
    type: Boolean,
    default: true,
  },
});
const showHint = ref(false);
const hintType = ref(0);
const capsuleHeight = ref(0);
const isLoading = ref(false);

const showGlobalHint = (type) => {
  hintType.value = type;
  showHint.value = true;
};

const hideGlobalHint = () => {
  showHint.value = false;
};

onMounted(() => {
  const menuBtnInfo = uni.getMenuButtonBoundingClientRect();
  capsuleHeight.value = menuBtnInfo.top - 9;
});

onShow(() => {
  uni.$showHint = showGlobalHint;
  uni.$hideHint = hideGlobalHint;
  showHint.value = false;
});

const backToGame = debounce(async () => {
  if (isLoading.value) return; // 防止重复点击

  try {
    isLoading.value = true;

    // 设置请求超时
    const timeoutPromise = new Promise((_, reject) => {
      setTimeout(() => reject(new Error("请求超时")), 10000); // 10秒超时
    });

    const result = await Promise.race([getCurrentGameAPI(), timeoutPromise]);

    // 处理返回结果
    if (result && result.gameId) {
      // 跳转到游戏页面
      uni.navigateTo({
        url: `/pages/battle-room?gameId=${result.gameId}`,
      });
    } else {
      uni.showToast({
        title: "没有进行中的对局",
        icon: "none",
      });
    }

    showHint.value = false;
  } catch (error) {
    console.error("获取当前游戏失败:", error);
    uni.showToast({
      title: error.message || "网络请求失败，请重试",
      icon: "none",
    });
  } finally {
    isLoading.value = false;
  }
});

const goBack = () => {
  uni.navigateBack();
};

const goCalibration = async () => {
  await laserAimAPI();
  uni.navigateTo({
    url: "/pages/calibration",
  });
};
</script>

<template>
  <view :style="{ paddingTop: capsuleHeight + 'px' }">
    <AppBackground :type="bgType" :bgColor="bgColor" />
    <Header
      v-if="!isHome"
      :title="title"
      :onBack="onBack"
      :whiteBackArrow="whiteBackArrow"
    />
    <BackToGame v-if="showBackToGame" />
    <view
      class="content"
      :style="{
        height: `calc(100vh - ${capsuleHeight + (isHome ? 0 : 50)}px)`,
        overflow,
      }"
    >
      <slot></slot>
    </view>
    <ScreenHint :show="showHint">
      <view v-if="hintType === 1" class="tip-content">
        <text>完成进行中的对局才能开启新的。</text>
        <text>您有正在进行中的对局，是否进入?</text>
        <view>
          <button hover-class="none" @click="() => (showHint = false)">
            不进入
          </button>
          <button hover-class="none" @click="backToGame" :disabled="isLoading">
            {{ isLoading ? "加载中..." : "进入" }}
          </button>
        </view>
      </view>
      <view v-if="hintType === 2" class="tip-content">
        <text>离开比赛可能会导致比赛失败，</text>
        <text>确认离开吗？</text>
        <view>
          <button hover-class="none" @click="goBack">离开比赛</button>
          <button hover-class="none" @click="() => (showHint = false)">
            继续比赛
          </button>
        </view>
      </view>
      <view v-if="hintType === 3" class="tip-content">
        <text>今天不玩了吗？</text>
        <view>
          <button hover-class="none" @click="() => (showHint = false)">
            取消
          </button>
          <button hover-class="none" @click="goBack">确认</button>
        </view>
      </view>
      <view v-if="hintType === 4" class="tip-content">
        <text>完成智能弓校准，即可解锁全部功能</text>
        <view>
          <button hover-class="none" @click="() => (showHint = false)">
            取消
          </button>
          <button hover-class="none" @click="goCalibration">去校准</button>
        </view>
      </view>
    </ScreenHint>
  </view>
</template>

<style scoped>
.content {
  width: 100vw;
  height: 100vh;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  overflow-x: hidden;
}
.tip-content {
  flex-direction: column;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  width: 100%;
  font-size: 14px;
}
.tip-content > text {
  text-align: center;
}
.tip-content > view {
  display: flex;
  align-items: center;
  justify-content: space-around;
  margin-top: 50rpx;
  width: 100%;
}
.tip-content > view > button {
  padding: 12px;
  border-radius: 20px;
  background-color: #fff6;
  color: #fff;
  width: 45%;
  font-size: 16px;
}
.tip-content > view > button:last-child {
  background-color: #fed847;
  color: #000;
}

.tip-content > view > button:disabled {
  background-color: #ccc;
  color: #666;
  opacity: 0.6;
}
</style>
