<script setup>
import {onMounted, onUnmounted, ref} from "vue";
import {onShareAppMessage, onShareTimeline, onShow} from "@dcloudio/uni-app";
import Container from "@/components/Container.vue";
import AppFooter from "@/components/AppFooter.vue";
import UserHeader from "@/components/UserHeader.vue";
import Signin from "@/components/Signin.vue";
import BubbleTip from "@/components/BubbleTip.vue";
import OtaModal from "@/components/OtaModal.vue";

import {
  checkUserBindAPI,
  getAppConfig,
  getDeviceBatteryAPI,
  getHardwareBoxTaskStatusAPI,
  getHardwareBoxVersionAPI,
  getHomeData,
  getMyDevicesAPI,
  getScoreRankList,
  sendHardwareBoxUpdateAPI,
  silentLoginAPI,
} from "@/apis";
import {topThreeColors} from "@/constants";

import useStore from "@/store";
import {storeToRefs} from "pinia";

const store = useStore();
const {
  updateConfig,
  updateUser,
  updateDevice,
  clearDevice,
  getLvlName,
  getLvlNameByScore,
  updateOnline,
} = store;
const {user, device, online, game} = storeToRefs(store);

const showModal = ref(false);
const showGuide = ref(false);
const scoreRankList = ref([]);

// OTA 相关
const otaVisible = ref(false);
const otaState = ref("new_version");
const otaProgress = ref(0);
const otaInfo = ref({
  versionNumber: "",
  versionInfo: "",
  resourceUrl: "",
  forceUpdate: false,
});
const isStartingOta = ref(false);
let otaProgressTimer = null;
let otaStatusTimer = null;
let otaTimeoutTimer = null;

// 清理首页 OTA 更新定时器，避免弹窗关闭或页面卸载后继续轮询。
const clearOtaUpdateTimers = () => {
  clearInterval(otaProgressTimer);
  clearTimeout(otaStatusTimer);
  clearTimeout(otaTimeoutTimer);
  otaProgressTimer = null;
  otaStatusTimer = null;
  otaTimeoutTimer = null;
};

// 启动首页 OTA 本地进度动画，最终成功失败以后端任务状态为准。
const startOtaProgressAnimation = () => {
  clearInterval(otaProgressTimer);
  otaProgressTimer = setInterval(() => {
    if (otaProgress.value >= 90) {
      clearInterval(otaProgressTimer);
      return;
    }
    const increment = Math.max(0.5, 2 - otaProgress.value / 60);
    otaProgress.value = Math.min(90, otaProgress.value + increment);
  }, 500);
};

// 获取并保存后端返回的 OTA 版本信息，供弹窗展示和更新接口使用。
const applyOtaVersionInfo = (versionInfo) => {
  otaInfo.value = {
    versionNumber: versionInfo?.versionNumber || "",
    versionInfo: versionInfo?.versionInfo || "",
    resourceUrl: versionInfo?.resourceUrl || "",
    forceUpdate: Number(versionInfo?.forceUpdate) === 1,
  };
};

// 检查当前设备盒子是否存在可升级版本。
const checkOtaUpdate = async () => {
  let versionInfo;
  try {
    versionInfo = await getHardwareBoxVersionAPI();
  } catch (err) {
    return;
  }
  if (!versionInfo?.needUpdate) return;
  applyOtaVersionInfo(versionInfo);

  const dismissedAt = uni.getStorageSync("ota_dismissed_at");
  const now = Date.now();
  if (!otaInfo.value.forceUpdate && dismissedAt && now - dismissedAt < 24 * 60 * 60 * 1000) return;
  otaState.value = "new_version";
  otaVisible.value = true;
};

// 拼接 OTA WiFi 页参数，让未连 WiFi 的设备继续使用同一份版本信息。
const getOtaWifiUrl = () => {
  const { versionNumber, resourceUrl } = otaInfo.value;
  const query = [
    `versionNumber=${encodeURIComponent(versionNumber)}`,
    `resourceUrl=${encodeURIComponent(resourceUrl)}`,
  ].join("&");
  return `/pages/ota-wifi?${query}`;
};

