import 'package:flutter/material.dart';

import 'package:asciineverdie/data/game_text_l10n.dart' as l10n;
import 'package:asciineverdie/src/core/model/combat_state.dart';

/// 컴팩트 HP 바 (숫자 오버레이 포함)
class CompactHpBar extends StatelessWidget {
  const CompactHpBar({
    super.key,
    required this.current,
    required this.max,
    required this.flashAnimation,
    required this.hpChange,
  });

  final int current;
  final int max;
  final Animation<double> flashAnimation;
  final int hpChange;

  @override
  Widget build(BuildContext context) {
    final ratio = max > 0 ? current / max : 0.0;
    final isLow = ratio < 0.2 && ratio > 0;

    return AnimatedBuilder(
      animation: flashAnimation,
      builder: (context, child) {
        return Stack(
          clipBehavior: Clip.none,
          children: [
            Container(
              decoration: BoxDecoration(
                color: isLow
                    ? Colors.red.withValues(alpha: 0.2)
                    : Colors.grey.shade800,
                borderRadius: BorderRadius.circular(3),
              ),
              child: Row(
                children: [
                  Container(
                    width: 32,
                    alignment: Alignment.center,
                    child: Text(
                      l10n.statHp,
                      style: const TextStyle(
                        fontSize: 11,
                        fontWeight: FontWeight.bold,
                        color: Colors.white70,
                      ),
                    ),
                  ),
                  Expanded(
                    child: Stack(
                      alignment: Alignment.center,
                      children: [
                        ClipRRect(
                          borderRadius: const BorderRadius.horizontal(
                            right: Radius.circular(3),
                          ),
                          child: SizedBox.expand(
                            child: LinearProgressIndicator(
                              value: ratio.clamp(0.0, 1.0),
                              backgroundColor: Colors.red.withValues(
                                alpha: 0.2,
                              ),
                              valueColor: AlwaysStoppedAnimation(
                                isLow ? Colors.red : Colors.red.shade600,
                              ),
                            ),
                          ),
                        ),
                        Text(
                          '$current/$max',
                          style: TextStyle(
                            fontSize: 11,
                            fontWeight: FontWeight.bold,
                            color: Colors.white,
                            shadows: [
                              Shadow(
                                color: Colors.black.withValues(alpha: 0.9),
                                blurRadius: 2,
                              ),
                              const Shadow(color: Colors.black, blurRadius: 4),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            if (hpChange != 0 && flashAnimation.value > 0.05)
              Positioned(
                right: 20,
                top: -8,
                child: Transform.translate(
                  offset: Offset(0, -10 * (1 - flashAnimation.value)),
                  child: Opacity(
                    opacity: flashAnimation.value,
                    child: Text(
                      hpChange > 0 ? '+$hpChange' : '$hpChange',
                      style: TextStyle(
                        fontSize: 13,
                        fontWeight: FontWeight.bold,
                        color: hpChange < 0 ? Colors.red : Colors.green,
                        shadows: const [
                          Shadow(color: Colors.black, blurRadius: 3),
                        ],
                      ),
                    ),
                  ),
                ),
              ),
          ],
        );
      },
    );
  }
}

/// 컴팩트 MP 바 (숫자 오버레이 포함)
class CompactMpBar extends StatelessWidget {
  const CompactMpBar({
    super.key,
    required this.current,
    required this.max,
    required this.flashAnimation,
    required this.mpChange,
  });

  final int current;
  final int max;
  final Animation<double> flashAnimation;
  final int mpChange;

  @override
  Widget build(BuildContext context) {
    final ratio = max > 0 ? current / max : 0.0;

    return AnimatedBuilder(
      animation: flashAnimation,
      builder: (context, child) {
        return Stack(
          clipBehavior: Clip.none,
          children: [
            Container(
              decoration: BoxDecoration(
                color: Colors.grey.shade800,
                borderRadius: BorderRadius.circular(3),
              ),
              child: Row(
                children: [
                  Container(
                    width: 32,
                    alignment: Alignment.center,
                    child: Text(
                      l10n.statMp,
                      style: const TextStyle(
                        fontSize: 11,
                        fontWeight: FontWeight.bold,
                        color: Colors.white70,
                      ),
                    ),
                  ),
                  Expanded(
                    child: Stack(
                      alignment: Alignment.center,
                      children: [
                        ClipRRect(
                          borderRadius: const BorderRadius.horizontal(
                            right: Radius.circular(3),
                          ),
                          child: SizedBox.expand(
                            child: LinearProgressIndicator(
                              value: ratio.clamp(0.0, 1.0),
                              backgroundColor: Colors.blue.withValues(
                                alpha: 0.2,
                              ),
                              valueColor: AlwaysStoppedAnimation(
                                Colors.blue.shade600,
                              ),
                            ),
                          ),
                        ),
                        Text(
                          '$current/$max',
                          style: TextStyle(
                            fontSize: 11,
                            fontWeight: FontWeight.bold,
                            color: Colors.white,
                            shadows: [
                              Shadow(
                                color: Colors.black.withValues(alpha: 0.9),
                                blurRadius: 2,
                              ),
                              const Shadow(color: Colors.black, blurRadius: 4),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            if (mpChange != 0 && flashAnimation.value > 0.05)
              Positioned(
                right: 20,
                top: -8,
                child: Transform.translate(
                  offset: Offset(0, -10 * (1 - flashAnimation.value)),
                  child: Opacity(
                    opacity: flashAnimation.value,
                    child: Text(
                      mpChange > 0 ? '+$mpChange' : '$mpChange',
                      style: TextStyle(
                        fontSize: 13,
                        fontWeight: FontWeight.bold,
                        color: mpChange < 0 ? Colors.orange : Colors.cyan,
                        shadows: const [
                          Shadow(color: Colors.black, blurRadius: 3),
                        ],
                      ),
                    ),
                  ),
                ),
              ),
          ],
        );
      },
    );
  }
}

/// 몬스터 HP 바 (전투 중)
class CompactMonsterHpBar extends StatelessWidget {
  const CompactMonsterHpBar({
    super.key,
    required this.combat,
    required this.monsterHpCurrent,
    required this.monsterHpMax,
    required this.monsterLevel,
    required this.flashAnimation,
    required this.monsterHpChange,
  });

  final CombatState combat;
  final int? monsterHpCurrent;
  final int? monsterHpMax;
  final int? monsterLevel;
  final Animation<double> flashAnimation;
  final int monsterHpChange;

  @override
  Widget build(BuildContext context) {
    final max = monsterHpMax ?? 1;
    final current = monsterHpCurrent ?? 0;
    final ratio = max > 0 ? current / max : 0.0;
    final monsterName = combat.monsterStats.name;
    final level = monsterLevel ?? combat.monsterStats.level;

    return AnimatedBuilder(
      animation: flashAnimation,
      builder: (context, child) {
        return Stack(
          clipBehavior: Clip.none,
          children: [
            Container(
              decoration: BoxDecoration(
                color: Colors.orange.withValues(alpha: 0.1),
                borderRadius: BorderRadius.circular(4),
                border: Border.all(color: Colors.orange.withValues(alpha: 0.3)),
              ),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Expanded(
                    child: Padding(
                      padding: const EdgeInsets.fromLTRB(4, 4, 4, 2),
                      child: Stack(
                        alignment: Alignment.center,
                        children: [
                          ClipRRect(
                            borderRadius: BorderRadius.circular(2),
                            child: SizedBox.expand(
                              child: LinearProgressIndicator(
                                value: ratio.clamp(0.0, 1.0),
                                backgroundColor: Colors.orange.withValues(
                                  alpha: 0.2,
                                ),
                                valueColor: const AlwaysStoppedAnimation(
                                  Colors.orange,
                                ),
                              ),
                            ),
                          ),
                          Text(
                            '${(ratio * 100).toInt()}%',
                            style: TextStyle(
                              fontSize: 12,
                              fontWeight: FontWeight.bold,
                              color: Colors.white,
                              shadows: [
                                Shadow(
                                  color: Colors.black.withValues(alpha: 0.8),
                                  blurRadius: 2,
                                ),
                                const Shadow(
                                  color: Colors.black,
                                  blurRadius: 4,
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.fromLTRB(4, 0, 4, 4),
                    child: Text(
                      'Lv.$level $monsterName',
                      style: const TextStyle(
                        fontSize: 11,
                        color: Colors.orange,
                        fontWeight: FontWeight.bold,
                      ),
                      overflow: TextOverflow.ellipsis,
                      maxLines: 1,
                    ),
                  ),
                ],
              ),
            ),
            if (monsterHpChange != 0 && flashAnimation.value > 0.05)
              Positioned(
                right: 10,
                top: -10,
                child: Transform.translate(
                  offset: Offset(0, -10 * (1 - flashAnimation.value)),
                  child: Opacity(
                    opacity: flashAnimation.value,
                    child: Text(
                      monsterHpChange > 0
                          ? '+$monsterHpChange'
                          : '$monsterHpChange',
                      style: TextStyle(
                        fontSize: 17,
                        fontWeight: FontWeight.bold,
                        color: monsterHpChange < 0
                            ? Colors.yellow
                            : Colors.green,
                        shadows: const [
                          Shadow(color: Colors.black, blurRadius: 3),
                        ],
                      ),
                    ),
                  ),
                ),
              ),
          ],
        );
      },
    );
  }
}
