import 'package:flutter/material.dart';

import 'package:asciineverdie/data/game_text_l10n.dart' as game_l10n;
import 'package:asciineverdie/l10n/app_localizations.dart';
import 'package:asciineverdie/src/core/engine/item_service.dart';
import 'package:asciineverdie/src/core/model/arena_match.dart';
import 'package:asciineverdie/src/core/model/equipment_item.dart';
import 'package:asciineverdie/src/core/model/equipment_slot.dart';
import 'package:asciineverdie/src/core/model/item_stats.dart';
import 'package:asciineverdie/src/shared/retro_colors.dart';

/// 아레나 결과 다이얼로그
///
/// 전투 승패 및 장비 교환 결과 표시
class ArenaResultDialog extends StatelessWidget {
  const ArenaResultDialog({
    super.key,
    required this.result,
    required this.onClose,
  });

  /// 대전 결과
  final ArenaMatchResult result;

  /// 닫기 콜백
  final VoidCallback onClose;

  @override
  Widget build(BuildContext context) {
    final isVictory = result.isVictory;
    final resultColor = isVictory ? Colors.amber : Colors.red;
    // 승패에 따라 교환 슬롯 결정
    final slot = isVictory
        ? result.match.challengerBettingSlot
        : result.match.opponentBettingSlot;

    return AlertDialog(
      backgroundColor: RetroColors.panelBgOf(context),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12),
        side: BorderSide(color: resultColor, width: 2),
      ),
      title: _buildTitle(context, isVictory, resultColor),
      content: ConstrainedBox(
        constraints: const BoxConstraints(maxWidth: 350),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            // 전투 정보
            _buildBattleInfo(context),
            const SizedBox(height: 16),
            const Divider(),
            const SizedBox(height: 16),
            // 장비 교환 결과
            _buildExchangeResult(context, slot),
          ],
        ),
      ),
      actions: [
        FilledButton(
          onPressed: onClose,
          style: FilledButton.styleFrom(backgroundColor: resultColor),
          child: Text(
            game_l10n.buttonConfirm,
            style: const TextStyle(fontFamily: 'PressStart2P', fontSize: 13),
          ),
        ),
      ],
    );
  }

  Widget _buildTitle(BuildContext context, bool isVictory, Color color) {
    final l10n = L10n.of(context);
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Icon(
          isVictory ? Icons.emoji_events : Icons.sentiment_very_dissatisfied,
          color: color,
          size: 28,
        ),
        const SizedBox(width: 8),
        Text(
          isVictory ? l10n.arenaVictory : l10n.arenaDefeat,
          style: TextStyle(
            fontFamily: 'PressStart2P',
            fontSize: 15,
            color: color,
          ),
        ),
        const SizedBox(width: 8),
        Icon(
          isVictory ? Icons.emoji_events : Icons.sentiment_very_dissatisfied,
          color: color,
          size: 28,
        ),
      ],
    );
  }

  Widget _buildBattleInfo(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: [
        // 도전자 정보
        _buildFighterInfo(
          context,
          result.match.challenger.characterName,
          result.isVictory,
        ),
        // VS
        Text(
          'VS',
          style: TextStyle(
            fontFamily: 'PressStart2P',
            fontSize: 13,
            color: RetroColors.textMutedOf(context),
          ),
        ),
        // 상대 정보
        _buildFighterInfo(
          context,
          result.match.opponent.characterName,
          !result.isVictory,
        ),
      ],
    );
  }

  Widget _buildFighterInfo(BuildContext context, String name, bool isWinner) {
    return Column(
      children: [
        Icon(
          isWinner ? Icons.emoji_events : Icons.close,
          color: isWinner ? Colors.amber : Colors.grey,
          size: 24,
        ),
        const SizedBox(height: 4),
        Text(
          name,
          style: TextStyle(
            fontFamily: 'PressStart2P',
            fontSize: 11,
            color: isWinner
                ? RetroColors.goldOf(context)
                : RetroColors.textMutedOf(context),
          ),
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
        ),
        Text(
          isWinner ? L10n.of(context).arenaWinner : L10n.of(context).arenaLoser,
          style: TextStyle(
            fontFamily: 'PressStart2P',
            fontSize: 11,
            color: isWinner ? Colors.amber : Colors.grey,
          ),
        ),
      ],
    );
  }

  Widget _buildExchangeResult(BuildContext context, EquipmentSlot slot) {
    // 교환된 장비 찾기
    final challengerOldItem = _findItem(
      result.match.challenger.finalEquipment,
      slot,
    );
    final opponentOldItem = _findItem(
      result.match.opponent.finalEquipment,
      slot,
    );

    final challengerNewItem = _findItem(
      result.updatedChallenger.finalEquipment,
      slot,
    );
    final opponentNewItem = _findItem(
      result.updatedOpponent.finalEquipment,
      slot,
    );

    return Column(
      children: [
        // 교환 타이틀
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(
              Icons.swap_horiz,
              color: RetroColors.goldOf(context),
              size: 20,
            ),
            const SizedBox(width: 8),
            Text(
              L10n.of(context).arenaEquipmentExchange,
              style: TextStyle(
                fontFamily: 'PressStart2P',
                fontSize: 14,
                color: RetroColors.goldOf(context),
              ),
            ),
          ],
        ),
        const SizedBox(height: 12),
        // 슬롯 정보
        Text(
          _getSlotLabel(slot),
          style: TextStyle(
            fontFamily: 'PressStart2P',
            fontSize: 11,
            color: RetroColors.textSecondaryOf(context),
          ),
        ),
        const SizedBox(height: 12),
        // 내 캐릭터 장비 변경
        _buildExchangeRow(
          context,
          result.match.challenger.characterName,
          challengerOldItem,
          challengerNewItem,
          result.isVictory,
        ),
        const SizedBox(height: 8),
        // 상대 장비 변경
        _buildExchangeRow(
          context,
          result.match.opponent.characterName,
          opponentOldItem,
          opponentNewItem,
          !result.isVictory,
        ),
      ],
    );
  }

  Widget _buildExchangeRow(
    BuildContext context,
    String name,
    EquipmentItem? oldItem,
    EquipmentItem? newItem,
    bool isWinner,
  ) {
    final oldScore = oldItem != null
        ? ItemService.calculateEquipmentScore(oldItem)
        : 0;
    final newScore = newItem != null
        ? ItemService.calculateEquipmentScore(newItem)
        : 0;
    final scoreDiff = newScore - oldScore;
    final isGain = scoreDiff > 0;

    return Container(
      padding: const EdgeInsets.all(8),
      decoration: BoxDecoration(
        color: isWinner
            ? Colors.green.withValues(alpha: 0.1)
            : Colors.red.withValues(alpha: 0.1),
        borderRadius: BorderRadius.circular(8),
        border: Border.all(
          color: isWinner
              ? Colors.green.withValues(alpha: 0.3)
              : Colors.red.withValues(alpha: 0.3),
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 이름
          Text(
            name,
            style: TextStyle(
              fontFamily: 'PressStart2P',
              fontSize: 13,
              color: RetroColors.textPrimaryOf(context),
            ),
          ),
          const SizedBox(height: 4),
          // 장비 변경
          Row(
            children: [
              // 이전 장비
              Expanded(
                child: _buildItemChip(context, oldItem, oldScore, isOld: true),
              ),
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 4),
                child: Icon(
                  Icons.arrow_forward,
                  size: 14,
                  color: RetroColors.textMutedOf(context),
                ),
              ),
              // 새 장비
              Expanded(
                child: _buildItemChip(context, newItem, newScore, isOld: false),
              ),
            ],
          ),
          const SizedBox(height: 4),
          // 점수 변화
          Align(
            alignment: Alignment.centerRight,
            child: Text(
              '${isGain ? '+' : ''}$scoreDiff pt',
              style: TextStyle(
                fontFamily: 'PressStart2P',
                fontSize: 13,
                color: isGain ? Colors.green : Colors.red,
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildItemChip(
    BuildContext context,
    EquipmentItem? item,
    int score, {
    required bool isOld,
  }) {
    if (item == null || item.isEmpty) {
      return Text(
        '(empty)',
        style: TextStyle(
          fontFamily: 'PressStart2P',
          fontSize: 12,
          color: RetroColors.textMutedOf(context),
        ),
      );
    }

    final rarityColor = _getRarityColor(item.rarity);

    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 2),
      decoration: BoxDecoration(
        color: rarityColor.withValues(alpha: isOld ? 0.1 : 0.2),
        borderRadius: BorderRadius.circular(4),
        border: Border.all(color: rarityColor.withValues(alpha: 0.5)),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            item.name,
            style: TextStyle(
              fontFamily: 'PressStart2P',
              fontSize: 12,
              color: rarityColor,
            ),
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ),
          Text(
            '$score pt',
            style: TextStyle(
              fontFamily: 'PressStart2P',
              fontSize: 12,
              color: RetroColors.textMutedOf(context),
            ),
          ),
        ],
      ),
    );
  }

  EquipmentItem? _findItem(List<EquipmentItem>? equipment, EquipmentSlot slot) {
    if (equipment == null) return null;
    for (final item in equipment) {
      if (item.slot == slot) return item;
    }
    return null;
  }

  String _getSlotLabel(EquipmentSlot slot) {
    return switch (slot) {
      EquipmentSlot.weapon => game_l10n.slotWeapon,
      EquipmentSlot.shield => game_l10n.slotShield,
      EquipmentSlot.helm => game_l10n.slotHelm,
      EquipmentSlot.hauberk => game_l10n.slotHauberk,
      EquipmentSlot.brassairts => game_l10n.slotBrassairts,
      EquipmentSlot.vambraces => game_l10n.slotVambraces,
      EquipmentSlot.gauntlets => game_l10n.slotGauntlets,
      EquipmentSlot.gambeson => game_l10n.slotGambeson,
      EquipmentSlot.cuisses => game_l10n.slotCuisses,
      EquipmentSlot.greaves => game_l10n.slotGreaves,
      EquipmentSlot.sollerets => game_l10n.slotSollerets,
    };
  }

  Color _getRarityColor(ItemRarity rarity) {
    return switch (rarity) {
      ItemRarity.common => Colors.grey.shade600,
      ItemRarity.uncommon => Colors.green.shade600,
      ItemRarity.rare => Colors.blue.shade600,
      ItemRarity.epic => Colors.purple.shade600,
      ItemRarity.legendary => Colors.orange.shade700,
    };
  }
}

/// 아레나 결과 다이얼로그 표시
Future<void> showArenaResultDialog(
  BuildContext context, {
  required ArenaMatchResult result,
  required VoidCallback onClose,
}) {
  return showDialog(
    context: context,
    barrierDismissible: false,
    builder: (context) => ArenaResultDialog(
      result: result,
      onClose: () {
        Navigator.of(context).pop();
        onClose();
      },
    ),
  );
}
