<script setup>
import { computed } from 'vue'
import { store } from './store.js'
import TraitPanel from './components/TraitPanel.vue'
import OpponentList from './components/OpponentList.vue'
import Shop from './components/Shop.vue'
import UnitChip from './components/UnitChip.vue'

const s = computed(() => store.state)
const kindLabel = { pvp: 'PvP', pve: 'PvE', carousel: 'Carousel' }
</script>

<template>
  <div v-if="!s" class="splash">
    <h1>TFT-Simulator</h1>
    <button class="big" @click="store.start()">Neues Spiel</button>
  </div>

  <div v-else class="layout">
    <header>
      <span class="round">Runde {{ s.round.label }} · {{ kindLabel[s.round.kind] }}</span>
      <span class="stat">❤️ {{ s.player.hp }}</span>
      <span class="stat">🪙 {{ s.player.gold }}</span>
      <span class="stat">
        Lvl {{ s.player.level }}
        <template v-if="s.player.xp_needed !== null">({{ s.player.xp }}/{{ s.player.xp_needed }})</template>
      </span>
      <span class="stat">Streak {{ s.player.streak }}</span>
      <span class="score">Boardstärke {{ s.player.score }}</span>
      <button class="fight" :disabled="s.over" @click="store.next()">
        {{ s.round.kind === 'pvp' ? 'Kampf' : 'Weiter' }} ▶
      </button>
    </header>

    <div v-if="store.error" class="error">{{ store.error }}</div>

    <div v-if="s.over" class="gameover">
      <h2>Platz {{ s.placement }}</h2>
      <button class="big" @click="store.start()">Nochmal</button>
    </div>

    <div v-if="s.augment_offer.length" class="augments">
      <h3>Augment wählen</h3>
      <div class="offer">
        <button v-for="(a, i) in s.augment_offer" :key="a.api_name" @click="store.pickAugment(i)">
          <img v-if="a.icon" :src="a.icon" :alt="a.name" />
          {{ a.name }}
        </button>
      </div>
    </div>

    <main>
      <TraitPanel :traits="s.traits" />

      <section class="center">
        <div class="zone">
          <h3>Board ({{ s.board.length }}/{{ s.player.level }})</h3>
          <div class="unitrow">
            <UnitChip
              v-for="(u, i) in s.board"
              :key="'b' + i"
              :unit="u"
              sellable
              @click="store.selectedItem !== null ? store.equip(i) : store.move('board', i)"
              @sell="store.sell('board', i)"
            />
          </div>
        </div>
        <div class="zone">
          <h3>Bank ({{ s.bench.length }}/9)</h3>
          <div class="unitrow">
            <UnitChip
              v-for="(u, i) in s.bench"
              :key="'n' + i"
              :unit="u"
              sellable
              @click="store.move('bench', i)"
              @sell="store.sell('bench', i)"
            />
          </div>
        </div>
        <div class="zone" v-if="s.items.length">
          <h3>Items {{ store.selectedItem !== null ? '— Ziel-Unit anklicken' : '' }}</h3>
          <div class="itemrow">
            <img
              v-for="(it, i) in s.items"
              :key="i"
              :src="it.icon"
              :alt="it.name"
              :title="it.name"
              :class="{ selected: store.selectedItem === i }"
              @click="store.selectedItem = store.selectedItem === i ? null : i"
            />
          </div>
        </div>
        <div class="zone log">
          <div v-for="(line, i) in [...s.log].reverse()" :key="i">{{ line }}</div>
        </div>
      </section>

      <OpponentList :opponents="s.opponents" />
    </main>

    <footer>
      <Shop />
    </footer>
  </div>
</template>

<style scoped>
.splash {
  height: 100vh;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 20px;
}
.big { font-size: 18px; padding: 12px 28px; }

.layout {
  height: 100vh;
  display: flex; flex-direction: column;
  padding: 10px; gap: 8px;
  max-width: 1400px; margin: 0 auto;
}
header {
  display: flex; align-items: center; gap: 16px;
  background: #161b29; border-radius: 8px; padding: 8px 14px;
}
.round { font-weight: 700; }
.stat { color: #c6cddd; }
.score { margin-left: auto; font-weight: 700; color: #7de3d4; }
.fight { background: #2b4a33; border-color: #4caf6e; font-weight: 700; }

.error {
  background: #3d2330; border: 1px solid #a5476a;
  border-radius: 6px; padding: 6px 12px;
}
.gameover {
  background: #161b29; border-radius: 8px; padding: 16px;
  display: flex; align-items: center; gap: 16px;
}
.augments { background: #1d2438; border-radius: 8px; padding: 10px 14px; }
.augments h3 { font-size: 12px; text-transform: uppercase; color: #8a92a8; margin-bottom: 6px; }
.offer { display: flex; gap: 8px; }
.offer button { display: flex; align-items: center; gap: 8px; flex: 1; }
.offer img { width: 28px; height: 28px; }

main {
  flex: 1; min-height: 0;
  display: grid; grid-template-columns: 220px 1fr 240px; gap: 10px;
  overflow: hidden;
}
main > * { overflow-y: auto; }

.center { display: flex; flex-direction: column; gap: 10px; }
.zone h3 { font-size: 12px; text-transform: uppercase; color: #8a92a8; margin-bottom: 5px; }
.unitrow { display: flex; flex-wrap: wrap; gap: 6px; min-height: 46px; }
.itemrow { display: flex; flex-wrap: wrap; gap: 5px; }
.itemrow img {
  width: 34px; height: 34px; border-radius: 6px;
  border: 2px solid #3a4460; cursor: pointer;
}
.itemrow img.selected { border-color: #7de3d4; }
.log { color: #7d86a0; font-size: 12px; margin-top: auto; }
</style>
