import 'dart:convert';
import 'package:flutter/services.dart';
import '../model/item.dart';
import '../enums.dart';

class ItemTemplate {
  final String id;
  final String name;
  final String description;
  final int atkBonus;
  final int hpBonus;
  final int armorBonus;
  final EquipmentSlot slot;
  final List<ItemEffect> effects;
  final int price;
  final String? image;
  final int luck;

  const ItemTemplate({
    required this.id,
    required this.name,
    required this.description,
    required this.atkBonus,
    required this.hpBonus,
    required this.armorBonus,
    required this.slot,
    required this.effects,
    required this.price,
    this.image,
    this.luck = 0,
  });

  factory ItemTemplate.fromJson(Map<String, dynamic> json) {
    var effectsList = <ItemEffect>[];
    if (json['effects'] != null) {
      effectsList = (json['effects'] as List)
          .map((e) => ItemEffect.fromJson(e))
          .toList();
    }

    return ItemTemplate(
      id: json['id'],
      name: json['name'],
      description: json['description'],
      atkBonus: json['atkBonus'] ?? 0,
      hpBonus: json['hpBonus'] ?? 0,
      armorBonus: json['armorBonus'] ?? 0,
      slot: EquipmentSlot.values.firstWhere((e) => e.name == json['slot']),
      effects: effectsList,
      price: json['price'] ?? 10,
      image: json['image'],
      luck: json['luck'] ?? 0,
    );
  }

  Item createItem({int stage = 1}) {
    // Scale stats based on stage
    int scaledAtk = (atkBonus * (1 + (stage - 1) * 0.1)).toInt();
    int scaledHp = (hpBonus * (1 + (stage - 1) * 0.1)).toInt();
    int scaledArmor = (armorBonus * (1 + (stage - 1) * 0.1)).toInt();

    return Item(
      id: id,
      name: "$name${stage > 1 ? ' +${stage - 1}' : ''}",
      description: description,
      atkBonus: scaledAtk,
      hpBonus: scaledHp,
      armorBonus: scaledArmor,
      slot: slot,
      effects: effects,
      price: price,
      image: image,
      luck: luck,
    );
  }
}

class ItemTable {
  static List<ItemTemplate> weapons = [];
  static List<ItemTemplate> armors = [];
  static List<ItemTemplate> shields = [];
  static List<ItemTemplate> accessories = [];

  static Future<void> load() async {
    final String jsonString = await rootBundle.loadString(
      'assets/data/items.json',
    );
    final Map<String, dynamic> data = jsonDecode(jsonString);

    weapons = (data['weapons'] as List)
        .map((e) => ItemTemplate.fromJson(e))
        .toList();
    armors = (data['armors'] as List)
        .map((e) => ItemTemplate.fromJson(e))
        .toList();
    shields = (data['shields'] as List)
        .map((e) => ItemTemplate.fromJson(e))
        .toList();
    accessories = (data['accessories'] as List)
        .map((e) => ItemTemplate.fromJson(e))
        .toList();
  }

  static List<ItemTemplate> get allItems => [
    ...weapons,
    ...armors,
    ...shields,
    ...accessories,
  ];

  static ItemTemplate? get(String id) {
    try {
      return allItems.firstWhere((item) => item.id == id);
    } catch (e) {
      return null;
    }
  }
}
