import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'package:asciineverdie/src/shared/l10n/game_data_l10n.dart';
import 'package:asciineverdie/src/core/model/hall_of_fame.dart';
import 'package:asciineverdie/src/features/hall_of_fame/hero_detail_dialog.dart';
import 'package:asciineverdie/src/shared/retro_colors.dart';

/// 명예의 전당 엔트리 카드
class HallOfFameEntryCard extends StatelessWidget {
  const HallOfFameEntryCard({
    super.key,
    required this.entry,
    required this.rank,
    required this.onDeleteRequest,
  });

  final HallOfFameEntry entry;
  final int rank;
  final VoidCallback onDeleteRequest;

  void _showDetailDialog(BuildContext context) {
    showDialog<void>(
      context: context,
      builder: (context) => HeroDetailDialog(entry: entry),
    );
  }

  @override
  Widget build(BuildContext context) {
    final rankColor = _getRankColor(context, rank);
    final rankIcon = _getRankIcon(rank);
    final borderColor = RetroColors.borderOf(context);
    final panelBg = RetroColors.panelBgOf(context);

    return Container(
      margin: const EdgeInsets.symmetric(vertical: 4),
      decoration: BoxDecoration(
        color: panelBg,
        border: Border.all(color: borderColor, width: 1),
      ),
      child: Material(
        color: Colors.transparent,
        child: InkWell(
          onTap: () => _showDetailDialog(context),
          child: Padding(
            padding: const EdgeInsets.all(10),
            child: Row(
              children: [
                // 순위
                Container(
                  width: 32,
                  height: 32,
                  decoration: BoxDecoration(
                    color: rankColor.withValues(alpha: 0.2),
                    border: Border.all(color: rankColor, width: 2),
                  ),
                  child: Center(
                    child: rankIcon != null
                        ? Icon(rankIcon, color: rankColor, size: 18)
                        : Text(
                            '$rank',
                            style: TextStyle(
                              fontFamily: 'PressStart2P',
                              color: rankColor,
                              fontSize: 13,
                            ),
                          ),
                  ),
                ),
                const SizedBox(width: 10),
                // 캐릭터 정보
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      // 이름 + 레벨
                      Row(
                        children: [
                          Flexible(
                            child: Text(
                              entry.characterName,
                              style: TextStyle(
                                fontFamily: 'PressStart2P',
                                fontSize: 13,
                                color: RetroColors.goldOf(context),
                              ),
                              overflow: TextOverflow.ellipsis,
                            ),
                          ),
                          const SizedBox(width: 6),
                          Container(
                            padding: const EdgeInsets.symmetric(
                              horizontal: 6,
                              vertical: 2,
                            ),
                            decoration: BoxDecoration(
                              color: RetroColors.mpOf(
                                context,
                              ).withValues(alpha: 0.2),
                              border: Border.all(
                                color: RetroColors.mpOf(context),
                                width: 1,
                              ),
                            ),
                            child: Text(
                              'Lv.${entry.level}',
                              style: TextStyle(
                                fontFamily: 'PressStart2P',
                                color: RetroColors.mpOf(context),
                                fontSize: 11,
                              ),
                            ),
                          ),
                        ],
                      ),
                      const SizedBox(height: 4),
                      // 종족/클래스
                      Text(
                        '${GameDataL10n.getRaceName(context, entry.race)} '
                        '${GameDataL10n.getKlassName(context, entry.klass)}',
                        style: TextStyle(
                          fontFamily: 'PressStart2P',
                          fontSize: 11,
                          color: RetroColors.textSecondaryOf(context),
                        ),
                      ),
                      const SizedBox(height: 6),
                      // 통계
                      Row(
                        children: [
                          _buildStatChip(
                            context,
                            Icons.timer,
                            entry.formattedPlayTime,
                            RetroColors.expOf(context),
                          ),
                          const SizedBox(width: 6),
                          _buildStatChip(
                            context,
                            Icons.heart_broken,
                            '${entry.totalDeaths}',
                            RetroColors.hpOf(context),
                          ),
                          const SizedBox(width: 6),
                          _buildStatChip(
                            context,
                            Icons.check_circle,
                            '${entry.questsCompleted}Q',
                            RetroColors.warningOf(context),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
                // 클리어 날짜
                Column(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    Icon(
                      Icons.calendar_today,
                      size: 12,
                      color: RetroColors.textMutedOf(context),
                    ),
                    const SizedBox(height: 4),
                    Text(
                      entry.formattedClearedDate,
                      style: TextStyle(
                        fontFamily: 'PressStart2P',
                        fontSize: 11,
                        color: RetroColors.textMutedOf(context),
                      ),
                    ),
                  ],
                ),
                // 삭제 버튼 (디버그 모드 전용)
                if (kDebugMode) ...[
                  const SizedBox(width: 8),
                  Material(
                    color: Colors.transparent,
                    child: InkWell(
                      onTap: () {
                        // 이벤트 전파 중지 (카드 클릭 방지)
                        onDeleteRequest();
                      },
                      child: Container(
                        padding: const EdgeInsets.all(4),
                        decoration: BoxDecoration(
                          color: RetroColors.hpOf(
                            context,
                          ).withValues(alpha: 0.2),
                          border: Border.all(
                            color: RetroColors.hpOf(context),
                            width: 1,
                          ),
                        ),
                        child: Icon(
                          Icons.delete_outline,
                          size: 16,
                          color: RetroColors.hpOf(context),
                        ),
                      ),
                    ),
                  ),
                ],
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildStatChip(
    BuildContext context,
    IconData icon,
    String value,
    Color color,
  ) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        Icon(icon, size: 10, color: color),
        const SizedBox(width: 2),
        Text(
          value,
          style: TextStyle(
            fontFamily: 'PressStart2P',
            fontSize: 11,
            color: color,
          ),
        ),
      ],
    );
  }

  Color _getRankColor(BuildContext context, int rank) {
    switch (rank) {
      case 1:
        return RetroColors.goldOf(context);
      case 2:
        return Colors.grey.shade400;
      case 3:
        return Colors.brown.shade400;
      default:
        return RetroColors.mpOf(context);
    }
  }

  IconData? _getRankIcon(int rank) {
    switch (rank) {
      case 1:
        return Icons.emoji_events;
      case 2:
        return Icons.workspace_premium;
      case 3:
        return Icons.military_tech;
      default:
        return null;
    }
  }
}
