import 'package:flutter/material.dart';
import '../game/models.dart';
import '../game/data.dart';
import '../game/enums.dart';
import '../game/config.dart';

class ShopProvider with ChangeNotifier {
  List<Item> availableItems = [];
  String _lastShopMessage = '';

  String get lastShopMessage => _lastShopMessage;

  void clearMessage() {
    _lastShopMessage = '';
    notifyListeners();
  }

  void generateShopItems(int stage) {
    ItemTier currentTier = ItemTier.tier1;
    if (stage > GameConfig.tier2StageMax) {
      currentTier = ItemTier.tier3;
    } else if (stage > GameConfig.tier1StageMax)
      currentTier = ItemTier.tier2;

    availableItems = [];
    availableItems = [];

    // 1. Generate 4 Random Equipment Items
    for (int i = 0; i < 4; i++) {
      // Exclude consumables from this pool if getRandomItem includes them by default (it does if we don't filter)
      // We need to implement slot exclusion or explicit slot inclusion in getRandomItem?
      // Or simply cycle slots?
      // ItemTable.getRandomItem picks from allItems which now includes consumables.
      // We should add filtering to getRandomItem logic OR filter here.
      // Let's filter here by retrying or explicitly asking for non-consumables.
      // Actually, ItemTable.getRandomItem accepts 'slot'. But we want ANY equipment.
      // Let's rely on type checking or add 'excludeSlot' to getRandomItem (too much change).
      // Simpler: Just pick random, if consumable, reroll? Or better:

      // Let's update getRandomItem to support multiple allowed slots? No.
      // Let's just pick strictly by slot rotation or random filtering.
      // Let's try simple filtering loop.

      while (true) {
        ItemTemplate? template = ItemTable.getRandomItem(tier: currentTier);
        if (template != null && template.slot != EquipmentSlot.consumable) {
          availableItems.add(template.createItem(stage: stage));
          break;
        }
      }
    }

    // 2. Generate 2 Random Consumables
    // Consumables might always be Tier 1 for now, or match current tier?
    // Let's match current tier (though we only defined Tier 1 potions).
    // If no potions at current tier, fallback to Tier 1?
    // ItemTable.consumables currently only has items.
    // Let's just pick from ItemTable.consumables directly for simplicity and safety.

    if (ItemTable.consumables.isNotEmpty) {
      for (int i = 0; i < 2; i++) {
        ItemTemplate? consTemplate = ItemTable.getRandomItem(
          tier: ItemTier.tier1, // Potions are Tier 1 for now
          slot: EquipmentSlot.consumable,
        );

        if (consTemplate != null) {
          availableItems.add(consTemplate.createItem(stage: stage));
        }
      }
    }
    notifyListeners();
  }

  bool rerollShopItems(Character player, int currentStageNumber) {
    const int rerollCost = GameConfig.shopRerollCost;
    if (player.gold >= rerollCost) {
      player.gold -= rerollCost;
      generateShopItems(
        currentStageNumber,
      ); // Regenerate based on current stage
      _lastShopMessage = "Shop items rerolled for $rerollCost G.";
      notifyListeners();
      return true;
    } else {
      _lastShopMessage = "Not enough gold to reroll!";
      notifyListeners();
      return false;
    }
  }

  bool buyItem(Item item, Character player) {
    if (player.gold >= item.price) {
      if (player.inventory.length < player.maxInventorySize) {
        player.gold -= item.price;
        player.addToInventory(item);
        availableItems.remove(item); // Remove from shop
        _lastShopMessage = "Bought ${item.name} for ${item.price} G.";
        notifyListeners();
        return true;
      } else {
        _lastShopMessage = "Inventory is full! Cannot buy ${item.name}.";
        notifyListeners();
        return false;
      }
    } else {
      _lastShopMessage = "Not enough gold!";
      notifyListeners();
      return false;
    }
  }
}
