import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../providers.dart';
import '../../game/models.dart';
import '../../game/enums.dart';
import '../../game/config.dart';
import '../../utils.dart';
import '../common/item_card_widget.dart';

enum InventoryGridMode { normal, shop, equipmentSwap }

class InventoryGridWidget extends StatelessWidget {
  final InventoryGridMode mode;
  final bool equipmentOnly;
  final bool showHeader;
  final int crossAxisCount;
  final EdgeInsetsGeometry gridPadding;
  final double childAspectRatio;

  const InventoryGridWidget({
    super.key,
    this.mode = InventoryGridMode.normal,
    this.equipmentOnly = false,
    this.showHeader = true,
    this.crossAxisCount = 4,
    this.gridPadding = const EdgeInsets.all(16.0),
    this.childAspectRatio = 1.0,
  });

  @override
  Widget build(BuildContext context) {
    return Consumer<BattleProvider>(
      builder: (context, battleProvider, child) {
        final player = battleProvider.player;
        final items = equipmentOnly
            ? player.inventory
                  .where((item) => item.slot != EquipmentSlot.consumable)
                  .toList()
            : player.inventory;
        final itemCount = mode == InventoryGridMode.equipmentSwap
            ? items.length
            : player.maxInventorySize;

        return Column(
          children: [
            if (showHeader)
              Padding(
                padding: const EdgeInsets.symmetric(
                  horizontal: 16.0,
                  vertical: 8.0,
                ),
                child: Align(
                  alignment: Alignment.centerLeft,
                  child: Text(
                    "${equipmentOnly ? AppStrings.equipment : AppStrings.bag} (${items.length}/${player.maxInventorySize})",
                    style: const TextStyle(
                      fontSize: ThemeConfig.fontSizeHeader,
                      fontWeight: ThemeConfig.fontWeightBold,
                    ),
                  ),
                ),
              ),
            Expanded(
              child: itemCount == 0
                  ? const Center(
                      child: Text(
                        "No equipment",
                        style: TextStyle(color: ThemeConfig.textColorGrey),
                      ),
                    )
                  : GridView.builder(
                      padding: gridPadding,
                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: crossAxisCount,
                        crossAxisSpacing: 8.0,
                        mainAxisSpacing: 8.0,
                        childAspectRatio: childAspectRatio,
                      ),
                      itemCount: itemCount,
                      itemBuilder: (context, index) {
                        if (index < items.length) {
                          final item = items[index];
                          return InkWell(
                            onTap: () {
                              if (mode == InventoryGridMode.equipmentSwap) {
                                _showEquipSlotDialog(
                                  context,
                                  battleProvider,
                                  item,
                                );
                              } else {
                                _showItemActionDialog(
                                  context,
                                  battleProvider,
                                  item,
                                );
                              }
                            },
                            child: ItemCardWidget(
                              item: item,
                              showPrice: false,
                              canBuy: false,
                              compact: mode == InventoryGridMode.equipmentSwap,
                            ),
                          );
                        } else {
                          return Container(
                            decoration: BoxDecoration(
                              border: Border.all(
                                color: ThemeConfig.textColorGrey,
                              ),
                              color: ThemeConfig.emptySlotBg,
                            ),
                            child: const Center(
                              child: Icon(
                                Icons.add_box,
                                color: ThemeConfig.textColorGrey,
                              ),
                            ),
                          );
                        }
                      },
                    ),
            ),
          ],
        );
      },
    );
  }

  void _showItemActionDialog(
    BuildContext context,
    BattleProvider provider,
    Item item,
  ) {
    final isShop =
        mode == InventoryGridMode.shop ||
        (mode == InventoryGridMode.normal &&
            provider.currentStage.type == StageType.shop);
    final isEquipmentSwap = mode == InventoryGridMode.equipmentSwap;
    int sellPrice = (item.price * GameConfig.sellPriceMultiplier).floor();

    showDialog(
      context: context,
      builder: (ctx) => SimpleDialog(
        title: Text("${item.name} Actions"),
        children: [
          if (item.slot == EquipmentSlot.consumable)
            SimpleDialogOption(
              onPressed: () {
                Navigator.pop(ctx);
                provider.useConsumable(item);
              },
              child: const Padding(
                padding: EdgeInsets.symmetric(vertical: 8.0),
                child: Row(
                  children: [
                    Icon(Icons.science, color: ThemeConfig.btnActionActive),
                    SizedBox(width: 10),
                    Text("Use"),
                  ],
                ),
              ),
            )
          else
            SimpleDialogOption(
              onPressed: () {
                Navigator.pop(ctx);
                _showEquipSlotDialog(context, provider, item);
              },
              child: const Padding(
                padding: EdgeInsets.symmetric(vertical: 8.0),
                child: Row(
                  children: [
                    Icon(Icons.shield, color: ThemeConfig.btnDefendActive),
                    SizedBox(width: 10),
                    Text(AppStrings.equip),
                  ],
                ),
              ),
            ),
          if (isShop)
            SimpleDialogOption(
              onPressed: () {
                Navigator.pop(ctx);
                _showSellConfirmationDialog(context, provider, item);
              },
              child: Padding(
                padding: const EdgeInsets.symmetric(vertical: 8.0),
                child: Row(
                  children: [
                    const Icon(
                      Icons.attach_money,
                      color: ThemeConfig.statGoldColor,
                    ),
                    const SizedBox(width: 10),
                    Text("${AppStrings.sell} ($sellPrice G)"),
                  ],
                ),
              ),
            ),
          if (!isEquipmentSwap)
            SimpleDialogOption(
              onPressed: () {
                Navigator.pop(ctx);
                _showDiscardConfirmationDialog(context, provider, item);
              },
              child: const Padding(
                padding: EdgeInsets.symmetric(vertical: 8.0),
                child: Row(
                  children: [
                    Icon(Icons.delete, color: ThemeConfig.btnActionActive),
                    SizedBox(width: 10),
                    Text(AppStrings.discard),
                  ],
                ),
              ),
            ),
        ],
      ),
    );
  }

  void _showEquipSlotDialog(
    BuildContext context,
    BattleProvider provider,
    Item newItem,
  ) {
    final compatibleSlots = newItem.compatibleEquipSlots;
    if (compatibleSlots.isEmpty) return;

    if (compatibleSlots.length == 1) {
      _showEquipConfirmationDialog(
        context,
        provider,
        newItem,
        compatibleSlots.first,
      );
      return;
    }

    showDialog(
      context: context,
      builder: (ctx) => SimpleDialog(
        title: Text("Equip ${newItem.name}"),
        children: compatibleSlots
            .map(
              (slot) => SimpleDialogOption(
                onPressed: () {
                  Navigator.pop(ctx);
                  _showEquipConfirmationDialog(
                    context,
                    provider,
                    newItem,
                    slot,
                  );
                },
                child: Padding(
                  padding: const EdgeInsets.symmetric(vertical: 8.0),
                  child: Row(
                    children: [
                      Image.asset(
                        ItemUtils.getIconPath(slot),
                        width: ThemeConfig.itemIconSizeMedium,
                        height: ThemeConfig.itemIconSizeMedium,
                        color: ThemeConfig.textColorWhite,
                      ),
                      const SizedBox(width: 10),
                      Text(ItemUtils.getSlotName(slot)),
                    ],
                  ),
                ),
              ),
            )
            .toList(),
      ),
    );
  }

  void _showSellConfirmationDialog(
    BuildContext context,
    BattleProvider provider,
    Item item,
  ) {
    int sellPrice = (item.price * GameConfig.sellPriceMultiplier).floor();

    showDialog(
      context: context,
      builder: (ctx) => AlertDialog(
        title: const Text("Sell Item"),
        content: Text("Sell ${item.name} for $sellPrice G?"),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(ctx),
            child: const Text(AppStrings.cancel),
          ),
          ElevatedButton(
            style: ElevatedButton.styleFrom(
              backgroundColor: ThemeConfig.statGoldColor,
            ),
            onPressed: () {
              provider.sellItem(item);
              Navigator.pop(ctx);
            },
            child: const Text(
              AppStrings.sell,
              style: TextStyle(color: Colors.black),
            ),
          ),
        ],
      ),
    );
  }

  void _showDiscardConfirmationDialog(
    BuildContext context,
    BattleProvider provider,
    Item item,
  ) {
    showDialog(
      context: context,
      builder: (ctx) => AlertDialog(
        title: const Text("Discard Item"),
        content: Text("Are you sure you want to discard ${item.name}?"),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(ctx),
            child: const Text(AppStrings.cancel),
          ),
          ElevatedButton(
            style: ElevatedButton.styleFrom(
              backgroundColor: ThemeConfig.btnActionActive,
            ),
            onPressed: () {
              provider.discardItem(item);
              Navigator.pop(ctx);
            },
            child: const Text(AppStrings.discard),
          ),
        ],
      ),
    );
  }

  void _showEquipConfirmationDialog(
    BuildContext context,
    BattleProvider provider,
    Item newItem,
    EquipmentSlot targetSlot,
  ) {
    final player = provider.player;
    final oldItem = player.equipment[targetSlot];

    final currentMaxHp = player.totalMaxHp;
    final currentAtk = player.totalAtk;
    final currentDef = player.totalDefense;
    final currentHp = player.hp;

    int newMaxHp = currentMaxHp - (oldItem?.hpBonus ?? 0) + newItem.hpBonus;
    int newAtk = currentAtk - (oldItem?.atkBonus ?? 0) + newItem.atkBonus;
    int newDef = currentDef - (oldItem?.armorBonus ?? 0) + newItem.armorBonus;

    double ratio = currentMaxHp > 0 ? currentHp / currentMaxHp : 0.0;
    int newHp = (newMaxHp * ratio).toInt();
    if (newHp < 0) newHp = 0;
    if (newHp > newMaxHp) newHp = newMaxHp;

    showDialog(
      context: context,
      builder: (ctx) => AlertDialog(
        title: const Text("Change Equipment"),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(
              "${AppStrings.equip} ${newItem.name} as ${ItemUtils.getSlotName(targetSlot)}?",
              style: const TextStyle(fontWeight: ThemeConfig.fontWeightBold),
            ),
            if (oldItem != null)
              Text(
                "Replaces ${oldItem.name}",
                style: const TextStyle(
                  fontSize: 12,
                  color: ThemeConfig.textColorGrey,
                ),
              ),
            const SizedBox(height: 16),
            _buildStatChangeRow("Max HP", currentMaxHp, newMaxHp),
            _buildStatChangeRow("Current HP", currentHp, newHp),
            _buildStatChangeRow(AppStrings.atk, currentAtk, newAtk),
            _buildStatChangeRow(AppStrings.def, currentDef, newDef),
            _buildStatChangeRow(
              "LUCK",
              player.totalLuck,
              player.totalLuck - (oldItem?.luck ?? 0) + newItem.luck,
            ),
            _buildStatChangeRow(
              "Dodge",
              player.totalDodge,
              player.totalDodge - (oldItem?.dodge ?? 0) + newItem.dodge,
            ),
            if (newItem.effects.isNotEmpty || (oldItem != null && oldItem.effects.isNotEmpty)) ...[
              const Divider(color: ThemeConfig.textColorGrey, height: 16),
              if (oldItem != null && oldItem.effects.isNotEmpty)
                ...oldItem.effects.map((e) => Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        const Text("- ", style: TextStyle(color: ThemeConfig.textColorGrey, fontSize: 12)),
                        Expanded(child: Text(e.description, style: const TextStyle(color: ThemeConfig.textColorGrey, fontSize: 12))),
                      ],
                    )),
              if (newItem.effects.isNotEmpty)
                ...newItem.effects.map((e) => Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        const Text("+ ", style: TextStyle(color: ThemeConfig.rarityLegendary, fontSize: 12, fontWeight: FontWeight.bold)),
                        Expanded(child: Text(e.description, style: const TextStyle(color: ThemeConfig.rarityLegendary, fontSize: 12, fontWeight: FontWeight.bold))),
                      ],
                    )),
            ],
          ],
        ),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(ctx),
            child: const Text(AppStrings.cancel),
          ),
          ElevatedButton(
            onPressed: () {
              provider.equipItem(newItem, targetSlot: targetSlot);
              Navigator.pop(ctx);
            },
            child: const Text(AppStrings.confirm),
          ),
        ],
      ),
    );
  }

  Widget _buildStatChangeRow(String label, int oldVal, int newVal) {
    int diff = newVal - oldVal;
    Color color = diff > 0
        ? ThemeConfig.statDiffPositive
        : (diff < 0
              ? ThemeConfig.statDiffNegative
              : ThemeConfig.statDiffNeutral);
    String diffText = diff > 0 ? "(+$diff)" : (diff < 0 ? "($diff)" : "");

    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 4.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(label),
          Row(
            children: [
              Text(
                "$oldVal",
                style: const TextStyle(color: ThemeConfig.textColorGrey),
              ),
              const Icon(
                Icons.arrow_right,
                size: 16,
                color: ThemeConfig.textColorGrey,
              ),
              Text(
                "$newVal",
                style: const TextStyle(fontWeight: ThemeConfig.fontWeightBold),
              ),
              const SizedBox(width: 4),
              Text(
                diffText,
                style: TextStyle(
                  color: color,
                  fontSize: 12,
                  fontWeight: ThemeConfig.fontWeightBold,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
