import 'package:flutter/material.dart';

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

/// HP/MP 바 위젯 (레트로 RPG 스타일)
///
/// - 세그먼트 스타일의 8-bit 프로그레스 바
/// - HP가 20% 미만일 때 빨간색 깜빡임
/// - HP/MP 변화 시 색상 플래시 + 변화량 표시
/// - 전투 중 몬스터 HP 바 표시
class HpMpBar extends StatefulWidget {
  const HpMpBar({
    super.key,
    required this.hpCurrent,
    required this.hpMax,
    required this.mpCurrent,
    required this.mpMax,
    this.monsterHpCurrent,
    this.monsterHpMax,
    this.monsterName,
    this.monsterLevel,
  });

  final int hpCurrent;
  final int hpMax;
  final int mpCurrent;
  final int mpMax;

  /// 전투 중 몬스터 HP (null이면 비전투)
  final int? monsterHpCurrent;
  final int? monsterHpMax;
  final String? monsterName;
  final int? monsterLevel;

  @override
  State<HpMpBar> createState() => _HpMpBarState();
}

class _HpMpBarState extends State<HpMpBar> with TickerProviderStateMixin {
  late AnimationController _blinkController;
  late Animation<double> _blinkAnimation;

  // HP/MP 변화 애니메이션
  late AnimationController _hpFlashController;
  late AnimationController _mpFlashController;
  late Animation<double> _hpFlashAnimation;
  late Animation<double> _mpFlashAnimation;

  // 변화량 표시용
  int _hpChange = 0;
  int _mpChange = 0;
  bool _hpDamage = false;
  bool _mpDamage = false;

  // 몬스터 HP 변화 애니메이션
  late AnimationController _monsterFlashController;
  late Animation<double> _monsterFlashAnimation;
  int _monsterHpChange = 0;

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

    // 위험 깜빡임
    _blinkController = AnimationController(
      duration: const Duration(milliseconds: 500),
      vsync: this,
    );
    _blinkAnimation = Tween<double>(begin: 1.0, end: 0.3).animate(
      CurvedAnimation(parent: _blinkController, curve: Curves.easeInOut),
    );

    // HP 플래시
    _hpFlashController = AnimationController(
      duration: const Duration(milliseconds: 400),
      vsync: this,
    );
    _hpFlashAnimation = Tween<double>(begin: 1.0, end: 0.0).animate(
      CurvedAnimation(parent: _hpFlashController, curve: Curves.easeOut),
    );

    // MP 플래시
    _mpFlashController = AnimationController(
      duration: const Duration(milliseconds: 400),
      vsync: this,
    );
    _mpFlashAnimation = Tween<double>(begin: 1.0, end: 0.0).animate(
      CurvedAnimation(parent: _mpFlashController, curve: Curves.easeOut),
    );

    // 몬스터 HP 플래시
    _monsterFlashController = AnimationController(
      duration: const Duration(milliseconds: 400),
      vsync: this,
    );
    _monsterFlashAnimation = Tween<double>(begin: 1.0, end: 0.0).animate(
      CurvedAnimation(parent: _monsterFlashController, curve: Curves.easeOut),
    );

