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

import 'package:asciineverdie/src/core/notification/notification_service.dart';
import 'package:asciineverdie/data/game_text_l10n.dart' as l10n;
import 'package:asciineverdie/l10n/app_localizations.dart';
import 'package:asciineverdie/src/core/animation/ascii_animation_type.dart';
import 'package:asciineverdie/src/core/model/game_state.dart';
import 'package:asciineverdie/src/features/game/pages/character_sheet_page.dart';
import 'package:asciineverdie/src/features/game/pages/combat_log_page.dart';
import 'package:asciineverdie/src/features/game/pages/equipment_page.dart';
import 'package:asciineverdie/src/features/game/pages/inventory_page.dart';
import 'package:asciineverdie/src/features/game/pages/quest_page.dart';
import 'package:asciineverdie/src/features/game/pages/skills_page.dart';
import 'package:asciineverdie/src/features/game/pages/story_page.dart';
import 'package:asciineverdie/src/features/game/widgets/carousel_nav_bar.dart';
import 'package:asciineverdie/src/features/game/widgets/combat_log.dart';
import 'package:asciineverdie/src/features/game/widgets/dialogs/retro_confirm_dialog.dart';
import 'package:asciineverdie/src/features/game/widgets/dialogs/retro_select_dialog.dart';
import 'package:asciineverdie/src/features/game/widgets/dialogs/retro_sound_dialog.dart';
import 'package:asciineverdie/src/features/game/widgets/enhanced_animation_panel.dart';
import 'package:asciineverdie/src/features/game/widgets/menu/retro_menu_widgets.dart';
import 'package:asciineverdie/src/shared/retro_colors.dart';
import 'package:asciineverdie/src/shared/widgets/retro_widgets.dart';

/// 모바일 캐로셀 레이아웃
///
/// 모바일 앱용 레이아웃:
/// - 상단: 확장 애니메이션 패널 (ASCII 애니메이션, HP/MP, 버프, 몬스터 HP)
/// - 중앙: 캐로셀 (7개 페이지: 스킬, 인벤토리, 장비, 캐릭터시트, 전투로그, 스토리, 퀘스트)
/// - 하단: 네비게이션 바 (7개 버튼)
class MobileCarouselLayout extends StatefulWidget {
  const MobileCarouselLayout({
    super.key,
    required this.state,
    required this.combatLogEntries,
    required this.speedMultiplier,
    required this.onSpeedCycle,
    required this.isPaused,
    required this.onPauseToggle,
    required this.onSave,
    required this.onExit,
    required this.notificationService,
    required this.onLanguageChange,
    required this.onDeleteSaveAndNewGame,
    this.specialAnimation,
    this.bgmVolume = 0.7,
    this.sfxVolume = 0.8,
    this.onBgmVolumeChange,
    this.onSfxVolumeChange,
    this.onShowStatistics,
    this.onShowHelp,
    this.cheatsEnabled = false,
    this.onCheatTask,
    this.onCheatQuest,
    this.onCheatPlot,
    this.onCreateTestCharacter,
    this.autoReviveEndMs,
    this.speedBoostEndMs,
    this.isPaidUser = false,
    this.onSpeedBoostActivate,
    this.isSpeedBoostActive = false,
    this.onSetSpeed,
    this.adSpeedMultiplier = 5,
    this.has2xUnlocked = false,
  });

  final GameState state;
  final List<CombatLogEntry> combatLogEntries;
  final int speedMultiplier;
  final VoidCallback onSpeedCycle;

  /// 특정 속도로 직접 설정 (옵션 메뉴용)
  final void Function(int speed)? onSetSpeed;
  final bool isPaused;
  final VoidCallback onPauseToggle;
  final VoidCallback onSave;
  final VoidCallback onExit;
  final NotificationService notificationService;
  final void Function(String locale) onLanguageChange;
  final VoidCallback onDeleteSaveAndNewGame;
  final AsciiAnimationType? specialAnimation;

  /// BGM 볼륨 (0.0 ~ 1.0)
  final double bgmVolume;

  /// SFX 볼륨 (0.0 ~ 1.0)
  final double sfxVolume;

  /// BGM 볼륨 변경 콜백
  final void Function(double volume)? onBgmVolumeChange;

  /// SFX 볼륨 변경 콜백
  final void Function(double volume)? onSfxVolumeChange;

  /// 통계 표시 콜백
  final VoidCallback? onShowStatistics;

  /// 도움말 표시 콜백
  final VoidCallback? onShowHelp;

  /// 치트 모드 활성화 여부
  final bool cheatsEnabled;

  /// 치트: 태스크 완료
  final VoidCallback? onCheatTask;

