<script setup lang="ts">
import { computed } from 'vue'
import type { BattleHUDEntry } from '@engine/types'
import { useGameStore } from '@/stores/gameStore'
import { useI18n } from '@/composables/useI18n'

const props = defineProps<{
  entries: BattleHUDEntry[]
}>()

const store = useGameStore()
const { t } = useI18n()

function statValue(variable: string): number {
  return store.variable(variable)
}

function barPercent(value: number, max: number): number {
  if (!max || max <= 0) return 0
  return Math.min(100, Math.max(0, (value / max) * 100))
}

function barClass(percent: number): string {
  if (percent <= 25) return 'danger'
  if (percent <= 50) return 'warning'
  return ''
}

function statStyle(stat: { variable: string; max?: number; style?: string }): 'bar' | 'number' {
  if (stat.style === 'bar' || stat.style === 'number') return stat.style
  return stat.max !== undefined ? 'bar' : 'number'
}
</script>

<template>
  <div class="battle-hud" v-if="entries.length > 0">
    <div class="hud-entry" v-for="entry in entries" :key="entry.label">
      <img v-if="entry.portrait" :src="entry.portrait" class="hud-portrait" />
      <div class="hud-stats">
        <div class="hud-name">{{ t(entry.labelKey || entry.label) }}</div>
        <div class="hud-stat" v-for="s in entry.stats" :key="s.variable">
          <span class="stat-label">{{ t(s.labelKey || s.label) }}</span>
          <template v-if="statStyle(s) === 'number'">
            <span class="stat-value">{{ statValue(s.variable) }}</span>
          </template>
          <template v-else>
            <div class="stat-bar-bg">
              <div
                class="stat-bar-fill"
                :class="barClass(barPercent(statValue(s.variable), s.max || 1))"
                :style="{ width: barPercent(statValue(s.variable), s.max || 1) + '%' }"
              ></div>
            </div>
            <span class="stat-value">{{ statValue(s.variable) }}/{{ s.max }}</span>
          </template>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.battle-hud {
  position: absolute;
  top: 20px;
  left: 20px;
  z-index: 25;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.hud-entry {
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(12, 12, 18, 0.75);
  backdrop-filter: blur(6px);
  border: none;
  border-radius: 6px;
  padding: 10px 14px;
  min-width: 180px;
}

.hud-portrait {
  width: 48px;
  height: 48px;
  border-radius: 4px;
  object-fit: cover;
  border: 1px solid rgba(255, 255, 255, 0.15);
}

.hud-stats {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1;
}

.hud-name {
  font-size: 13px;
  color: #ddd;
  margin-bottom: 2px;
}

.hud-stat {
  display: flex;
  align-items: center;
  gap: 6px;
}

.stat-label {
  font-size: 11px;
  color: #999;
  min-width: 28px;
}

.stat-bar-bg {
  flex: 1;
  height: 8px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 4px;
  overflow: hidden;
}

.stat-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, #8b6914, #c9a84c);
  border-radius: 4px;
  transition: width 0.3s ease;
}

.stat-bar-fill.warning {
  background: linear-gradient(90deg, #8b6914, #c9a84c);
}

.stat-bar-fill.danger {
  background: linear-gradient(90deg, #6b3010, #b8541a);
}

.stat-value {
  font-size: 11px;
  color: #ddd;
  white-space: nowrap;
  min-width: 50px;
  text-align: right;
}
</style>