// 处理 OTA 弹窗暂不更新，强制更新时不允许关闭。
const handleOtaDismiss = () => {
  if (otaInfo.value.forceUpdate) return;
  uni.setStorageSync("ota_dismissed_at", Date.now());
  otaVisible.value = false;
};

// 将首页 OTA 直连更新流程标记为失败。
const failHomeOtaUpdate = () => {
  clearOtaUpdateTimers();
  isStartingOta.value = false;
  otaState.value = "update_failure";
  otaVisible.value = true;
};

// 将首页 OTA 直连更新流程标记为成功。
const completeHomeOtaUpdate = () => {
  clearOtaUpdateTimers();
  isStartingOta.value = false;
  otaProgress.value = 100;
  setTimeout(() => {
    otaState.value = "update_success";
    otaVisible.value = true;
  }, 300);
};

// 轮询首页直接发起的 OTA 更新任务状态。
const pollHomeOtaTaskStatus = (taskId) => {
  clearTimeout(otaStatusTimer);
  otaStatusTimer = setTimeout(async () => {
    try {
      const taskStatus = await getHardwareBoxTaskStatusAPI(taskId);
      const status = Number(taskStatus?.status);
      if (status === 2) {
        completeHomeOtaUpdate();
        return;
      }
      if (status === 3) {
        failHomeOtaUpdate();
        return;
      }
      if (status === 0 || status === 1) {
        pollHomeOtaTaskStatus(taskId);
        return;
      }
      failHomeOtaUpdate();
    } catch (err) {
      failHomeOtaUpdate();
    }
  }, 3000);
};

// 设备盒子已连 WiFi 时，从首页直接传空 WiFi 信息发起 OTA 更新。
const startHomeOtaUpdate = async () => {
  otaState.value = "update_progress";
  otaVisible.value = true;
  otaProgress.value = 0;
  startOtaProgressAnimation();
  otaTimeoutTimer = setTimeout(() => {
    if (otaState.value === "update_progress") {
      failHomeOtaUpdate();
    }
  }, 5 * 60 * 1000);

  try {
    const updateResult = await sendHardwareBoxUpdateAPI({
      versionNumber: otaInfo.value.versionNumber,
      wifiSsid: "",
      wifiPassword: "",
      resourceUrl: otaInfo.value.resourceUrl,
    });
    if (!updateResult?.taskId) {
      failHomeOtaUpdate();
      return;
    }
    pollHomeOtaTaskStatus(updateResult.taskId);
  } catch (err) {
    failHomeOtaUpdate();
  }
};

// 点击立即更新时先判断设备是否在线并已通过 WiFi 联网，已联网则首页直接更新，否则跳转 WiFi 页面。
const handleOtaUpdate = async () => {
  if (isStartingOta.value) return;
  isStartingOta.value = true;
  let deviceStatus;
  try {
    deviceStatus = await getDeviceBatteryAPI();
  } catch (err) {
    isStartingOta.value = false;
    uni.showToast({
      title: "获取设备状态失败，请重试",
      icon: "none",
    });
    return;
  }

  if (deviceStatus?.online !== true) {
    isStartingOta.value = false;
    uni.showToast({
      title: "请先开启智能弓",
      icon: "none",
    });
    return;
  }

  if (String(deviceStatus?.netType || "").toLowerCase() === "wifi") {
    startHomeOtaUpdate();
    return;
  }

  isStartingOta.value = false;
  otaVisible.value = false;
  uni.navigateTo({ url: getOtaWifiUrl() });
};

// 处理 OTA 更新成功后的完成按钮，关闭结果弹窗。
const handleOtaDone = () => {
  otaVisible.value = false;
};

// 处理 OTA 更新失败后的重试按钮，重新走立即更新判断流程。
const handleOtaRetry = () => {
  handleOtaUpdate();
};

// 提取积分榜接口返回的榜单数组，兼容数组和对象两种返回格式。
const getScoreRankData = (result) => {
  if (Array.isArray(result)) return result;
  if (Array.isArray(result?.list)) return result.list;
  if (Array.isArray(result?.items)) return result.items;
  return [];
};

const toPage = async (path) => {
  if (!user.value.id) {
    showModal.value = true;
    return;
  }
  uni.navigateTo({url: path});
};

