import 'package:flutter/material.dart';

import 'package:asciineverdie/data/game_text_l10n.dart' as l10n;
import 'package:asciineverdie/src/core/model/combat_event.dart';
import 'package:asciineverdie/src/shared/retro_colors.dart';

/// 사망 화면 전투 로그 위젯
class DeathCombatLog extends StatelessWidget {
  const DeathCombatLog({super.key, required this.events});

  final List<CombatEvent> events;

  @override
  Widget build(BuildContext context) {
    final gold = RetroColors.goldOf(context);
    final background = RetroColors.backgroundOf(context);
    final borderColor = RetroColors.borderOf(context);

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          children: [
            const Text('\u{1F4DC}', style: TextStyle(fontSize: 17)),
            const SizedBox(width: 6),
            Text(
              l10n.deathCombatLog.toUpperCase(),
              style: TextStyle(
                fontFamily: 'PressStart2P',
                fontSize: 14,
                color: gold,
              ),
            ),
          ],
        ),
        const SizedBox(height: 8),
        Container(
          constraints: const BoxConstraints(maxHeight: 100),
          decoration: BoxDecoration(
            color: background,
            border: Border.all(color: borderColor, width: 2),
          ),
          child: ListView.builder(
            shrinkWrap: true,
            padding: const EdgeInsets.all(6),
            itemCount: events.length,
            itemBuilder: (context, index) {
              final event = events[index];
              return _CombatEventTile(event: event);
            },
          ),
        ),
      ],
    );
  }
}

/// 개별 전투 이벤트 타일
class _CombatEventTile extends StatelessWidget {
  const _CombatEventTile({required this.event});

  final CombatEvent event;

  @override
  Widget build(BuildContext context) {
    final (asciiIcon, color, message) = _formatCombatEvent(context, event);

    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 1),
      child: Row(
        children: [
          Text(asciiIcon, style: TextStyle(fontSize: 15, color: color)),
          const SizedBox(width: 4),
          Expanded(
            child: Text(
              message,
              style: TextStyle(
                fontFamily: 'JetBrainsMono',
                fontSize: 14,
                color: color,
              ),
              overflow: TextOverflow.ellipsis,
            ),
          ),
        ],
      ),
    );
  }

  /// 전투 이벤트를 ASCII 아이콘, 색상, 메시지로 포맷
  (String, Color, String) _formatCombatEvent(
    BuildContext context,
    CombatEvent event,
  ) {
    final target = event.targetName ?? '';
    final gold = RetroColors.goldOf(context);
    final exp = RetroColors.expOf(context);
    final hp = RetroColors.hpOf(context);
    final mp = RetroColors.mpOf(context);

    return switch (event.type) {
      CombatEventType.playerAttack => (
        event.isCritical ? '\u26A1' : '\u2694',
        event.isCritical ? gold : exp,
        event.isCritical
            ? l10n.combatCritical(event.damage, target)
            : l10n.combatYouHit(target, event.damage),
      ),
      CombatEventType.monsterAttack => (
        '\u{1F480}',
        hp,
        l10n.combatMonsterHitsYou(target, event.damage),
      ),
      CombatEventType.playerEvade => (
        '\u27A4',
        RetroColors.asciiCyan,
        l10n.combatEvadedAttackFrom(target),
      ),
      CombatEventType.monsterEvade => (
        '\u27A4',
        const Color(0xFFFF9933),
        l10n.combatMonsterEvaded(target),
      ),
      CombatEventType.playerBlock => (
        '\u{1F6E1}',
        mp,
        l10n.combatBlockedAttack(target, event.damage),
      ),
      CombatEventType.playerParry => (
        '\u2694',
        const Color(0xFF00CCCC),
        l10n.combatParriedAttack(target, event.damage),
      ),
      CombatEventType.playerSkill => (
        '\u2727',
        const Color(0xFF9966FF),
        l10n.combatSkillDamage(event.skillName ?? '', event.damage),
      ),
      CombatEventType.playerHeal => (
        '\u2665',
        exp,
        l10n.combatHealedFor(event.healAmount),
      ),
      CombatEventType.playerBuff => (
        '\u2191',
        mp,
        l10n.combatBuffActivated(event.skillName ?? ''),
      ),
      CombatEventType.playerDebuff => (
        '\u2193',
        const Color(0xFFFF6633),
        l10n.combatDebuffApplied(event.skillName ?? '', target),
      ),
      CombatEventType.dotTick => (
        '\u{1F525}',
        const Color(0xFFFF6633),
        l10n.combatDotTick(event.skillName ?? '', event.damage),
      ),
      CombatEventType.playerPotion => (
        '\u{1F9EA}',
        exp,
        l10n.combatPotionUsed(event.skillName ?? '', event.healAmount, target),
      ),
      CombatEventType.potionDrop => (
        '\u{1F381}',
        gold,
        l10n.combatPotionDrop(event.skillName ?? ''),
      ),
    };
  }
}
