<script setup>
import Avatar from "@/components/Avatar.vue";
import { meleeAvatarColors } from "@/constants";
defineProps({
  players: {
    type: Array,
    default: () => [],
  },
  blueTeam: {
    type: Array,
    default: () => [],
  },
  redTeam: {
    type: Array,
    default: () => [],
  },
  showRank: {
    type: Boolean,
    default: false,
  },
  winner: {
    type: Number,
    default: 2,
  },
  showHeader: {
    type: Boolean,
    default: true,
  },
});
</script>

<template>
  <view class="container" :style="{ paddingTop: showHeader ? '5px' : '0' }">
    <image
      v-if="showHeader"
      :src="`../static/battle-header${players.length ? '-melee' : ''}.png`"
      mode="widthFix"
    />
    <view
      v-if="!players.length && blueTeam.length && redTeam.length"
      class="players"
      :style="{ paddingTop: showHeader ? '15px' : '0' }"
    >
      <view>
        <view
          v-for="(player, index) in blueTeam"
          :key="index"
          :style="{ margin: blueTeam.length === 2 ? '0 12px' : '0 6px' }"
        >
          <Avatar :src="player.avatar" :rankLvl="player.rankLvl" />
          <text class="player-name">{{ player.name }}</text>
        </view>
        <image
          v-if="winner === 1"
          src="../static/winner-badge.png"
          mode="widthFix"
        />
      </view>
      <view>
        <view
          v-for="(player, index) in redTeam"
          :key="index"
          :style="{ margin: redTeam.length === 2 ? '0 12px' : '0 6px' }"
        >
          <Avatar :src="player.avatar" :rankLvl="player.rankLvl" />
          <text class="player-name">{{ player.name }}</text>
        </view>
        <image
          v-if="winner === 0"
          src="../static/winner-badge.png"
          mode="widthFix"
        />
      </view>
    </view>
    <view
      v-if="players.length"
      class="players-melee"
      :style="{ paddingTop: showHeader ? '15px' : '0' }"
    >
      <view
        v-for="(player, index) in players"
        :key="index"
        :style="{
          backgroundColor: meleeAvatarColors[index],
          width: `${Math.max(100 / players.length, 18)}vw`,
        }"
      >
        <Avatar
          :src="player.avatar"
          :rankLvl="player.rankLvl"
          :size="40"
          :rank="showRank ? index + 1 : 0"
        />
        <text class="player-name">{{ player.name }}</text>
      </view>
    </view>
  </view>
</template>

<style scoped>
.container {
  width: 100%;
  position: relative;
  margin-bottom: 10px;
}
.container > image:first-child {
  position: absolute;
  width: 100%;
  top: -5px;
  z-index: 1;
}
.players {
  display: flex;
}
.players > view {
  width: 50%;
  height: 80px;
  color: #fff9;
  font-size: 12px;
  overflow: hidden;
  position: relative;
  padding-top: 7px;
  display: flex;
  justify-content: center;
}
.players > view:first-child {
  background-color: #364469;
}
.players > view:last-child {
  background-color: #692735;
}
.players > view > image:last-child {
  position: absolute;
  width: 40px;
  top: 0;
  left: 0;
}
.players > view > view {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.players-melee {
  display: flex;
  height: 80px;
  width: 100%;
  overflow-x: auto;
}
.players-melee::-webkit-scrollbar {
  width: 0;
  height: 0;
  color: transparent;
}
.players-melee > view {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #fff9;
  font-size: 12px;
  padding-top: 7px;
  flex: 0 0 auto;
}
.player-name {
  margin-top: 5px;
  width: 80%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: center;
}
</style>
