<script setup lang="ts">
import { CDRAGON_BASE } from '~/utils/cdragon'
import MatchupSpectrum from './Spectrum.vue'

defineProps<{
  matchups?: Array<MatchupData>
  championId: number
}>()

function formatWinrate(winrate: number): string {
  return (winrate * 100).toFixed(1) + '%'
}

function getWinrateColor(winrate: number): string {
  if (winrate > 0.52) return '#4CAF50' // Green for strong matchups
  if (winrate < 0.48) return '#F44336' // Red for weak matchups
  return '#FFFFFF' // White for neutral matchups
}

function getChampionImageUrl(championId: number): string {
  return `${CDRAGON_BASE}plugins/rcp-be-lol-game-data/global/default/v1/champion-icons/${championId}.png`
}
</script>

<template>
  <div class="counter-section">
    <div class="counter-column">
      <h2 class="section-title">Strong Against</h2>
      <div v-if="matchups?.length" class="counter-list">
        <div
          v-for="(counter, index) in matchups.slice(0, 5)"
          :key="'strong-' + index"
          class="counter-item"
          @click="navigateTo(`/champion/${counter.championAlias.toLowerCase()}`)"
        >
          <NuxtImg
            class="champion-icon"
            :src="getChampionImageUrl(counter.championId)"
            width="40"
            height="40"
            format="webp"
          />
          <div class="counter-info">
            <span class="champion-name">{{
              counter.championName || `Champion ${counter.championId}`
            }}</span>
            <span class="winrate" :style="{ color: getWinrateColor(counter.winrate) }">
              {{ formatWinrate(counter.winrate) }} winrate
            </span>
          </div>
          <div class="game-count">{{ counter.games }} games</div>
        </div>
      </div>
      <div v-else class="no-data">
        <p>No strong counter data available</p>
      </div>
    </div>

    <div class="counter-column">
      <h2 class="section-title">Weak Against</h2>
      <div v-if="matchups?.length" class="counter-list">
        <div
          v-for="(counter, index) in matchups.slice(matchups.length - 5, matchups.length).reverse()"
          :key="'weak-' + index"
          class="counter-item"
          @click="navigateTo(`/champion/${counter.championAlias.toLowerCase()}`)"
        >
          <NuxtImg
            class="champion-icon"
            :src="getChampionImageUrl(counter.championId)"
            width="40"
            height="40"
            format="webp"
          />
          <div class="counter-info">
            <span class="champion-name">{{
              counter.championName || `Champion ${counter.championId}`
            }}</span>
            <span class="winrate" :style="{ color: getWinrateColor(counter.winrate) }">
              {{ formatWinrate(counter.winrate) }} win rate
            </span>
          </div>
          <div class="game-count">{{ counter.games }} games</div>
        </div>
      </div>
      <div v-else class="no-data">
        <p>No weak counter data available</p>
      </div>
    </div>

    <!-- Matchup Spectrum - Full range from easiest to hardest -->
    <MatchupSpectrum :matchups="matchups" :champion-id="championId" />
  </div>
</template>

<style scoped>
.counter-section {
  display: flex;
  gap: 32px;
  margin-top: 20px;
  flex-wrap: wrap;
  justify-content: center;
}

.counter-column {
  flex: 1;
  min-width: 300px;
  max-width: 400px;
}

.section-title {
  font-size: 1.8rem;
  margin-bottom: 16px;
  color: var(--color-on-surface);
  text-align: center;
  font-weight: 600;
}

.counter-list {
  /* background-color: var(--color-surface-darker); */
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.counter-item {
  padding: 12px 16px;
  margin-bottom: 8px;
  background-color: var(--color-surface);
  border-radius: 8px;
  border: 1px solid;
  border-color: var(--color-on-surface);
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  gap: 12px;
}

.counter-item:hover {
  background-color: var(--color-surface-lighter);
  transform: translateY(-2px);
}

.champion-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  flex-shrink: 0;
}

.counter-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1;
  min-width: 0;
}

.champion-name {
  font-size: 1.4rem;
  font-weight: 500;
  color: var(--color-on-surface);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.winrate {
  font-size: 1.2rem;
  font-weight: 600;
}

.game-count {
  font-size: 1.1rem;
  color: var(--color-on-surface);
  white-space: nowrap;
  margin-left: auto;
}

.no-data {
  text-align: center;
  padding: 24px;
  color: var(--color-on-surface);
  background-color: var(--color-surface-darker);
  border-radius: 12px;
}

@media (max-width: 768px) {
  .counter-section {
    flex-direction: column;
    gap: 16px;
  }

  .counter-column {
    min-width: 100%;
    max-width: 100%;
  }

  .counter-item {
    flex-direction: row;
    align-items: center;
    gap: 12px;
    padding: 8px 12px;
    margin-bottom: 6px;
  }

  .champion-icon {
    width: 28px;
    height: 28px;
  }

  .game-count {
    margin-left: 0;
    font-size: 1rem;
  }
}
</style>
