import 'package:flutter/material.dart';

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

/// 캐로셀 페이지 인덱스
enum CarouselPage {
  skills, // 0: 스킬
  inventory, // 1: 인벤토리
  equipment, // 2: 장비
  character, // 3: 캐릭터시트 (기본)
  combatLog, // 4: 전투로그
  quest, // 5: 퀘스트
  story, // 6: 스토리
}

/// 캐로셀 네비게이션 바 (레트로 스타일)
///
/// 7개의 페이지 버튼을 표시하고 현재 페이지를 하이라이트.
/// 버튼 탭 시 해당 페이지로 이동.
class CarouselNavBar extends StatelessWidget {
  const CarouselNavBar({
    super.key,
    required this.currentPage,
    required this.onPageSelected,
  });

  final int currentPage;
  final ValueChanged<int> onPageSelected;

  @override
  Widget build(BuildContext context) {
    // 하단 SafeArea 패딩 (Android 제스처 네비게이션 영역)
    final bottomPadding = MediaQuery.of(context).padding.bottom;
    final panelBg = RetroColors.panelBgOf(context);
    final accent = RetroColors.goldOf(context);

    return Container(
      padding: EdgeInsets.only(
        left: 2,
        right: 2,
        top: 4,
        bottom: 4 + bottomPadding,
      ),
      decoration: BoxDecoration(
        color: panelBg,
        border: Border(top: BorderSide(color: accent, width: 2)),
      ),
      child: SizedBox(
        height: 56,
        child: Row(
          children: CarouselPage.values.map((page) {
            final isSelected = page.index == currentPage;
            return Expanded(
              child: _NavButton(
                page: page,
                isSelected: isSelected,
                onTap: () => onPageSelected(page.index),
              ),
            );
          }).toList(),
        ),
      ),
    );
  }
}

/// 개별 네비게이션 버튼 (레트로 스타일)
class _NavButton extends StatelessWidget {
  const _NavButton({
    required this.page,
    required this.isSelected,
    required this.onTap,
  });

  final CarouselPage page;
  final bool isSelected;
  final VoidCallback onTap;

  @override
  Widget build(BuildContext context) {
    final (icon, label) = _getIconAndLabel(page);
    final accent = RetroColors.goldOf(context);
    final muted = RetroColors.textMutedOf(context);
    final surface = RetroColors.surfaceOf(context);

    final color = isSelected ? accent : muted;
    final bgColor = isSelected ? surface : Colors.transparent;

    return GestureDetector(
      onTap: onTap,
      child: Container(
        margin: const EdgeInsets.symmetric(horizontal: 1),
        padding: const EdgeInsets.symmetric(vertical: 4),
        decoration: BoxDecoration(
          color: bgColor,
          border: isSelected ? Border.all(color: accent, width: 1) : null,
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(icon, size: 20, color: color),
            const SizedBox(height: 2),
            Text(
              label,
              style: TextStyle(
                fontFamily: 'PressStart2P',
                fontSize: 13,
                fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
                color: color,
              ),
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
            ),
          ],
        ),
      ),
    );
  }

  /// 페이지별 아이콘과 라벨
  (IconData, String) _getIconAndLabel(CarouselPage page) {
    return switch (page) {
      CarouselPage.skills => (Icons.auto_fix_high, l10n.navSkills),
      CarouselPage.inventory => (Icons.inventory_2, l10n.navInventory),
      CarouselPage.equipment => (Icons.shield, l10n.navEquipment),
      CarouselPage.character => (Icons.person, l10n.navCharacter),
      CarouselPage.combatLog => (Icons.list_alt, l10n.navCombatLog),
      CarouselPage.story => (Icons.auto_stories, l10n.navStory),
      CarouselPage.quest => (Icons.flag, l10n.navQuest),
    };
  }
}
