using UnityEngine;

/// <summary>
/// A single weapon class that can handle all weapon types through configuration
/// </summary>
public class GenericWeapon : Weapon
{
    [Header("Weapon Configuration")]
    public WeaponBehaviorType behaviorType;
    public GameObject arrowPrefab; // For ranged weapons

    private WeaponItem weaponData;

    // Implement abstract properties from Weapon base class
    public override int MinDamage => weaponData?.minDamage ?? 1;
    public override int MaxDamage => weaponData?.maxDamage ?? 1;
    public override int Range => weaponData?.range ?? 1;
    public override int WeaponModifier
    {
        get => weaponData?.weaponModifier ?? 0;
        set => weaponModifier = value; // Set the base class field
    }

    public void InitializeFromItem(WeaponItem item)
    {
        weaponData = item;

        // Set basic weapon properties
        weaponName = item.itemName;
        description = item.description;
        attackSpeed = item.attackSpeed;

        // Determine behavior type from weapon data
        DetermineBehaviorType(item);

        // Initialize based on behavior
        InitializeBehavior(item);

        Debug.Log($"GenericWeapon initialized as {behaviorType} for {item.itemName}");
    }

    private void DetermineBehaviorType(WeaponItem item)
    {
        // Determine behavior based on WeaponType enum or weapon name
        switch (item.weaponType)
        {
            case WeaponType.Fists:
                behaviorType = WeaponBehaviorType.Melee;
                break;
            case WeaponType.Bow:
            case WeaponType.Crossbow:
                behaviorType = WeaponBehaviorType.Ranged;
                if (item.arrowPrefab != null) arrowPrefab = item.arrowPrefab;
                break;
            case WeaponType.Sword:
            case WeaponType.Axe:
            case WeaponType.Mace:
            case WeaponType.Hammer:
            case WeaponType.Spear:
                behaviorType = WeaponBehaviorType.Melee;
                break;
            case WeaponType.Staff:
                behaviorType = WeaponBehaviorType.Magic;
                break;
            case WeaponType.Dagger:
                behaviorType = WeaponBehaviorType.FastMelee;
                break;
            default:
                behaviorType = WeaponBehaviorType.Melee;
                break;
        }
    }

    private void InitializeBehavior(WeaponItem item)
    {
        // Set weapon stats from item data
        // Assuming Weapon base class has these properties
        // You'll need to adjust based on your actual Weapon class structure

        // Example initialization:
        // damage = Random.Range(item.minDamage, item.maxDamage + 1);
        // range = item.range;
        // attackSpeed = item.attackSpeed;

        // Initialize specific behavior patterns
        switch (behaviorType)
        {
            case WeaponBehaviorType.Ranged:
                InitializeRangedWeapon(item);
                break;
            case WeaponBehaviorType.FastMelee:
                InitializeFastMeleeWeapon(item);
                break;
            case WeaponBehaviorType.Magic:
                InitializeMagicWeapon(item);
                break;
            default:
                InitializeMeleeWeapon(item);
                break;
        }
    }

    private void InitializeMeleeWeapon(WeaponItem item)
    {
        // Standard melee weapon setup
        Debug.Log($"Initialized melee weapon: {item.itemName}");
    }

    private void InitializeRangedWeapon(WeaponItem item)
    {
        // Ranged weapon setup
        if (arrowPrefab == null && item.arrowPrefab != null)
        {
            arrowPrefab = item.arrowPrefab;
        }
        Debug.Log($"Initialized ranged weapon: {item.itemName} with arrows: {arrowPrefab != null}");
    }

    private void InitializeFastMeleeWeapon(WeaponItem item)
    {
        // Fast attack weapon setup (daggers, etc.)
        Debug.Log($"Initialized fast melee weapon: {item.itemName}");
    }

    private void InitializeMagicWeapon(WeaponItem item)
    {
        // Magic weapon setup (staffs, etc.)
        Debug.Log($"Initialized magic weapon: {item.itemName}");
    }

    // Override attack method to add behavior-specific logic
    public override void Attack(GameObject target)
    {
        // Call the base attack method first (handles all the standard logic)
        base.Attack(target);

        // Add any behavior-specific effects here if needed
        switch (behaviorType)
        {
            case WeaponBehaviorType.Ranged:
                // Could add special ranged effects here
                Debug.Log($"{weaponData.itemName} fires with ranged behavior!");
                break;
            case WeaponBehaviorType.FastMelee:
                // Could add fast attack effects here
                Debug.Log($"{weaponData.itemName} strikes with fast melee behavior!");
                break;
            case WeaponBehaviorType.Magic:
                // Could add magic effects here
                Debug.Log($"{weaponData.itemName} attacks with magical energy!");
                break;
            default:
                // Standard melee behavior
                Debug.Log($"{weaponData.itemName} performs melee attack!");
                break;
        }
    }
}

[System.Serializable]
public enum WeaponBehaviorType
{
    Melee,
    Ranged,
    FastMelee,
    Magic
}