  /// 치트: 퀘스트 완료
  final VoidCallback? onCheatQuest;

  /// 치트: 액트(플롯) 완료
  final VoidCallback? onCheatPlot;

  /// 테스트 캐릭터 생성 콜백 (디버그 모드 전용)
  final Future<void> Function()? onCreateTestCharacter;

  /// 자동부활 버프 종료 시점 (elapsedMs 기준)
  final int? autoReviveEndMs;

  /// 5배속 버프 종료 시점 (elapsedMs 기준)
  final int? speedBoostEndMs;

  /// 유료 유저 여부
  final bool isPaidUser;

  /// 광고 배속 활성화 콜백 (광고 시청)
  final VoidCallback? onSpeedBoostActivate;

  /// 배속 부스트 활성화 여부
  final bool isSpeedBoostActive;

  /// 광고 배속 배율 (릴리즈: 5x, 디버그빌드+디버그모드: 20x)
  final int adSpeedMultiplier;

  /// 2x 배속 해금 여부 (명예의 전당에 캐릭터가 있으면 true)
  final bool has2xUnlocked;

  @override
  State<MobileCarouselLayout> createState() => _MobileCarouselLayoutState();
}

class _MobileCarouselLayoutState extends State<MobileCarouselLayout> {
  late PageController _pageController;
  int _currentPage = CarouselPage.character.index; // 기본: 캐릭터시트

