<script setup>
import { ref, watch, onMounted, onUnmounted } from "vue";
import { onLoad } from "@dcloudio/uni-app";
import Container from "@/components/Container.vue";
import BowTarget from "@/components/BowTarget.vue";
import ShootProgress from "@/components/ShootProgress.vue";
import Guide from "@/components/Guide.vue";
import BattleHeader from "@/components/BattleHeader.vue";
import Timer from "@/components/Timer.vue";
import PlayerScore from "@/components/PlayerScore.vue";
import SButton from "@/components/SButton.vue";
import Avatar from "@/components/Avatar.vue";
import ScreenHint from "@/components/ScreenHint.vue";
import Matching from "@/components/Matching.vue";
import TestDistance from "@/components/TestDistance.vue";
import { matchGameAPI, readyGameAPI } from "@/apis";
import { MESSAGETYPES, getMessageTypeName } from "@/constants";
import useStore from "@/store";
import { storeToRefs } from "pinia";
const store = useStore();
const { user } = storeToRefs(store);
const gameType = ref(0);
const teamSize = ref(0);
const start = ref(false);
const countDown = ref(90);
const battleId = ref("");
const currentRound = ref(1);
const totalRounds = ref(0);
const power = ref(0);
const scores = ref([]);
const tips = ref("即将开始...");
const seq = ref(0);
const timerSeq = ref(0);
const players = ref([]);
const playersSorted = ref([]);
const playersScores = ref({});
const halfTimeTip = ref(false);
const onComplete = ref(null);

watch(
  () => [players.value, playersScores.value],
  ([n_players, n_scores]) => {
    if (n_players.length) {
      playersSorted.value = Object.keys(n_scores)
        .sort((a, b) => n_scores[b].length - n_scores[a].length)
        .map((pid) => n_players.find((p) => p.id == pid));
    }
  },
  {
    deep: true, // 添加深度监听
    immediate: true,
  }
);

onLoad(async (options) => {
  if (options.battleId) {
    const battleInfo = uni.getStorageSync(`battle-${options.battleId}`);
    // console.log("----battleInfo", battleInfo);
    if (battleInfo) {
      battleId.value = battleInfo.id;
      start.value = true;
      tips.value = "请连续射出6支箭";
      players.value = [...battleInfo.blueTeam, ...battleInfo.redTeam];
      players.value.forEach((p) => {
        playersScores.value[p.id] = p.arrows;
      });
      if (battleInfo.startTime) {
        const remain = Date.now() / 1000 - battleInfo.startTime;
        if (remain <= 90) {
          setTimeout(() => {
            uni.$emit("update-ramain", remain);
          }, 300);
        } else if (remain > 90 && remain <= 110) {
          halfTimeTip.value = true;
          countDown.value = 20;
          tips.value = "准备下半场";
          setTimeout(() => {
            uni.$emit("update-ramain", 110 - remain);
          }, 300);
        } else if (remain > 110) {
          setTimeout(() => {
            uni.$emit("update-ramain", remain - 110);
          }, 300);
        }
      }
    }
  } else {
    gameType.value = options.gameType;
    teamSize.value = options.teamSize;
    if (gameType.value && teamSize.value) {
      await matchGameAPI(true, gameType.value, teamSize.value);
    }
  }
});
async function stopMatch() {
  uni.navigateBack();
}
async function readyToGo() {
  if (battleId.value) {
    await readyGameAPI(battleId.value);
    scores.value = [];
    start.value = true;
    timerSeq.value = 0;
  }
}

async function onReceiveMessage(messages = []) {
  messages.forEach((msg) => {
    if (
      !msg.id ||
      (battleId.value && msg.id === battleId.value) ||
      msg.constructor === MESSAGETYPES.WaitForAllReady
    ) {
      console.log("收到消息：", getMessageTypeName(msg.constructor), msg);
    }
    if (msg.constructor === MESSAGETYPES.WaitForAllReady) {
      onComplete.value = () => {
        // 这里会掉多次;
        timerSeq.value += 1;
        battleId.value = msg.id;
        players.value = [
          ...msg.groupUserStatus.redTeam,
          ...msg.groupUserStatus.blueTeam,
        ];
        players.value.forEach((p) => {
          playersScores.value[p.id] = [];
        });
      };
    }
    if (msg.id !== battleId.value) return;
    if (msg.constructor === MESSAGETYPES.MeleeAllReady) {
      start.value = true;
      seq.value += 1;
      timerSeq.value = 0;
      tips.value = "请连续射出6支箭";
      scores.value = [];
      countDown.value = 90;
    }
    if (msg.constructor === MESSAGETYPES.ShootResult) {
      if (msg.userId === user.value.id) {
        scores.value.push(msg.target);
        power.value = msg.target.battery;
      }
      playersScores.value[msg.userId].push(msg.target);
    }
    if (msg.constructor === MESSAGETYPES.HalfTimeOver) {
      halfTimeTip.value = true;
      countDown.value = 20;
      tips.value = "准备下半场";
    }
    if (msg.constructor === MESSAGETYPES.MatchOver) {
      uni.redirectTo({
        url: `/pages/battle-result?battleId=${msg.id}`,
      });
    }
  });
}
const onBack = () => {
  if (battleId.value) {
    uni.$showHint(2);
  } else {
    uni.$showHint(3);
  }
};
onMounted(() => {
  uni.$on("socket-inbox", onReceiveMessage);
});
onUnmounted(() => {
  uni.$off("socket-inbox", onReceiveMessage);
  if (gameType.value && teamSize.value) {
    matchGameAPI(true, gameType.value, teamSize.value);
  }
});
</script>

<template>
  <Container
    :title="battleId ? '大乱斗排位赛' : '搜索对手...'"
    :bgType="1"
    :onBack="onBack"
  >
    <view class="container">
      <block v-if="battleId">
        <BattleHeader :players="players" />
        <TestDistance v-if="!start" :guide="false" />
        <ShootProgress
          v-if="start"
          :seq="seq"
          :start="start"
          :tips="tips"
          :total="countDown"
        />
        <view v-if="start" class="user-row">
          <Avatar :src="user.avatar" :size="35" />
          <BowPower :power="power" />
        </view>
        <BowTarget
          v-if="start"
          :showE="start"
          :currentRound="scores.length"
          :totalRound="start ? 12 : 0"
          :scores="scores"
        />
        <view :style="{ paddingBottom: '20px' }">
          <PlayerScore
            v-if="start"
            v-for="(player, index) in playersSorted"
            :key="index"
            :name="player.name"
            :avatar="player.avatar"
            :scores="playersScores[player.id] || []"
          />
        </view>
        <Timer :seq="timerSeq" :callBack="readyToGo" />
        <ScreenHint
          :show="halfTimeTip"
          mode="small"
          :onClose="() => (halfTimeTip = false)"
        >
          <view class="half-time-tip">
            <text>上半场结束，休息一下吧:）</text>
            <text>20秒后开始下半场</text>
          </view>
        </ScreenHint>
      </block>
      <block v-else>
        <Matching
          v-if="!battleId"
          :stopMatch="stopMatch"
          :onComplete="onComplete"
        />
      </block>
    </view>
    <view :style="{ marginBottom: '20px' }">
      <SButton v-if="battleId && !start" :onClick="readyToGo">准备完毕</SButton>
    </view>
  </Container>
</template>

<style scoped>
.container {
  width: 100%;
  height: 100%;
}
.half-time-tip {
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.half-time-tip > text:last-child {
  margin-top: 20px;
  color: #fff9;
}
</style>