const toRankListPage = () => {
  uni.navigateTo({
    url: "/pages/rank-list",
  });
};

onShow(async (options) => {
  const env = uni.getAccountInfoSync().miniProgram.envVersion;
  const token = uni.getStorageSync(`${env}_token`);

  // 检查是否从 OTA 更新页面返回
  if (options && options.updateResult) {
    otaState.value = options.updateResult;
    otaVisible.value = true;
  } else if (token || user.value.id) {
    await checkOtaUpdate();
  }

  if (!user.value.id && !token) {
    // showModal.value = true;
    // try {
    //   const wxResult = await uni.login({provider: "weixin"});
    //   const bindResult = await checkUserBindAPI(wxResult.code);
    //   if (bindResult.binded) {
    //     const newResult = await uni.login({provider: "weixin"});
    //     const silentResult = await silentLoginAPI(newResult.code);
    //     if (silentResult.user) updateUser(silentResult.user);
    //     const devices = await getMyDevicesAPI();
    //     if (devices.bindings && devices.bindings.length) {
    //       updateDevice(
    //           devices.bindings[0].deviceId,
    //           devices.bindings[0].deviceName
    //       );
    //       const data = await getDeviceBatteryAPI();
    //       updateOnline(data.online);
    //     }
    //   } else {
    //     showModal.value = true;
    //   }
    // } catch (e) {
    //   console.log("检查绑定状态失败", e);
    // }
  }

  const promises = [getScoreRankList(undefined, 1, 10)];
  if (token || user.value.id) {
    promises.push(getHomeData());
  }

  const [rankList, homeData] = await Promise.all(promises);

  console.log("积分榜数据", rankList);
  scoreRankList.value = getScoreRankData(rankList).slice(0, 10);

  if (homeData) {
    console.log("首页数据：", homeData);
    if (homeData.user) {
      updateUser(homeData.user);
      if ("823,209,293,257".indexOf(homeData.user.id) !== -1) {
        const show = uni.getStorageSync("show-the-user");
        if (!show) {
          uni.setStorageSync("show-the-user", true);
        }
      }
      if (homeData.user.trio <= 0) {
        showGuide.value = true;
        setTimeout(() => {
          showGuide.value = false;
        }, 3000);
      }
      const devices = await getMyDevicesAPI();
      if (devices.bindings && devices.bindings.length) {
        updateDevice(
            devices.bindings[0].deviceId,
            devices.bindings[0].deviceName
        );
        const data = await getDeviceBatteryAPI();
        updateOnline(data.online);
      } else {
        clearDevice();
      }
    }
  }
});

onMounted(async () => {
  const config = await getAppConfig();
  updateConfig(config);
  console.log("全局配置：", config);
});

onUnmounted(() => {
  clearOtaUpdateTimers();
});

onShareAppMessage(() => {
  return {
    title: "智能真弓：实时捕捉+毫秒级同步，弓箭选手全球竞技！", // 分享卡片的标题
    path: "/pages/index", // 用户点击分享卡片后跳转的页面路径
    imageUrl:
        "https://static.shelingxingqiu.com/attachment/2025-09-12/dcqoz26q0268wxmzjg.png", // 分享卡片的配图，可以是本地或网络图片
  };
});
onShareTimeline(() => {
  return {
    title: "智能真弓：实时捕捉+毫秒级同步，弓箭选手全球竞技！", // 分享到朋友圈的标题
    query: "from=timeline", // 用户通过朋友圈点击后，在页面 onShow 的 options 中可以获取到的参数
    imageUrl:
        "https://static.shelingxingqiu.com/attachment/2025-09-12/dcqoz26q0268wxmzjg.png", // 分享到朋友圈的配图
  };
});
</script>

