import 'package:flutter/material.dart';

import 'package:asciineverdie/data/game_text_l10n.dart' as l10n;
import 'package:asciineverdie/src/shared/animation/ascii_animation_type.dart';
import 'package:asciineverdie/src/shared/animation/monster_size.dart';
import 'package:asciineverdie/src/core/model/combat_event.dart';
import 'package:asciineverdie/src/core/model/game_state.dart';
import 'package:asciineverdie/src/core/model/item_stats.dart';
import 'package:asciineverdie/src/core/model/monster_grade.dart';
import 'package:asciineverdie/src/features/game/widgets/ascii_animation_card.dart';
import 'package:asciineverdie/src/features/game/widgets/compact_status_bars.dart';

/// 모바일용 확장 애니메이션 패널
///
/// 캐로셀 레이아웃에서 상단 영역에 표시되는 통합 패널:
/// - ASCII 애니메이션 (기존 높이 유지)
/// - 플레이어 HP/MP 컴팩트 바 (플로팅 텍스트 포함)
/// - 활성 버프 아이콘 (최대 3개)
/// - 몬스터 HP 바 (전투 중)
class EnhancedAnimationPanel extends StatefulWidget {
  const EnhancedAnimationPanel({
    super.key,
    required this.progress,
    required this.stats,
    required this.skillSystem,
    required this.speedMultiplier,
    required this.onSpeedCycle,
    required this.isPaused,
    required this.onPauseToggle,
    this.specialAnimation,
    this.weaponName,
    this.shieldName,
    this.characterLevel,
    this.monsterLevel,
    this.monsterGrade,
    this.monsterSize,
    this.latestCombatEvent,
    this.raceId,
    this.weaponRarity,
    this.autoReviveEndMs,
    this.speedBoostEndMs,
    this.isPaidUser = false,
    this.onSpeedBoostActivate,
    this.isSpeedBoostActive = false,
    this.adSpeedMultiplier = 5,
  });

  final ProgressState progress;
  final Stats stats;
  final SkillSystemState skillSystem;
  final int speedMultiplier;
  final VoidCallback onSpeedCycle;
  final bool isPaused;
  final VoidCallback onPauseToggle;
  final AsciiAnimationType? specialAnimation;
  final String? weaponName;
  final String? shieldName;
  final int? characterLevel;
  final int? monsterLevel;

  /// 몬스터 등급 (Normal/Elite/Boss) - UI 색상/접두사 표시용
  final MonsterGrade? monsterGrade;

  /// 몬스터 사이즈 (애니메이션 크기 결정용, Act 기반)
  final MonsterSize? monsterSize;
  final CombatEvent? latestCombatEvent;

  /// 종족 ID (Phase 4: 종족별 캐릭터 애니메이션)
  final String? raceId;

  /// 무기 희귀도 (Phase 9: 무기 등급별 이펙트 색상)
  final ItemRarity? weaponRarity;

  /// 자동부활 버프 종료 시점 (elapsedMs 기준, null이면 비활성)
  final int? autoReviveEndMs;

  /// 5배속 버프 종료 시점 (elapsedMs 기준, null이면 비활성)
  final int? speedBoostEndMs;

  /// 유료 유저 여부 (광고배속 항상 활성)
  final bool isPaidUser;

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

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

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

  @override
  State<EnhancedAnimationPanel> createState() => _EnhancedAnimationPanelState();
}

