using UnityEngine;

[CreateAssetMenu(fileName = "New Weapon", menuName = "RPG/Items/Weapon")]
[System.Serializable]
public class WeaponItem : Item
{
    [Header("Weapon Stats")]
    public int minDamage;
    public int maxDamage;
    public int range;
    public int weaponModifier;
    public float attackSpeed;
    public WeaponType weaponType;

    [Header("Weapon Behavior")]
    public string weaponClassName; // The class name to instantiate (e.g., "SimpleSword", "SimpleBow")
    public GameObject arrowPrefab; // For ranged weapons

    public WeaponItem()
    {
        itemType = ItemType.Weapon;
    }

    /// <summary>
    /// Creates an actual Weapon component instance from this WeaponItem data
    /// This is the updated method that works with the enhanced weapon classes
    /// </summary>
    /// <param name="parent">Parent transform to attach the weapon to</param>
    /// <returns>The instantiated Weapon component</returns>
    public Weapon CreateWeaponInstance(Transform parent)
    {
        GameObject weaponObject = new GameObject(itemName);
        weaponObject.transform.SetParent(parent, false);

        Weapon weaponComponent = null;

        // Create the appropriate weapon component based on weaponClassName
        switch (weaponClassName)
        {
            case "SimpleSword":
                var swordComponent = weaponObject.AddComponent<SimpleSword>();
                swordComponent.InitializeFromItem(this);
                weaponComponent = swordComponent;
                break;
            case "SimpleBow":
                var bowComponent = weaponObject.AddComponent<SimpleBow>();
                bowComponent.arrowPrefab = arrowPrefab;
                bowComponent.InitializeFromItem(this);
                weaponComponent = bowComponent;
                break;
            // Add more weapon types here as you create them
            default:
                Debug.LogError($"Unknown weapon class: {weaponClassName}");
                DestroyImmediate(weaponObject);
                return null;
        }

        if (weaponComponent != null)
        {
            Debug.Log($"Created weapon instance: {itemName}");
        }

        return weaponComponent;
    }

    public override bool MatchesSearch(string searchTerm)
    {
        if (base.MatchesSearch(searchTerm)) return true;

        // Additional weapon-specific search terms
        if (string.IsNullOrEmpty(searchTerm)) return true;

        searchTerm = searchTerm.ToLower();

        // Check weapon type
        if (weaponType.ToString().ToLower().Contains(searchTerm)) return true;

        return false;
    }
}

[System.Serializable]
public enum WeaponType
{
    Sword,
    Bow,
    Crossbow,
    Dagger,
    Mace,
    Staff,
    Spear,
    Axe,
    Hammer
}