<template>
  <Container :isHome="true" :showBackToGame="true">
    <!-- OTA 升级弹窗：使用 visible 控制显隐，description 为副标题，changelog 为详细说明 -->
    <OtaModal
      :visible="otaVisible"
      :state="otaState"
      :version="otaInfo.versionNumber"
      :progress="otaProgress"
      :description="''"
      :changelog="otaInfo.versionInfo"
      :forceUpdate="otaInfo.forceUpdate"
      @update="handleOtaUpdate"
      @skip="handleOtaDismiss"
      @close="handleOtaDismiss"
      @done="handleOtaDone"
      @retry="handleOtaRetry"
    />
    <view class="container">
      <view class="top-theme">
        <!-- <image
            src="https://static.shelingxingqiu.com/attachment/2025-12-31/dfc9dxrq4xn7e6y2pp.png"
            mode="widthFix"
        /> -->
      </view>
      <UserHeader showRank :onSignin="() => (showModal = true)"/>
      <view :style="{ padding: '12px 10px' }">
        <view class="feature-grid">
          <view class="bow-card">
            <image
                v-if="online"
                src="https://static.shelingxingqiu.com/attachment/2025-08-07/dbvt1o6dvhr2rop3kn.webp"
                mode="widthFix"
                @click="() => toPage('/pages/my-device')"
            />
            <image
                v-else
                src="https://static.shelingxingqiu.com/attachment/2026-01-04/dffohwtk1gwh0xfa6h.png"
                mode="widthFix"
                @click="$clickSound(() => toPage('/pages/my-device'))"
            />
            <block v-if="user.id">
              <text v-if="!device.deviceId">绑定我的智能弓</text>
              <text v-else-if="!online">设备离线</text>
              <text v-else-if="online">设备在线</text>
            </block>
            <image
                src="https://static.shelingxingqiu.com/shootmini/static/first-try.png"
                mode="widthFix"
                @click="() => toPage('/pages/first-try')"
            />
            <BubbleTip v-if="showGuide" :location="{ top: '60%', left: '47%' }">
              <text>新人必刷！</text>
              <text>快来报到吧~</text>
            </BubbleTip>
          </view>
          <view class="play-card">
            <!-- toPage('/pages/practise') -->
            <view @click="$clickSound(() => toPage('/pages/training/index'))">
              <image src="https://static.shelingxingqiu.com/shootmini/static/my-practise.png" mode="widthFix"/>
            </view>
            <view @click="$clickSound(() => toPage('/pages/friend-battle'))">
              <image src="https://static.shelingxingqiu.com/shootmini/static/friend-battle.png" mode="widthFix"/>
            </view>
          </view>
        </view>
        <view class="ranking-section">
          <image
              src="https://static.shelingxingqiu.com/attachment/2025-09-25/dd1p9ci9v7frcrsxhj.png"
              mode="widthFix"
          />
          <button
              class="into-btn"
              @click="$clickSound(() => toPage('/pages/ranking'))"
              hover-class="none"
          ></button>
          <view class="ranking-players" @click="toRankListPage">
            <img src="https://static.shelingxingqiu.com/shootmini/static/juezhanbang.png" mode="widthFix"/>
            <view class="divide-line"></view>
            <view class="player-avatars">
              <view
                  v-for="i in 6"
                  :key="i"
                  class="player-avatar"
                  :style="{
                  zIndex: 8 - i,
                  borderColor: scoreRankList[i - 1]
                    ? topThreeColors[i - 1] || '#000'
                    : '#000',
                }"
              >
                <image v-if="i === 1" src="../static/champ1.png"/>
                <image v-if="i === 2" src="../static/champ2.png"/>
                <image v-if="i === 3" src="../static/champ3.png"/>
                <view v-if="i > 3">{{ i }}</view>
                <image
                    :src="
                    scoreRankList[i - 1]
                      ? (scoreRankList[i - 1].avatar || '../static/user-icon.png')
                      : '../static/user-icon-dark.png'
                  "
                    mode="aspectFill"
                />
              </view>
              <view class="more-players">
                <text>{{ scoreRankList.length }}</text>
              </view>
            </view>
          </view>
          <view class="my-data">
            <view @click="() => toPage('/pages/my-growth')">
              <image src="https://static.shelingxingqiu.com/shootmini/static/my-growth.png" mode="widthFix"/>
            </view>
            <view @click="() => toPage('/pages/ranking')">
              <view>
                <text>段位</text>
                <text>{{
                    user.lvlName || "暂无"
                  }}
                </text>
              </view>
              <view>
                <text>赛季平均环数</text>
                <text>{{ user.avg_ring ? user.avg_ring + "环" : "暂无" }}</text>
              </view>
              <view>
                <text>赛季胜率</text>
                <text>{{
                    user.avg_win
                        ? Number((user.avg_win * 100).toFixed(2)) + "%"
                        : "暂无"
                  }}
                </text>
              </view>
            </view>
          </view>
        </view>
      </view>
      <Signin :show="showModal" :onClose="() => (showModal = false)"/>
    </view>
    <view class="foot-space"></view>
    <AppFooter/>
  </Container>
