import 'package:flutter/material.dart';
import '../../game/enums.dart';
import '../../game/models.dart';
import '../../game/config.dart';
import '../../providers/battle_provider.dart';
import '../../utils/item_utils.dart';
import '../inventory/item_stat_widget.dart';
import '../test/sprite_animation_widget.dart';
import '../../screens/main_menu_screen.dart';

class BattleRewardOverlay extends StatefulWidget {
  final BattleProvider battleProvider;

  const BattleRewardOverlay({super.key, required this.battleProvider});

  @override
  State<BattleRewardOverlay> createState() => _BattleRewardOverlayState();
}

class _BattleRewardOverlayState extends State<BattleRewardOverlay> {
  bool _isCompletingReward = false;

  Future<void> _selectReward(Item item) async {
    if (_isCompletingReward) return;
    setState(() => _isCompletingReward = true);

    widget.battleProvider.selectReward(item);

    if (mounted) {
      setState(() => _isCompletingReward = false);
    }
  }

  @override
  Widget build(BuildContext context) {
    final battleProvider = widget.battleProvider;
    return Container(
      color: ThemeConfig.cardBgColor,
      child: Center(
        child: SimpleDialog(
          title: Row(
            children: [
              const Text(
                "${AppStrings.victory} ${AppStrings.chooseReward}",
              ),
              const Spacer(),
              Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Icon(
                    Icons.monetization_on,
                    color: ThemeConfig.statGoldColor,
                    size: ThemeConfig.itemIconSizeSmall,
                  ),
                  const SizedBox(width: 4),
                  Text(
                    "${battleProvider.lastGoldReward} G",
                    style: TextStyle(
                      color: ThemeConfig.statGoldColor,
                      fontSize: ThemeConfig.fontSizeBody,
                      fontWeight: ThemeConfig.fontWeightBold,
                    ),
                  ),
                ],
              ),
            ],
          ),
          children: battleProvider.rewardOptions.map((item) {
            bool isSkip = item.id == "reward_skip";
            return SimpleDialogOption(
              onPressed: _isCompletingReward ? null : () => _selectReward(item),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      if (!isSkip)
                        Container(
                          padding: const EdgeInsets.all(4),
                          decoration: BoxDecoration(
                            color: ThemeConfig.rewardItemBg,
                            borderRadius: BorderRadius.circular(4),
                            border: Border.all(
                              color: item.rarity != ItemRarity.magic
                                  ? ItemUtils.getRarityColor(item.rarity)
                                  : ThemeConfig.rarityCommon,
                            ),
                          ),
                          child: Image.asset(
                            ItemUtils.getIconPath(item.slot),
                            width: ThemeConfig.itemIconSizeMedium,
                            height: ThemeConfig.itemIconSizeMedium,
                            fit: BoxFit.contain,
                            filterQuality: FilterQuality.high,
                          ),
                        ),
                      if (!isSkip) const SizedBox(width: 12),
                      Text(
                        item.name,
                        style: TextStyle(
                          fontWeight: ThemeConfig.fontWeightBold,
                          fontSize: ThemeConfig.fontSizeLarge,
                          color: isSkip
                              ? ThemeConfig.textColorGrey
                              : ItemUtils.getRarityColor(item.rarity),
                        ),
                      ),
                    ],
                  ),
                  if (!isSkip) ItemStatWidget(item: item),
                  Text(
                    item.description,
                    style: const TextStyle(
                      fontSize: ThemeConfig.fontSizeMedium,
                      color: ThemeConfig.textColorGrey,
                    ),
                  ),
                ],
              ),
            );
          }).toList(),
        ),
      ),
    );
  }
}

class BattleDefeatOverlay extends StatelessWidget {
  const BattleDefeatOverlay({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      color: ThemeConfig.battleBg,
      child: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            const SpriteAnimationWidget(
              assetPath: 'assets/images/character/Knight-Death.png',
              frameCount: 4,
              scale: 4.0,
              loop: false,
              customDuration: Duration(milliseconds: 1500),
            ),
            const SizedBox(height: 16),
            const Text(
              AppStrings.defeat,
              style: TextStyle(
                color: ThemeConfig.statHpColor,
                fontSize: ThemeConfig.fontSizeHuge,
                fontWeight: ThemeConfig.fontWeightBold,
                letterSpacing: ThemeConfig.letterSpacingHeader,
              ),
            ),
            const SizedBox(height: 32),
            ElevatedButton(
              style: ElevatedButton.styleFrom(
                backgroundColor: ThemeConfig.menuButtonBg,
                padding: const EdgeInsets.symmetric(
                  horizontal: ThemeConfig.paddingBtnHorizontal,
                  vertical: ThemeConfig.paddingBtnVertical,
                ),
              ),
              onPressed: () {
                Navigator.of(context).pushAndRemoveUntil(
                  MaterialPageRoute(
                    builder: (context) => const MainMenuScreen(),
                  ),
                  (route) => false,
                );
              },
              child: const Text(
                AppStrings.returnToMenu,
                style: TextStyle(
                  color: ThemeConfig.textColorWhite,
                  fontSize: ThemeConfig.fontSizeHeader,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
