import 'package:flutter/material.dart';

import 'package:asciineverdie/src/shared/retro_colors.dart';

/// 몬스터 HP 바 (레트로 세그먼트 스타일)
///
/// 데스크탑 전용 세그먼트 바. HP% 중앙 오버레이 + 레벨.이름 표시.
class RetroMonsterHpBar extends StatelessWidget {
  const RetroMonsterHpBar({
    super.key,
    required this.monsterHpCurrent,
    required this.monsterHpMax,
    required this.monsterName,
    required this.monsterLevel,
    required this.flashAnimation,
    required this.monsterHpChange,
  });

  final int monsterHpCurrent;
  final int monsterHpMax;
  final String? monsterName;
  final int? monsterLevel;
  final Animation<double> flashAnimation;
  final int monsterHpChange;

  @override
  Widget build(BuildContext context) {
    final ratio = monsterHpMax > 0 ? monsterHpCurrent / monsterHpMax : 0.0;
    const segmentCount = 10;
    final filledSegments = (ratio.clamp(0.0, 1.0) * segmentCount).round();
    final levelPrefix = monsterLevel != null ? 'Lv.$monsterLevel ' : '';
    final name = monsterName ?? '';

    return AnimatedBuilder(
      animation: flashAnimation,
      builder: (context, child) {
        final flashColor = RetroColors.gold.withValues(
          alpha: flashAnimation.value * 0.3,
        );

        return Container(
          padding: const EdgeInsets.all(4),
          decoration: BoxDecoration(
            color: flashAnimation.value > 0.1
                ? flashColor
                : RetroColors.panelBgLight.withValues(alpha: 0.5),
            border: Border.all(
              color: RetroColors.gold.withValues(alpha: 0.6),
              width: 1,
            ),
          ),
          child: Stack(
            clipBehavior: Clip.none,
            children: [
              Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  // HP 바 (HP% 중앙 오버레이)
                  Stack(
                    alignment: Alignment.center,
                    children: [
                      _buildSegmentBar(segmentCount, filledSegments),
                      // HP% 중앙 오버레이
                      Text(
                        '${(ratio * 100).toInt()}%',
                        style: TextStyle(
                          fontFamily: 'PressStart2P',
                          fontSize: 13,
                          color: RetroColors.textLight,
                          shadows: [
                            Shadow(
                              color: Colors.black.withValues(alpha: 0.8),
                              blurRadius: 2,
                            ),
                            const Shadow(color: Colors.black, blurRadius: 4),
                          ],
                        ),
                      ),
                    ],
                  ),
                  const SizedBox(height: 4),
                  // 레벨.이름 표시
                  Text(
                    '$levelPrefix$name',
                    style: const TextStyle(
                      fontFamily: 'PressStart2P',
                      fontSize: 12,
                      color: RetroColors.gold,
                    ),
                    overflow: TextOverflow.ellipsis,
                    maxLines: 1,
                  ),
                ],
              ),

              // 플로팅 데미지 텍스트
              if (monsterHpChange != 0 && flashAnimation.value > 0.05)
                Positioned(
                  right: 50,
                  top: -8,
                  child: Transform.translate(
                    offset: Offset(0, -12 * (1 - flashAnimation.value)),
                    child: Opacity(
                      opacity: flashAnimation.value,
                      child: Text(
                        monsterHpChange > 0
                            ? '+$monsterHpChange'
                            : '$monsterHpChange',
                        style: TextStyle(
                          fontFamily: 'PressStart2P',
                          fontSize: 14,
                          fontWeight: FontWeight.bold,
                          color: monsterHpChange < 0
                              ? RetroColors.gold
                              : RetroColors.expGreen,
                          shadows: const [
                            Shadow(color: Colors.black, blurRadius: 3),
                            Shadow(color: Colors.black, blurRadius: 6),
                          ],
                        ),
                      ),
                    ),
                  ),
                ),
            ],
          ),
        );
      },
    );
  }

  /// 세그먼트 HP 바
  Widget _buildSegmentBar(int segmentCount, int filledSegments) {
    return Container(
      height: 12,
      decoration: BoxDecoration(
        color: RetroColors.hpRedDark.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
                    ? RetroColors.gold
                    : RetroColors.panelBorderOuter.withValues(alpha: 0.3),
                border: Border(
                  right: index < segmentCount - 1
                      ? BorderSide(
                          color: RetroColors.panelBorderOuter.withValues(
                            alpha: 0.3,
                          ),
                          width: 1,
                        )
                      : BorderSide.none,
                ),
              ),
            ),
          );
        }),
      ),
    );
  }
}
