import 'package:flutter/material.dart';

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

/// 일반 부활 버튼 (HP 50%, 아이템 희생)
class DeathResurrectButton extends StatelessWidget {
  const DeathResurrectButton({super.key, required this.onResurrect});

  final VoidCallback onResurrect;

  @override
  Widget build(BuildContext context) {
    final expColor = RetroColors.expOf(context);
    final expDark = RetroColors.expDarkOf(context);

    return GestureDetector(
      onTap: onResurrect,
      child: Container(
        width: double.infinity,
        padding: const EdgeInsets.symmetric(vertical: 12),
        decoration: BoxDecoration(
          color: expColor.withValues(alpha: 0.2),
          border: Border(
            top: BorderSide(color: expColor, width: 3),
            left: BorderSide(color: expColor, width: 3),
            bottom: BorderSide(color: expDark.withValues(alpha: 0.8), width: 3),
            right: BorderSide(color: expDark.withValues(alpha: 0.8), width: 3),
          ),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              '\u21BA',
              style: TextStyle(
                fontSize: 20,
                color: expColor,
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(width: 8),
            Text(
              l10n.deathResurrect.toUpperCase(),
              style: TextStyle(
                fontFamily: 'PressStart2P',
                fontSize: 14,
                color: expColor,
                letterSpacing: 1,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

/// 광고 부활 버튼 (HP 100% + 아이템 복구 + 10분 자동부활)
class DeathAdReviveButton extends StatelessWidget {
  const DeathAdReviveButton({
    super.key,
    required this.onAdRevive,
    required this.deathInfo,
    required this.isPaidUser,
  });

  final VoidCallback onAdRevive;
  final DeathInfo deathInfo;
  final bool isPaidUser;

  @override
  Widget build(BuildContext context) {
    final gold = RetroColors.goldOf(context);
    final goldDark = RetroColors.goldDarkOf(context);
    final muted = RetroColors.textMutedOf(context);
    final hasLostItem = deathInfo.lostItemName != null;
    final itemRarityColor = _getRarityColor(deathInfo.lostItemRarity);

    return GestureDetector(
      onTap: onAdRevive,
      child: Container(
        width: double.infinity,
        padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 12),
        decoration: BoxDecoration(
          color: gold.withValues(alpha: 0.2),
          border: Border(
            top: BorderSide(color: gold, width: 3),
            left: BorderSide(color: gold, width: 3),
            bottom: BorderSide(
              color: goldDark.withValues(alpha: 0.8),
              width: 3,
            ),
            right: BorderSide(color: goldDark.withValues(alpha: 0.8), width: 3),
          ),
        ),
        child: Column(
          children: [
            // 메인 버튼 텍스트
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text('\u2728', style: TextStyle(fontSize: 20, color: gold)),
                const SizedBox(width: 8),
                Text(
                  l10n.deathAdRevive.toUpperCase(),
                  style: TextStyle(
                    fontFamily: 'PressStart2P',
                    fontSize: 14,
                    color: gold,
                    letterSpacing: 1,
                  ),
                ),
                // 광고 뱃지 (무료 유저만)
                if (!isPaidUser) ...[
                  const SizedBox(width: 8),
                  Container(
                    padding: const EdgeInsets.symmetric(
                      horizontal: 6,
                      vertical: 2,
                    ),
                    decoration: BoxDecoration(
                      color: Colors.white.withValues(alpha: 0.2),
                      borderRadius: BorderRadius.circular(4),
                    ),
                    child: const Text(
                      '\u25B6 AD',
                      style: TextStyle(
                        fontFamily: 'PressStart2P',
                        fontSize: 10,
                        color: Colors.white,
                      ),
                    ),
                  ),
                ],
              ],
            ),
            const SizedBox(height: 8),
            // 혜택 목록
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                _BenefitRow(
                  icon: '\u2665',
                  text: l10n.deathAdReviveHp,
                  color: RetroColors.hpOf(context),
                ),
                const SizedBox(height: 4),
                if (hasLostItem) ...[
                  _BenefitRow(
                    icon: '\u{1F504}',
                    text:
                        '${l10n.deathAdReviveItem}: ${deathInfo.lostItemName}',
                    color: itemRarityColor,
                  ),
                  const SizedBox(height: 4),
                ],
                _BenefitRow(
                  icon: '\u23F1',
                  text: l10n.deathAdReviveAuto,
                  color: RetroColors.mpOf(context),
                ),
              ],
            ),
            const SizedBox(height: 6),
            if (isPaidUser)
              Text(
                l10n.deathAdRevivePaidDesc,
                style: TextStyle(
                  fontFamily: 'PressStart2P',
                  fontSize: 9,
                  color: muted,
                ),
                textAlign: TextAlign.center,
              ),
          ],
        ),
      ),
    );
  }

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

/// 혜택 항목 행
class _BenefitRow extends StatelessWidget {
  const _BenefitRow({
    required this.icon,
    required this.text,
    required this.color,
  });

  final String icon;
  final String text;
  final Color color;

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text(icon, style: TextStyle(fontSize: 14, color: color)),
        const SizedBox(width: 6),
        Flexible(
          child: Text(
            text,
            style: TextStyle(
              fontFamily: 'PressStart2P',
              fontSize: 10,
              color: color,
            ),
            overflow: TextOverflow.ellipsis,
          ),
        ),
      ],
    );
  }
}