</template>

<style scoped>
.container {
  width: 100%;
  /* height: calc(100% + 240rpx); */
}
.foot-space{
  width: 100%;
  height: 200rpx;
}
.feature-grid {
  width: 100%;
  display: flex;
  margin-bottom: 5px;
}

.feature-grid > view {
  position: relative;
  display: flex;
  flex-direction: column;
}

.bow-card {
  width: 50%;
  border-radius: 25rpx;
  overflow: hidden;
}

.feature-grid > view > image {
  width: 100%;
}

.bow-card > text {
  position: absolute;
  top: 66%;
  left: 50%;
  transform: translate(-50%, -50%);
  white-space: nowrap;
  font-size: 13px;
  color: #b3b3b3;
}

.bow-card > image:nth-child(3) {
  transform: translateY(-1px);
}

.play-card {
  width: 48%;
  margin-left: 2%;
}

.play-card > view > image {
  width: 100%;
}

.ranking-section {
  border-radius: 15px;
  padding: 15px;
  position: relative;
}

.ranking-section > image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  z-index: -1;
}

.into-btn {
  position: absolute;
  top: 40px;
  left: calc(50% - 100px);
  width: 200px;
  height: 100px;
}

.ranking-players {
  display: flex;
  align-items: center;
  padding-bottom: 20px;
  margin-top: 42%;
  border-bottom: 1rpx solid rgba(255, 255, 255, 0.2);
}

.ranking-players > image:first-child {
  width: 28%;
  transform: translateX(-10px) translateY(-8px);
}

.player-avatars {
  display: flex;
  align-items: center;
}

.divide-line {
  width: 1px;
  height: 35px;
  background-color: #80808033;
  margin-right: 8px;
}

.player-avatar,
.more-players {
  width: 82rpx;
  height: 82rpx;
  border-radius: 50%;
  margin-right: -20rpx;
  border: 1rpx solid #312f35;
  position: relative;
  box-sizing: border-box;
}

.player-avatar > image:first-child,
.player-avatar > view:first-child {
  position: absolute;
  top: -24rpx;
  left: 22rpx;
  width: 32rpx;
  height: 32rpx;
}

.player-avatar > view:first-child {
  border-radius: 50%;
  background: #777777;
  text-align: center;
  font-size: 10px;
  line-height: 18px;
  width: 18px;
  height: 18px;
  color: #fff;
}

.player-avatar > image:last-child {
  width: 100%;
  height: 100%;
  border-radius: 50%;
}

.more-players {
  background: #3c445a;
  font-size: 9px;
  line-height: 80rpx;
  text-align: center;
  z-index: 1;
}

.more-players > text {
  margin-left: 2px;
  color: #fff;
}

.my-data {
  display: flex;
  margin-top: 20px;
  justify-content: space-between;
}

.my-data > view:first-child {
  width: 28%;
}

.my-data > view:first-child > image {
  width: 100%;
  transform: translateX(-8px);
}

.my-data > view:nth-child(2) {
  width: 68%;
  font-size: 24rpx;
  color: #fff6;
  display: flex;
  justify-content: space-between;
}

.my-data > view:nth-child(2) > view:nth-child(2) {
  width: 38%;
}

.my-data > view:nth-child(2) > view {
  width: 28%;
  border-radius: 10px;
  background: linear-gradient(180deg, #303b4c 30%, #2c384a 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.my-data > view:nth-child(2) > view > text:last-child {
  color: #fff;
  line-height: 25px;
  font-weight: 500;
}

.top-theme {
  position: absolute;
  display: none;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 60px;
  z-index: -1;
}

.top-theme > image {
  width: 300rpx;
  transform: translate(-4%, -14%);
}
</style>
