<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 { getGameAPI } from "@/apis";
import useStore from "@/store";
import { storeToRefs } from "pinia";
const store = useStore();
const { user } = storeToRefs(store);
const scores = ref([]);
const currentUser = ref({});
const data = ref({});
const onSelect = (userId) => {
  const user = data.value.players.find((p) => p.playerId === userId);
  currentUser.value = user;
  if (user && user.arrowHistory) {
    scores.value = user.arrowHistory;
  }
};
onLoad(async (options) => {
  if (options.battleId) {
    const result = await getGameAPI(options.battleId);
    data.value = result;
    if (result.players && result.players[0]) {
      onSelect(result.players[0].playerId);
    }
  }
});
</script>

<template>
  <Container title="5人大乱斗 - 靶纸">
    <view class="container">
      <view class="players" v-if="data.players">
        <view
          v-for="(player, index) in data.players"
          :key="index"
          :style="{
            width: `${Math.max(100 / data.players.length, 18)}vw`,
            backgroundColor:
              player.playerId === currentUser.playerId ? '#aba57a' : '',
            color: player.playerId === currentUser.playerId ? '#000' : '#fff9',
          }"
          @click="() => onSelect(player.playerId)"
        >
          <Avatar
            :src="player.avatar"
            :size="40"
            :rank="showRank ? index + 1 : 0"
          />
          <text>{{ player.name }}</text>
        </view>
      </view>
      <view :style="{ marginTop: '10px' }">
        <BowTarget :scores="scores" :showLatestArrow="false" />
      </view>
      <view class="score-text"
        ><text :style="{ color: '#fed847' }">{{ scores.length }}</text
        >支箭，共<text :style="{ color: '#fed847' }">{{
          scores.reduce((last, next) => last + next.ring, 0)
        }}</text
        >环</view
      >
      <view class="score-row">
        <view
          v-for="(score, index) in scores"
          :key="index"
          class="score-item"
          :style="{ width: '13vw', height: '13vw' }"
        >
          {{ score.ring }}
        </view>
      </view>
    </view>
  </Container>
</template>

<style scoped>
.container {
  width: 100%;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.players {
  display: flex;
  height: 85px;
  width: 100%;
  overflow-x: auto;
  background-color: #fff3;
}
.players > view {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  padding-top: 5px;
  flex: 0 0 auto;
}
.players > view > text {
  margin-top: 5px;
  width: 80%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: center;
}
.score-text {
  width: 100%;
  color: #fff;
  text-align: center;
  font-size: 16px;
  margin-bottom: 20px;
}
.score-row {
  margin: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
}
.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: 24px;
  margin: 3px;
}
</style>
