<script setup>
import { ref } from "vue";
import { onLoad } from "@dcloudio/uni-app";
import Container from "@/components/Container.vue";
import BowTarget from "@/components/BowTarget.vue";
import Avatar from "@/components/Avatar.vue";
import { roundsName } from "@/constants";
import { getGameAPI } from "@/apis";
const selected = ref(0);
const redScores = ref([]);
const blueScores = ref([]);
const tabs = ref(["所有轮次"]);
const players = ref([]);
const allRoundsScore = ref({});
const data = ref({
  goldenRounds: [],
});
onLoad(async (options) => {
  if (options.battleId) {
    const result = await getGameAPI(
      options.battleId || "BATTLE-1756453741433684760-512"
    );
    data.value = result;
    Object.values(result.bluePlayers).forEach((p, index) => {
      players.value.push(p);
      if (
        Object.values(result.redPlayers) &&
        Object.values(result.redPlayers)[index]
      ) {
        players.value.push(Object.values(result.redPlayers)[index]);
      }
    });
    if (result.goldenRounds) {
      result.goldenRounds.forEach(() => {
        tabs.value.push("决金箭");
      });
    }
    Object.keys(result.roundsData).forEach((key, index) => {
      if (
        index <
        Object.keys(result.roundsData).length - result.goldenRounds.length
      ) {
        tabs.value.push(`第${roundsName[key]}轮`);
      }
    });
    onClickTab(0);
  }
});
const onClickTab = (index) => {
  selected.value = index;
  redScores.value = [];
  blueScores.value = [];
  const { bluePlayers, redPlayers, roundsData, goldenRounds } = data.value;
  let maxArrowLength = 0;
  if (index === 0) {
    Object.keys(bluePlayers).forEach((p) => {
      allRoundsScore.value[p] = [];
      Object.values(roundsData).forEach((round) => {
        if (!round[p]) return;
        allRoundsScore.value[p].push(
          round[p].reduce((last, next) => last + next.ring, 0)
        );
        round[p].forEach((arrow) => {
          blueScores.value.push(arrow);
        });
      });
    });
    Object.keys(redPlayers).forEach((p) => {
      allRoundsScore.value[p] = [];
      Object.values(roundsData).forEach((round) => {
        if (!round[p]) return;
        allRoundsScore.value[p].push(
          round[p].reduce((last, next) => last + next.ring, 0)
        );
        round[p].forEach((arrow) => {
          redScores.value.push(arrow);
        });
      });
    });
  } else if (index <= goldenRounds.length) {
    const dataIndex =
      Object.keys(roundsData).length - goldenRounds.length + index;
    Object.keys(bluePlayers).forEach((p) => {
      if (!roundsData[dataIndex][p]) return;
      roundsData[dataIndex][p].forEach((arrow) => {
        blueScores.value.push(arrow);
      });
    });
    Object.keys(redPlayers).forEach((p) => {
      if (!roundsData[dataIndex][p]) return;
      roundsData[dataIndex][p].forEach((arrow) => {
        redScores.value.push(arrow);
      });
    });
  } else {
    Object.keys(bluePlayers).forEach((p) => {
      roundsData[index - goldenRounds.length][p].forEach((arrow) => {
        blueScores.value.push(arrow);
      });
    });
    Object.keys(redPlayers).forEach((p) => {
      roundsData[index - goldenRounds.length][p].forEach((arrow) => {
        redScores.value.push(arrow);
      });
    });
  }
};
</script>

<template>
  <Container title="靶纸">
    <view class="container">
      <view>
        <view
          v-for="(tab, index) in tabs"
          :key="index"
          @click="() => onClickTab(index)"
          :class="selected === index ? 'selected-tab' : ''"
        >
          {{ tab }}
        </view>
      </view>
      <view :style="{ margin: '20px 0' }">
        <BowTarget :scores="redScores" :blueScores="blueScores" mode="team" />
      </view>
      <view class="score-container">
        <view
          class="score-row"
          v-for="(player, index) in players"
          :key="index"
          :style="{
            justifyContent: index % 2 === 0 ? 'flex-end' : 'flex-start',
          }"
        >
          <Avatar
            :src="player.avatar"
            :borderColor="
              data.bluePlayers[player.playerId] ? '#64BAFF' : '#FF6767'
            "
            :size="36"
          />
          <view>
            <view
              v-if="selected === 0"
              v-for="(ring, index) in allRoundsScore[player.playerId]"
              :key="index"
              class="score-item"
            >
              {{ ring }}
            </view>
            <view
              v-if="
                selected > 0 &&
                selected >= data.goldenRounds.length &&
                selected <= data.goldenRounds.length
              "
              v-for="(score, index) in data.roundsData[
                Object.keys(data.roundsData).length -
                  data.goldenRounds.length +
                  selected
              ][player.playerId]"
              :key="index"
              class="score-item"
            >
              {{ score.ring }}
            </view>
            <view
              v-if="selected > data.goldenRounds.length"
              v-for="(score, index) in data.roundsData[
                selected - data.goldenRounds.length
              ][player.playerId]"
              :key="index"
              class="score-item"
            >
              {{ score.ring }}
            </view>
          </view>
        </view>
      </view>
    </view>
  </Container>
</template>

<style scoped>
.container {
  width: 100%;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.container > view:nth-child(1) {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  width: calc(100% - 20px);
  color: #fff9;
  padding: 10px;
  overflow-x: auto;
}
.container > view:nth-child(1)::-webkit-scrollbar {
  width: 0;
  height: 0;
  color: transparent;
}
.container > view:nth-child(1) > view {
  border: 1px solid #fff9;
  border-radius: 20px;
  padding: 7px 10px;
  margin: 0 5px;
  font-size: 14px;
  flex: 0 0 auto;
}
.selected-tab {
  background-color: #fed847;
  border-color: #fed847 !important;
  color: #000;
}
.score-row {
  display: flex;
  align-items: flex-start;
  margin-bottom: 5px;
  width: calc(50% - 5px);
  padding-left: 5px;
}
.score-row > view:last-child {
  margin-left: 10px;
  display: grid;
  grid-template-columns: repeat(3, auto);
  gap: 5px;
  margin-right: 5px;
  min-width: 26%;
}
.score-item {
  background-image: url("../static/score-bg.png");
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  color: #fed847;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 20px;
  width: 10vw;
  height: 10vw;
}
.score-container {
  display: flex;
  flex-wrap: wrap;
  width: 100%;
}
</style>