    _updateBlinkState();
  }

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

    // HP 변화 감지
    if (oldWidget.hpCurrent != widget.hpCurrent) {
      _hpChange = widget.hpCurrent - oldWidget.hpCurrent;
      _hpDamage = _hpChange < 0;
      _hpFlashController.forward(from: 0.0);
    }

    // MP 변화 감지
    if (oldWidget.mpCurrent != widget.mpCurrent) {
      _mpChange = widget.mpCurrent - oldWidget.mpCurrent;
      _mpDamage = _mpChange < 0;
      _mpFlashController.forward(from: 0.0);
    }

    // 몬스터 HP 변화 감지
    if (oldWidget.monsterHpCurrent != widget.monsterHpCurrent &&
        widget.monsterHpCurrent != null &&
        oldWidget.monsterHpCurrent != null) {
      _monsterHpChange = widget.monsterHpCurrent! - oldWidget.monsterHpCurrent!;
      _monsterFlashController.forward(from: 0.0);
    }

    _updateBlinkState();
  }

  void _updateBlinkState() {
    final hpRatio = widget.hpMax > 0 ? widget.hpCurrent / widget.hpMax : 1.0;

    if (hpRatio < 0.2 && hpRatio > 0) {
      if (!_blinkController.isAnimating) {
        _blinkController.repeat(reverse: true);
      }
    } else {
      _blinkController.stop();
      _blinkController.reset();
    }
  }

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

  @override
  Widget build(BuildContext context) {
    final hpRatio = widget.hpMax > 0 ? widget.hpCurrent / widget.hpMax : 0.0;
    final mpRatio = widget.mpMax > 0 ? widget.mpCurrent / widget.mpMax : 0.0;

    final hasMonster =
        widget.monsterHpCurrent != null &&
        widget.monsterHpMax != null &&
        widget.monsterHpMax! > 0;

    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 6),
      decoration: BoxDecoration(
        color: RetroColors.panelBg,
        border: Border.all(color: RetroColors.panelBorderOuter, width: 2),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          // HP 바 (플래시 효과 포함)
          _buildAnimatedBar(
            label: l10n.statHp,
            current: widget.hpCurrent,
            max: widget.hpMax,
            ratio: hpRatio,
            fillColor: RetroColors.hpRed,
            emptyColor: RetroColors.hpRedDark,
            isLow: hpRatio < 0.2 && hpRatio > 0,
            flashController: _hpFlashAnimation,
            change: _hpChange,
            isDamage: _hpDamage,
          ),
          const SizedBox(height: 6),

          // MP 바 (플래시 효과 포함)
          _buildAnimatedBar(
            label: l10n.statMp,
            current: widget.mpCurrent,
            max: widget.mpMax,
            ratio: mpRatio,
            fillColor: RetroColors.mpBlue,
            emptyColor: RetroColors.mpBlueDark,
            isLow: false,
            flashController: _mpFlashAnimation,
            change: _mpChange,
            isDamage: _mpDamage,
          ),

          // 몬스터 HP 바 (전투 중일 때만)
          if (hasMonster) ...[
            const SizedBox(height: 8),
            RetroMonsterHpBar(
              monsterHpCurrent: widget.monsterHpCurrent!,
              monsterHpMax: widget.monsterHpMax!,
              monsterName: widget.monsterName,
              monsterLevel: widget.monsterLevel,
              flashAnimation: _monsterFlashAnimation,
              monsterHpChange: _monsterHpChange,
            ),
          ],
        ],
      ),
    );
  }

  Widget _buildAnimatedBar({
    required String label,
    required int current,
    required int max,
    required double ratio,
    required Color fillColor,
    required Color emptyColor,
    required bool isLow,
    required Animation<double> flashController,
    required int change,
    required bool isDamage,
  }) {
    return AnimatedBuilder(
      animation: Listenable.merge([_blinkAnimation, flashController]),
      builder: (context, child) {
        // 플래시 색상 (데미지=빨강, 회복=녹색)
        final flashColor = isDamage
            ? RetroColors.hpRed.withValues(alpha: flashController.value * 0.4)
            : RetroColors.expGreen.withValues(
                alpha: flashController.value * 0.4,
              );

        // 위험 깜빡임 배경
        final lowBgColor = isLow
            ? RetroColors.hpRed.withValues(
                alpha: (1 - _blinkAnimation.value) * 0.3,
              )
            : Colors.transparent;

        return Container(
          decoration: BoxDecoration(
            color: flashController.value > 0.1 ? flashColor : lowBgColor,
          ),
          child: Stack(
            children: [
              _buildRetroBar(
                label: label,
                current: current,
                max: max,
                ratio: ratio,
                fillColor: fillColor,
                emptyColor: emptyColor,
                blinkOpacity: isLow ? _blinkAnimation.value : 1.0,
              ),

              // 플로팅 변화량 텍스트 (위로 떠오르며 사라짐)
              if (change != 0 && flashController.value > 0.05)
                Positioned(
                  right: 70,
                  top: 0,
                  bottom: 0,
                  child: Transform.translate(
                    // 위로 떠오르는 애니메이션 (최대 15픽셀 위로)
                    offset: Offset(0, -15 * (1 - flashController.value)),
                    child: Opacity(
                      opacity: flashController.value,
                      child: Text(
                        change > 0 ? '+$change' : '$change',
                        style: TextStyle(
                          fontFamily: 'PressStart2P',
                          fontSize: 13,
                          fontWeight: FontWeight.bold,
                          color: isDamage
                              ? RetroColors.hpRed
                              : RetroColors.expGreen,
                          shadows: const [
                            Shadow(color: Colors.black, blurRadius: 3),
                            Shadow(color: Colors.black, blurRadius: 6),
                          ],
                        ),
                      ),
                    ),
                  ),
                ),
            ],
          ),
        );
      },
    );
  }

  /// 레트로 스타일 세그먼트 바 (숫자 바 위 오버레이)
  Widget _buildRetroBar({
    required String label,
    required int current,
    required int max,
    required double ratio,
    required Color fillColor,
    required Color emptyColor,
    required double blinkOpacity,
  }) {
    const segmentCount = 15;
    final filledSegments = (ratio.clamp(0.0, 1.0) * segmentCount).round();

    return Row(
      children: [
        // 레이블 (HP/MP)
        SizedBox(
          width: 28,
          child: Text(
            label,
            style: TextStyle(
              fontFamily: 'PressStart2P',
              fontSize: 14,
              fontWeight: FontWeight.bold,
              color: RetroColors.gold.withValues(alpha: blinkOpacity),
            ),
          ),
        ),
        // 세그먼트 바 (숫자 오버레이)
        Expanded(
          child: Stack(
            alignment: Alignment.center,
            children: [
              // 세그먼트 바
              Container(
                height: 14,
                decoration: BoxDecoration(
                  color: emptyColor.withValues(alpha: 0.3),
                  border: Border.all(
                    color: RetroColors.panelBorderOuter,
                    width: 1,
                  ),
                ),
                child: Row(
                  children: List.generate(segmentCount, (index) {
                    final isFilled = index < filledSegments;
                    return Expanded(
                      child: Container(
                        decoration: BoxDecoration(
                          color: isFilled
                              ? fillColor.withValues(alpha: blinkOpacity)
                              : emptyColor.withValues(alpha: 0.2),
                          border: Border(
                            right: index < segmentCount - 1
                                ? BorderSide(
                                    color: RetroColors.panelBorderOuter
                                        .withValues(alpha: 0.3),
                                    width: 1,
                                  )
                                : BorderSide.none,
                          ),
                        ),
                      ),
                    );
                  }),
                ),
              ),
              // 숫자 오버레이 (바 중앙)
              Text(
                '$current/$max',
                style: TextStyle(
                  fontFamily: 'PressStart2P',
                  fontSize: 12,
                  color: RetroColors.textLight.withValues(alpha: blinkOpacity),
                  shadows: [
                    Shadow(
                      color: Colors.black.withValues(alpha: 0.9),
                      blurRadius: 2,
                    ),
                    const Shadow(color: Colors.black, blurRadius: 4),
                  ],
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }
}