  @override
  void initState() {
    super.initState();
    _pageController = PageController(initialPage: _currentPage);
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  void _onPageChanged(int page) {
    setState(() {
      _currentPage = page;
    });
  }

  void _onNavPageSelected(int page) {
    _pageController.animateToPage(
      page,
      duration: const Duration(milliseconds: 300),
      curve: Curves.easeInOut,
    );
  }

  /// 현재 언어명 가져오기
  String _getCurrentLanguageName() {
    final locale = l10n.currentGameLocale;
    if (locale == 'ko') return l10n.languageKorean;
    if (locale == 'ja') return l10n.languageJapanese;
    return l10n.languageEnglish;
  }

  /// 언어 선택 다이얼로그 표시
  void _showLanguageDialog(BuildContext context) {
    showDialog<void>(
      context: context,
      builder: (context) => RetroSelectDialog(
        title: l10n.menuLanguage.toUpperCase(),
        children: [
          _buildLanguageOption(context, 'en', l10n.languageEnglish, '🇺🇸'),
          _buildLanguageOption(context, 'ko', l10n.languageKorean, '🇰🇷'),
          _buildLanguageOption(context, 'ja', l10n.languageJapanese, '🇯🇵'),
        ],
      ),
    );
  }

  Widget _buildLanguageOption(
    BuildContext context,
    String locale,
    String label,
    String flag,
  ) {
    final isSelected = l10n.currentGameLocale == locale;
    return RetroOptionItem(
      label: label.toUpperCase(),
      prefix: flag,
      isSelected: isSelected,
      onTap: () {
        Navigator.pop(context);
        widget.onLanguageChange(locale);
      },
    );
  }

  /// 사운드 상태 텍스트 가져오기
  String _getSoundStatus() {
    final bgmPercent = (widget.bgmVolume * 100).round();
    final sfxPercent = (widget.sfxVolume * 100).round();
    if (bgmPercent == 0 && sfxPercent == 0) {
      return l10n.uiSoundOff;
    }
    return 'BGM $bgmPercent% / SFX $sfxPercent%';
  }

  /// 사운드 설정 다이얼로그 표시
  void _showSoundDialog(BuildContext context) {
    var bgmVolume = widget.bgmVolume;
    var sfxVolume = widget.sfxVolume;

    showDialog<void>(
      context: context,
      builder: (context) => StatefulBuilder(
        builder: (context, setDialogState) => RetroSoundDialog(
          bgmVolume: bgmVolume,
          sfxVolume: sfxVolume,
          onBgmChanged: (double value) {
            setDialogState(() => bgmVolume = value);
            widget.onBgmVolumeChange?.call(value);
          },
          onSfxChanged: (double value) {
            setDialogState(() => sfxVolume = value);
            widget.onSfxVolumeChange?.call(value);
          },
        ),
      ),
    );
  }

  /// 세이브 삭제 확인 다이얼로그 표시
  void _showDeleteConfirmDialog(BuildContext context) {
    showDialog<void>(
      context: context,
      builder: (context) => RetroConfirmDialog(
        title: l10n.confirmDeleteTitle.toUpperCase(),
        message: l10n.confirmDeleteMessage,
        confirmText: l10n.buttonConfirm.toUpperCase(),
        cancelText: l10n.buttonCancel.toUpperCase(),
        onConfirm: () {
          Navigator.pop(context);
          widget.onDeleteSaveAndNewGame();
        },
        onCancel: () => Navigator.pop(context),
      ),
    );
  }

  /// 테스트 캐릭터 생성 확인 다이얼로그
  Future<void> _showTestCharacterDialog(BuildContext context) async {
    final confirmed = await showDialog<bool>(
      context: context,
      builder: (context) => RetroConfirmDialog(
        title: L10n.of(context).debugCreateTestCharacterTitle,
        message: L10n.of(context).debugCreateTestCharacterMessage,
        confirmText: L10n.of(context).createButton,
        cancelText: L10n.of(context).cancel.toUpperCase(),
        onConfirm: () => Navigator.of(context).pop(true),
        onCancel: () => Navigator.of(context).pop(false),
      ),
    );

    if (confirmed == true && mounted) {
      await widget.onCreateTestCharacter?.call();
    }
  }

  /// 옵션 메뉴 표시
  void _showOptionsMenu(BuildContext context) {
    final localizations = L10n.of(context);
    final background = RetroColors.backgroundOf(context);
    final gold = RetroColors.goldOf(context);
    final border = RetroColors.borderOf(context);

    showModalBottomSheet<void>(
      context: context,
      isScrollControlled: true,
      backgroundColor: Colors.transparent,
      constraints: BoxConstraints(
        maxHeight: MediaQuery.of(context).size.height * 0.75,
      ),
      builder: (context) => Container(
        decoration: BoxDecoration(
          color: background,
          border: Border.all(color: border, width: 2),
          borderRadius: const BorderRadius.vertical(top: Radius.circular(4)),
        ),
        child: SafeArea(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              // 핸들 바
              Padding(
                padding: const EdgeInsets.only(top: 8, bottom: 4),
                child: Container(
                  width: 60,
                  height: 4,
                  color: border,
                ),
              ),
              // 헤더
              Container(
                padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
                width: double.infinity,
                decoration: BoxDecoration(
                  color: RetroColors.panelBgOf(context),
                  border: Border(bottom: BorderSide(color: gold, width: 2)),
                ),
                child: Row(
                  children: [
                    Icon(Icons.settings, color: gold, size: 18),
                    const SizedBox(width: 8),
                    Text(
                      L10n.of(context).optionsTitle,
                      style: TextStyle(
                        fontFamily: 'PressStart2P',
                        fontSize: 14,
                        color: gold,
                      ),
                    ),
                    const Spacer(),
                    RetroIconButton(
                      icon: Icons.close,
                      onPressed: () => Navigator.pop(context),
                      size: 28,
                    ),
                  ],
                ),
              ),
              // 메뉴 목록
              Expanded(
                child: SingleChildScrollView(
                  padding: const EdgeInsets.all(12),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      // === 게임 제어 ===
                      RetroMenuSection(title: L10n.of(context).controlSection),
                      const SizedBox(height: 8),
                      // 일시정지/재개
                      RetroMenuItem(
                        icon: widget.isPaused ? Icons.play_arrow : Icons.pause,
                        iconColor: widget.isPaused
                            ? RetroColors.expOf(context)
                            : RetroColors.warningOf(context),
                        label: widget.isPaused
                            ? l10n.menuResume.toUpperCase()
                            : l10n.menuPause.toUpperCase(),
                        onTap: () {
                          Navigator.pop(context);
                          widget.onPauseToggle();
                        },
                      ),
                      const SizedBox(height: 8),
                      // 속도 조절
                      RetroMenuItem(
                        icon: Icons.speed,
                        iconColor: gold,
                        label: l10n.menuSpeed.toUpperCase(),
                        trailing: _buildRetroSpeedSelector(context),
                      ),
                      const SizedBox(height: 16),

                      // === 정보 ===
                      RetroMenuSection(title: L10n.of(context).infoSection),
                      const SizedBox(height: 8),
                      if (widget.onShowStatistics != null)
                        RetroMenuItem(
                          icon: Icons.bar_chart,
                          iconColor: RetroColors.mpOf(context),
                          label: l10n.uiStatistics.toUpperCase(),
                          onTap: () {
                            Navigator.pop(context);
                            widget.onShowStatistics?.call();
                          },
                        ),
                      if (widget.onShowHelp != null) ...[
                        const SizedBox(height: 8),
                        RetroMenuItem(
                          icon: Icons.help_outline,
                          iconColor: RetroColors.expOf(context),
                          label: l10n.uiHelp.toUpperCase(),
                          onTap: () {
                            Navigator.pop(context);
                            widget.onShowHelp?.call();
                          },
                        ),
                      ],
                      const SizedBox(height: 16),

                      // === 설정 ===
                      RetroMenuSection(title: L10n.of(context).settingsSection),
                      const SizedBox(height: 8),
                      RetroMenuItem(
                        icon: Icons.language,
                        iconColor: RetroColors.mpOf(context),
                        label: l10n.menuLanguage.toUpperCase(),
                        value: _getCurrentLanguageName(),
                        onTap: () {
                          Navigator.pop(context);
                          _showLanguageDialog(context);
                        },
                      ),
                      if (widget.onBgmVolumeChange != null ||
                          widget.onSfxVolumeChange != null) ...[
                        const SizedBox(height: 8),
                        RetroMenuItem(
                          icon: widget.bgmVolume == 0 && widget.sfxVolume == 0
                              ? Icons.volume_off
                              : Icons.volume_up,
                          iconColor: RetroColors.textMutedOf(context),
                          label: l10n.uiSound.toUpperCase(),
                          value: _getSoundStatus(),
                          onTap: () {
                            Navigator.pop(context);
                            _showSoundDialog(context);
                          },
                        ),
                      ],
                      const SizedBox(height: 16),

                      // === 저장/종료 ===
                      RetroMenuSection(title: L10n.of(context).saveExitSection),
                      const SizedBox(height: 8),
                      RetroMenuItem(
                        icon: Icons.save,
                        iconColor: RetroColors.mpOf(context),
                        label: l10n.menuSave.toUpperCase(),
                        onTap: () {
                          Navigator.pop(context);
                          widget.onSave();
                          widget.notificationService.showGameSaved(
                            l10n.menuSaved,
                          );
                        },
                      ),
                      const SizedBox(height: 8),
                      RetroMenuItem(
                        icon: Icons.refresh,
                        iconColor: RetroColors.warningOf(context),
                        label: l10n.menuNewGame.toUpperCase(),
                        subtitle: l10n.menuDeleteSave,
                        onTap: () {
                          Navigator.pop(context);
                          _showDeleteConfirmDialog(context);
                        },
                      ),
                      const SizedBox(height: 8),
                      RetroMenuItem(
                        icon: Icons.exit_to_app,
                        iconColor: RetroColors.hpOf(context),
                        label: localizations.exitGame.toUpperCase(),
                        onTap: () {
                          Navigator.pop(context);
                          widget.onExit();
                        },
                      ),

                      // === 치트 섹션 (디버그 모드에서만) ===
                      if (widget.cheatsEnabled) ...[
                        const SizedBox(height: 16),
                        RetroMenuSection(
                          title: L10n.of(context).debugCheatsTitle,
                          color: RetroColors.hpOf(context),
                        ),
                        const SizedBox(height: 8),
                        RetroMenuItem(
                          icon: Icons.fast_forward,
                          iconColor: RetroColors.hpOf(context),
                          label: L10n.of(context).debugSkipTask,
                          subtitle: L10n.of(context).debugSkipTaskDesc,
                          onTap: () {
                            Navigator.pop(context);
                            widget.onCheatTask?.call();
                          },
                        ),
                        const SizedBox(height: 8),
                        RetroMenuItem(
                          icon: Icons.skip_next,
                          iconColor: RetroColors.hpOf(context),
                          label: L10n.of(context).debugSkipQuest,
                          subtitle: L10n.of(context).debugSkipQuestDesc,
                          onTap: () {
                            Navigator.pop(context);
                            widget.onCheatQuest?.call();
                          },
                        ),
                        const SizedBox(height: 8),
                        RetroMenuItem(
                          icon: Icons.double_arrow,
                          iconColor: RetroColors.hpOf(context),
                          label: L10n.of(context).debugSkipAct,
                          subtitle: L10n.of(context).debugSkipActDesc,
                          onTap: () {
                            Navigator.pop(context);
                            widget.onCheatPlot?.call();
                          },
                        ),
                      ],

                      // === 디버그 도구 섹션 ===
                      if (kDebugMode && widget.onCreateTestCharacter != null) ...[
                        const SizedBox(height: 16),
                        RetroMenuSection(
                          title: L10n.of(context).debugToolsTitle,
                          color: RetroColors.warningOf(context),
                        ),
                        const SizedBox(height: 8),
                        RetroMenuItem(
                          icon: Icons.science,
                          iconColor: RetroColors.warningOf(context),
                          label: L10n.of(context).debugCreateTestCharacter,
                          subtitle: L10n.of(context).debugCreateTestCharacterDesc,
                          onTap: () {
                            Navigator.pop(context);
                            _showTestCharacterDialog(context);
                          },
                        ),
                      ],
                      const SizedBox(height: 16),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  /// 레트로 스타일 속도 선택기
  ///
  /// - 5x/20x 토글 버튼 하나만 표시
  /// - 부스트 활성화 중: 반투명, 비활성 (누를 수 없음)
  /// - 부스트 비활성화: 불투명, 활성 (누를 수 있음)
  Widget _buildRetroSpeedSelector(BuildContext context) {
    final isSpeedBoostActive = widget.isSpeedBoostActive;
    final adSpeed = widget.adSpeedMultiplier;

    return RetroSpeedChip(
      speed: adSpeed,
      isSelected: isSpeedBoostActive,
      isAdBased: !isSpeedBoostActive && !widget.isPaidUser,
      // 부스트 활성화 중이면 비활성 (반투명)
      isDisabled: isSpeedBoostActive,
      onTap: () {
        if (!isSpeedBoostActive) {
          widget.onSpeedBoostActivate?.call();
        }
        Navigator.pop(context);
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    final state = widget.state;
    final background = RetroColors.backgroundOf(context);
    final panelBg = RetroColors.panelBgOf(context);
    final gold = RetroColors.goldOf(context);

    return Scaffold(
      backgroundColor: background,
      appBar: AppBar(
        backgroundColor: panelBg,
        title: Text(
          L10n.of(context).progressQuestTitle(state.traits.name),
          style: TextStyle(
            fontFamily: 'PressStart2P',
            fontSize: 14,
            color: gold,
          ),
        ),
        actions: [
          // 옵션 버튼
          IconButton(
            icon: Icon(Icons.settings, color: gold),
            onPressed: () => _showOptionsMenu(context),
            tooltip: l10n.menuOptions,
          ),
        ],
      ),
      body: SafeArea(
        top: false, // AppBar가 상단 처리
        child: Column(
          children: [
            // 상단: 확장 애니메이션 패널
            EnhancedAnimationPanel(
              progress: state.progress,
              stats: state.stats,
              skillSystem: state.skillSystem,
              speedMultiplier: widget.speedMultiplier,
              onSpeedCycle: widget.onSpeedCycle,
              isPaused: widget.isPaused,
              onPauseToggle: widget.onPauseToggle,
              specialAnimation: widget.specialAnimation,
              weaponName: state.equipment.weapon,
              shieldName: state.equipment.shield,
              characterLevel: state.traits.level,
              monsterLevel: state.progress.currentTask.monsterLevel,
              monsterGrade: state.progress.currentTask.monsterGrade,
              monsterSize: state.progress.currentTask.monsterSize,
              latestCombatEvent:
                  state.progress.currentCombat?.recentEvents.lastOrNull,
              raceId: state.traits.raceId,
              weaponRarity: state.equipment.weaponItem.rarity,
              autoReviveEndMs: widget.autoReviveEndMs,
              speedBoostEndMs: widget.speedBoostEndMs,
              isPaidUser: widget.isPaidUser,
              onSpeedBoostActivate: widget.onSpeedBoostActivate,
              isSpeedBoostActive: widget.isSpeedBoostActive,
              adSpeedMultiplier: widget.adSpeedMultiplier,
            ),

            // 중앙: 캐로셀 (PageView)
            Expanded(
              child: PageView(
                controller: _pageController,
                onPageChanged: _onPageChanged,
                children: [
                  // 0: 스킬
                  SkillsPage(
                    skillBook: state.skillBook,
                    skillSystem: state.skillSystem,
                  ),

                  // 1: 인벤토리
                  InventoryPage(
                    inventory: state.inventory,
                    potionInventory: state.potionInventory,
                    encumbrance: state.progress.encumbrance,
                  ),

                  // 2: 장비
                  EquipmentPage(equipment: state.equipment),

                  // 3: 캐릭터시트 (기본)
                  CharacterSheetPage(
                    traits: state.traits,
                    stats: state.stats,
                    exp: state.progress.exp,
                  ),

                  // 4: 전투로그
                  CombatLogPage(entries: widget.combatLogEntries),

                  // 5: 퀘스트
                  QuestPage(
                    questHistory: state.progress.questHistory,
                    quest: state.progress.quest,
                  ),

                  // 6: 스토리
                  StoryPage(
                    plotStageCount: state.progress.plotStageCount,
                    plot: state.progress.plot,
                  ),
                ],
              ),
            ),

            // 하단: 네비게이션 바
            CarouselNavBar(
              currentPage: _currentPage,
              onPageSelected: _onNavPageSelected,
            ),
          ],
        ),
      ),
    );
  }
}
