import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../game/models.dart';
import '../../game/enums.dart';
import '../../providers.dart';
import 'battle_animation_widget.dart';
import '../../game/config.dart';
import '../test/sprite_animation_widget.dart'; // Temporary for testing

class CharacterStatusCard extends StatelessWidget {
  final Character character;
  final bool isPlayer;
  final bool isTurn;
  final GlobalKey<BattleAnimationWidgetState>? animationKey;
  final bool hideStats;
  final String? overrideImage;

  const CharacterStatusCard({
    super.key,
    required this.character,
    this.isPlayer = false,
    this.isTurn = false,
    this.animationKey,
    this.hideStats = false,
    this.overrideImage,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        AnimatedOpacity(
          opacity: hideStats ? 0.0 : 1.0,
          duration: AnimationConfig.fadeDuration,
          child: Column(
            children: [
              FittedBox(
                fit: BoxFit.scaleDown,
                child: Text(
                  "Armor: ${character.armor}",
                  style: const TextStyle(color: ThemeConfig.textColorWhite),
                ),
              ),
              FittedBox(
                fit: BoxFit.scaleDown,
                child: Text(
                  "${character.name}: HP ${character.hp}/${character.totalMaxHp}",
                  style: TextStyle(
                    color: character.isDead
                        ? ThemeConfig.statHpEnemyColor
                        : ThemeConfig.textColorWhite,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
              SizedBox(
                width: ThemeConfig.playerImageSize,
                child: LinearProgressIndicator(
                  value: character.totalMaxHp > 0
                      ? character.hp / character.totalMaxHp
                      : 0,
                  color: !isPlayer
                      ? ThemeConfig.statHpEnemyColor
                      : ThemeConfig.statHpPlayerColor,
                  backgroundColor: ThemeConfig.textColorGrey,
                ),
              ),
              if (character.statusEffects.isNotEmpty)
                Padding(
                  padding: const EdgeInsets.only(top: 4.0),
                  child: Wrap(
                    spacing: 4.0,
                    children: character.statusEffects.map((effect) {
                      final isBuff = effect.type == StatusEffectType.attackUp;
                      return Container(
                        padding: const EdgeInsets.symmetric(
                          horizontal: 6,
                          vertical: 2,
                        ),
                        decoration: BoxDecoration(
                          color: isBuff
                              ? ThemeConfig.effectBuffBg
                              : ThemeConfig.effectDebuffBg,
                          borderRadius: BorderRadius.circular(4),
                        ),
                        child: Text(
                          effect.stacks > 1 
                            ? "${effect.type.name.toUpperCase()} x${effect.stacks} (${effect.duration})"
                            : "${effect.type.name.toUpperCase()} (${effect.duration})",
                          style: const TextStyle(
                            color: ThemeConfig.effectText,
                            fontSize: ThemeConfig.statusEffectFontSize,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      );
                    }).toList(),
                  ),
                ),
            ],
          ),
        ),
        const SizedBox(height: 8),
        BattleAnimationWidget(
          key: animationKey,
          child: Container(
            width: isPlayer
                ? ThemeConfig.playerImageSize
                : ThemeConfig.enemyImageSize,
            height: isPlayer
                ? ThemeConfig.playerImageSize
                : ThemeConfig.enemyImageSize,
            clipBehavior: Clip.antiAlias,
            decoration: BoxDecoration(
              // color: isPlayer ? ThemeConfig.playerImageBgColor : null,
              borderRadius: BorderRadius.circular(8),
            ),
            child: Center(
              child:
                  (overrideImage != null ||
                      (character.image != null && character.image!.isNotEmpty))
                  ? OverflowBox(
                      minWidth: 0,
                      maxWidth: double.infinity,
                      minHeight: 0,
                      maxHeight: double.infinity,
                      alignment: Alignment.center,
                      child: SpriteAnimationWidget(
                        // assetPath: 'assets/images/character/Soldier.png',
                        assetPath: overrideImage ?? character.image!,
                        scale: 5.0, // Zoomed in (300x300 in 200x200 box)
                        frameCount: (overrideImage != null &&
                                (overrideImage!.contains("Knight-Hurt") ||
                                    overrideImage!.contains("Knight-Death") ||
                                    overrideImage!.contains("Knight-Block")))
                            ? 4
                            : (overrideImage != null &&
                                    overrideImage!.contains("Knight-Attack01"))
                                ? 7
                                : (overrideImage != null &&
                                        overrideImage!
                                            .contains("Knight-Attack02"))
                                    ? 10
                                    : (overrideImage != null &&
                                            overrideImage!
                                                .contains("Knight-Attack03"))
                                        ? 11
                                        : 6,
                        loop: !(overrideImage != null &&
                            (overrideImage!.contains("Knight-Hurt") ||
                                overrideImage!.contains("Knight-Death") ||
                                overrideImage!.contains("Knight-Block") ||
                                overrideImage!.contains("Knight-Attack"))),
                        flip: !isPlayer,
                        fallbackAssetPath: !isPlayer
                            ? 'assets/images/enemies/Orc.png'
                            : null,
                      ),
                    )
                  : Icon(
                      isPlayer ? Icons.person : Icons.psychology,
                      size: ThemeConfig.characterIconSize,
                      color: ThemeConfig.textColorWhite,
                    ),
            ),
          ),
        ),
        if (!isPlayer && !hideStats)
          Consumer<BattleProvider>(
            builder: (context, provider, child) {
              if (provider.currentEnemyIntent != null && !character.isDead) {
                final intent = provider.currentEnemyIntent!;
                return Padding(
                  padding: const EdgeInsets.only(top: 8.0),
                  child: Container(
                    padding: const EdgeInsets.all(8.0),
                    decoration: BoxDecoration(
                      color: ThemeConfig.enemyIntentBg,
                      borderRadius: BorderRadius.circular(8),
                      border: Border.all(color: ThemeConfig.enemyIntentBorder),
                    ),
                    child: Column(
                      children: [
                        Row(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Icon(
                              intent.type == EnemyActionType.attack
                                  ? Icons.flash_on
                                  : Icons.shield,
                              color: ThemeConfig.rarityRare,
                              size: ThemeConfig.intentIconSize,
                            ),
                            const SizedBox(width: 4),
                            Flexible(
                              child: FittedBox(
                                fit: BoxFit.scaleDown,
                                child: Text(
                                  intent.description,
                                  style: const TextStyle(
                                    color: ThemeConfig.textColorWhite,
                                    fontSize: ThemeConfig.intentFontSize,
                                  ),
                                ),
                              ),
                            ),
                          ],
                        ),
                      ],
                    ),
                  ),
                );
              }
              return const SizedBox.shrink();
            },
          ),
      ],
    );
  }
}