class _EnhancedAnimationPanelState extends State<EnhancedAnimationPanel>
    with TickerProviderStateMixin {
  // HP/MP 변화 애니메이션
  late AnimationController _hpFlashController;
  late AnimationController _mpFlashController;
  late AnimationController _monsterFlashController;
  late Animation<double> _hpFlashAnimation;
  late Animation<double> _mpFlashAnimation;
  late Animation<double> _monsterFlashAnimation;

  int _hpChange = 0;
  int _mpChange = 0;
  int _monsterHpChange = 0;

  // 이전 값 추적
  int _lastHp = 0;
  int _lastMp = 0;
  int _lastMonsterHp = 0;

  // 몬스터 사망 상태 추적
  bool _monsterDied = false;
  bool _wasInCombat = false;

  @override
  void initState() {
    super.initState();

    _hpFlashController = AnimationController(
      duration: const Duration(milliseconds: 500),
      vsync: this,
    );
    _mpFlashController = AnimationController(
      duration: const Duration(milliseconds: 500),
      vsync: this,
    );
    _monsterFlashController = AnimationController(
      duration: const Duration(milliseconds: 500),
      vsync: this,
    );

    _hpFlashAnimation = Tween<double>(begin: 1.0, end: 0.0).animate(
      CurvedAnimation(parent: _hpFlashController, curve: Curves.easeOut),
    );
    _mpFlashAnimation = Tween<double>(begin: 1.0, end: 0.0).animate(
      CurvedAnimation(parent: _mpFlashController, curve: Curves.easeOut),
    );
    _monsterFlashAnimation = Tween<double>(begin: 1.0, end: 0.0).animate(
      CurvedAnimation(parent: _monsterFlashController, curve: Curves.easeOut),
    );

    // 초기값 설정
    _lastHp = _currentHp;
    _lastMp = _currentMp;
    _lastMonsterHp = _currentMonsterHp ?? 0;
  }

  @override
  void didUpdateWidget(EnhancedAnimationPanel oldWidget) {
    super.didUpdateWidget(oldWidget);

    // HP 변화 감지
    final newHp = _currentHp;
    if (newHp != _lastHp) {
      _hpChange = newHp - _lastHp;
      _hpFlashController.forward(from: 0.0);
      _lastHp = newHp;
    }

    // MP 변화 감지
    final newMp = _currentMp;
    if (newMp != _lastMp) {
      _mpChange = newMp - _lastMp;
      _mpFlashController.forward(from: 0.0);
      _lastMp = newMp;
    }

    // 몬스터 HP 변화 감지
    final newMonsterHp = _currentMonsterHp;
    if (newMonsterHp != null && newMonsterHp != _lastMonsterHp) {
      _monsterHpChange = newMonsterHp - _lastMonsterHp;
      _monsterFlashController.forward(from: 0.0);
      _lastMonsterHp = newMonsterHp;
    } else if (newMonsterHp == null) {
      _lastMonsterHp = 0;
    }

    // 몬스터 사망 감지: 전투 중 → 비전투 전환 시 몬스터 사망
    final combat = widget.progress.currentCombat;
    final isNowInCombat = combat != null && combat.isActive;
    if (_wasInCombat && !isNowInCombat) {
      // 전투가 끝났고, 태스크가 여전히 kill이면 몬스터 사망 (플레이어 승리)
      if (widget.progress.currentTask.type == TaskType.kill) {
        setState(() {
          _monsterDied = true;
        });
        // 잠시 후 리셋 (애니메이션 완료 후)
        Future.delayed(const Duration(milliseconds: 900), () {
          if (mounted) {
            setState(() {
              _monsterDied = false;
            });
          }
        });
      }
    }
    _wasInCombat = isNowInCombat;
  }

  int get _currentHp =>
      widget.progress.currentCombat?.playerStats.hpCurrent ?? widget.stats.hp;
  int get _currentHpMax =>
      widget.progress.currentCombat?.playerStats.hpMax ?? widget.stats.hpMax;
  int get _currentMp =>
      widget.progress.currentCombat?.playerStats.mpCurrent ?? widget.stats.mp;
  int get _currentMpMax =>
      widget.progress.currentCombat?.playerStats.mpMax ?? widget.stats.mpMax;
  int? get _currentMonsterHp =>
      widget.progress.currentCombat?.monsterStats.hpCurrent;
  int? get _currentMonsterHpMax =>
      widget.progress.currentCombat?.monsterStats.hpMax;

  /// 자동부활 버프 남은 시간 (ms)
  int get _autoReviveRemainingMs {
    final endMs = widget.autoReviveEndMs;
    if (endMs == null) return 0;
    final remaining = endMs - widget.skillSystem.elapsedMs;
    return remaining > 0 ? remaining : 0;
  }

  /// 5배속 버프 남은 시간 (ms)
  int get _speedBoostRemainingMs {
    final endMs = widget.speedBoostEndMs;
    if (endMs == null) return 0;
    final remaining = endMs - widget.skillSystem.elapsedMs;
    return remaining > 0 ? remaining : 0;
  }

  @override
  void dispose() {
    _hpFlashController.dispose();
    _mpFlashController.dispose();
    _monsterFlashController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final combat = widget.progress.currentCombat;
    final isInCombat = combat != null && combat.isActive;
    // 몬스터 HP는 전투 중이면서 kill 태스크일 때만 표시
    // (전투 승리 후 타운 방문 시 HP UI 잔존 버그 방지)
    final isKillTask = widget.progress.currentTask.type == TaskType.kill;
    final shouldShowMonsterHp = isInCombat && isKillTask;

    return Container(
      padding: const EdgeInsets.all(8),
      decoration: BoxDecoration(
        color: Theme.of(context).colorScheme.surfaceContainerHighest,
        border: Border(
          bottom: BorderSide(color: Theme.of(context).dividerColor),
        ),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          // ASCII 애니메이션 (기존 높이 120 유지) + 버프 오버레이
          SizedBox(
            height: 120,
            child: Stack(
              children: [
                // ASCII 애니메이션
                AsciiAnimationCard(
                  taskType: widget.progress.currentTask.type,
                  monsterBaseName: widget.progress.currentTask.monsterBaseName,
                  specialAnimation: widget.specialAnimation,
                  weaponName: widget.weaponName,
                  shieldName: widget.shieldName,
                  characterLevel: widget.characterLevel,
                  monsterLevel: widget.monsterLevel,
                  monsterGrade: widget.monsterGrade,
                  monsterSize: widget.monsterSize,
                  isPaused: widget.isPaused,
                  isInCombat: isInCombat,
                  monsterDied: _monsterDied,
                  latestCombatEvent: widget.latestCombatEvent,
                  raceId: widget.raceId,
                  weaponRarity: widget.weaponRarity,
                ),
                // 좌상단: 자동부활 버프
                if (_autoReviveRemainingMs > 0)
                  Positioned(
                    left: 4,
                    top: 4,
                    child: _buildBuffChip(
                      icon: '↺',
                      remainingMs: _autoReviveRemainingMs,
                      color: Colors.green,
                    ),
                  ),
                // 우상단: 광고배속 버프 (버프 활성 시에만)
                if (_speedBoostRemainingMs > 0 || widget.isPaidUser)
                  Positioned(
                    right: 4,
                    top: 4,
                    child: _buildBuffChip(
                      icon: '⚡',
                      label: '${widget.adSpeedMultiplier}x',
                      remainingMs: widget.isPaidUser
                          ? -1
                          : _speedBoostRemainingMs,
                      color: Colors.orange,
                      isPermanent: widget.isPaidUser,
                    ),
                  ),
              ],
            ),
          ),

          const SizedBox(height: 8),

          // 상태 바 영역: HP/MP (40%) + 빈공간 (20%) + 몬스터 HP (40%)
          SizedBox(
            height: 48,
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                // 좌측: HP/MP 바 (40%)
                Expanded(
                  flex: 2,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      Expanded(
                        child: CompactHpBar(
                          current: _currentHp,
                          max: _currentHpMax,
                          flashAnimation: _hpFlashAnimation,
                          hpChange: _hpChange,
                        ),
                      ),
                      const SizedBox(height: 4),
                      Expanded(
                        child: CompactMpBar(
                          current: _currentMp,
                          max: _currentMpMax,
                          flashAnimation: _mpFlashAnimation,
                          mpChange: _mpChange,
                        ),
                      ),
                    ],
                  ),
                ),

                // 중앙: 빈 공간 (20%)
                const Spacer(flex: 1),

                // 우측: 몬스터 HP (전투 중) 또는 빈 공간 (40%)
                Expanded(
                  flex: 2,
                  child: switch ((shouldShowMonsterHp, combat)) {
                    (true, final c?) => CompactMonsterHpBar(
                      combat: c,
                      monsterHpCurrent: _currentMonsterHp,
                      monsterHpMax: _currentMonsterHpMax,
                      monsterLevel: widget.monsterLevel,
                      flashAnimation: _monsterFlashAnimation,
                      monsterHpChange: _monsterHpChange,
                    ),
                    _ => const SizedBox.shrink(),
                  },
                ),
              ],
            ),
          ),

          const SizedBox(height: 6),

          // 하단: 태스크 프로그레스 바 + 캡션
          _buildTaskProgress(),
        ],
      ),
    );
  }

  /// 속도 컨트롤 버튼 (태스크 프로그레스 바 우측)
  ///
  /// - 5x/20x 토글 버튼 하나만 표시
  /// - 부스트 활성화 중: 반투명, 비활성 (누를 수 없음)
  /// - 부스트 비활성화: 불투명, 활성 (누를 수 있음)
  Widget _buildSpeedControls() {
    final isSpeedBoostActive = widget.isSpeedBoostActive;
    final adSpeed = widget.adSpeedMultiplier;
    final isPaidUser = widget.isPaidUser;

    // 부스트 활성화 중이면 반투명
    final opacity = isSpeedBoostActive ? 0.4 : 1.0;

    return SizedBox(
      width: 56,
      height: 32,
      child: OutlinedButton(
        // 부스트 활성화 중이면 누를 수 없음
        onPressed: isSpeedBoostActive ? null : widget.onSpeedBoostActivate,
        style: OutlinedButton.styleFrom(
          padding: EdgeInsets.zero,
          side: BorderSide(
            color: Colors.orange.withValues(alpha: opacity),
            width: isSpeedBoostActive ? 2 : 1.5,
          ),
          disabledForegroundColor: Colors.orange.withValues(alpha: 0.4),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // 무료유저 + 비활성 상태면 AD 아이콘 표시
            if (!isPaidUser && !isSpeedBoostActive) ...[
              const Text(
                '▶',
                style: TextStyle(fontSize: 9, color: Colors.orange),
              ),
              const SizedBox(width: 2),
            ],
            Text(
              '${adSpeed}x',
              style: TextStyle(
                fontSize: 13,
                fontWeight: FontWeight.bold,
                color: Colors.orange.withValues(alpha: opacity),
              ),
            ),
          ],
        ),
      ),
    );
  }

  /// 현재 상태에 맞는 메시지 반환
  ///
  /// 특수 애니메이션(부활 등) 중에는 해당 메시지 표시
  String _getStatusMessage() {
    // 부활 애니메이션 중에는 부활 메시지 표시
    if (widget.specialAnimation == AsciiAnimationType.resurrection) {
      return l10n.animationResurrecting;
    }

    // 기본: 현재 태스크 캡션
    return widget.progress.currentTask.caption;
  }

  /// 태스크 프로그레스 바 + 속도 컨트롤
  Widget _buildTaskProgress() {
    final task = widget.progress.task;
    final progressValue = task.max > 0
        ? (task.position / task.max).clamp(0.0, 1.0)
        : 0.0;

    // 몬스터 등급에 따른 접두사와 색상
    final grade = widget.monsterGrade;
    final isKillTask = widget.progress.currentTask.type == TaskType.kill;
    final gradePrefix = (isKillTask && grade != null)
        ? grade.displayPrefix
        : '';
    final gradeColorCode = (isKillTask && grade != null)
        ? grade.displayColorCode
        : null;
    final gradeColor = gradeColorCode != null ? Color(gradeColorCode) : null;

    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        // 좌측: 캡션 + 프로그레스 바
        Expanded(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              // 캡션 (등급에 따른 접두사 및 색상)
              Text.rich(
                TextSpan(
                  children: [
                    if (gradePrefix.isNotEmpty)
                      TextSpan(
                        text: gradePrefix,
                        style: TextStyle(
                          color: gradeColor,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    TextSpan(
                      text: _getStatusMessage(),
                      style: gradeColor != null
                          ? TextStyle(color: gradeColor)
                          : null,
                    ),
                  ],
                ),
                style: Theme.of(context).textTheme.bodySmall,
                textAlign: TextAlign.center,
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              ),
              const SizedBox(height: 4),
              // 프로그레스 바
              LinearProgressIndicator(
                value: progressValue,
                backgroundColor: Theme.of(
                  context,
                ).colorScheme.primary.withValues(alpha: 0.2),
                valueColor: AlwaysStoppedAnimation<Color>(
                  Theme.of(context).colorScheme.primary,
                ),
                minHeight: 10,
              ),
            ],
          ),
        ),
        const SizedBox(width: 8),
        // 우측: 속도 컨트롤
        _buildSpeedControls(),
      ],
    );
  }

  /// 버프 칩 위젯 (좌상단/우상단 오버레이용)
  Widget _buildBuffChip({
    required String icon,
    required int remainingMs,
    required Color color,
    String? label,
    bool isPermanent = false,
  }) {
    // 남은 시간 포맷 (분:초)
    String timeText;
    if (isPermanent) {
      timeText = '∞';
    } else {
      final seconds = (remainingMs / 1000).ceil();
      final min = seconds ~/ 60;
      final sec = seconds % 60;
      timeText = '$min:${sec.toString().padLeft(2, '0')}';
    }

    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 3),
      decoration: BoxDecoration(
        color: Colors.black.withValues(alpha: 0.7),
        borderRadius: BorderRadius.circular(4),
        border: Border.all(color: color.withValues(alpha: 0.7), width: 1),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(icon, style: TextStyle(fontSize: 12, color: color)),
          if (label != null) ...[
            const SizedBox(width: 2),
            Text(
              label,
              style: TextStyle(
                fontFamily: 'JetBrainsMono',
                fontSize: 10,
                color: color,
                fontWeight: FontWeight.bold,
              ),
            ),
          ],
          const SizedBox(width: 3),
          Text(
            timeText,
            style: TextStyle(
              fontFamily: 'JetBrainsMono',
              fontSize: 10,
              color: color,
              fontWeight: FontWeight.bold,
            ),
          ),
        ],
      ),
    );
  }
}
