using UnityEngine;

/// <summary>
/// Miscellaneous items with dice-based restoration effects
/// 
/// EXAMPLES:
/// Small Health Potion (1d6+1 = 2-7 health):
/// - healthDiceCount: 1, healthDiceType: 6, healthBonus: 1
/// 
/// Large Health Potion (2d8+2 = 4-18 health):
/// - healthDiceCount: 2, healthDiceType: 8, healthBonus: 2
/// 
/// Simple range-based (5-10 health):
/// - healthDiceCount: 0, healthRestoreMin: 5, healthRestoreMax: 10
/// 
/// Weak Mana Potion (1d4 = 1-4 mana):
/// - manaDiceCount: 1, manaDiceType: 4, manaBonus: 0
/// </summary>
[CreateAssetMenu(fileName = "New Misc Item", menuName = "RPG/Items/Miscellaneous")]
[System.Serializable]
public class MiscellaneousItem : Item
{
    [Header("Misc Item Properties")]
    public bool isConsumable;
    public bool isStackable = true;
    public int maxStackSize = 99;

    [Header("Effects (if consumable)")]
    [Header("Health Restoration")]
    [Tooltip("Minimum health restored (used when dice count is 0)")]
    public int healthRestoreMin = 0;
    [Tooltip("Maximum health restored (used when dice count is 0)")]
    public int healthRestoreMax = 0;
    [Tooltip("Number of dice to roll (e.g., 2 for 2d6). Leave 0 to use min/max directly")]
    public int healthDiceCount = 0;
    [Tooltip("Dice type (4, 6, 8, 10, 12, 20, etc.)")]
    public int healthDiceType = 6;
    [Tooltip("Flat bonus added to dice roll (can be negative)")]
    public int healthBonus = 0;

    [Header("Mana Restoration")]
    [Tooltip("Minimum mana restored (used when dice count is 0)")]
    public int manaRestoreMin = 0;
    [Tooltip("Maximum mana restored (used when dice count is 0)")]
    public int manaRestoreMax = 0;
    [Tooltip("Number of dice to roll (e.g., 1 for 1d4). Leave 0 to use min/max directly")]
    public int manaDiceCount = 0;
    [Tooltip("Dice type (4, 6, 8, 10, 12, 20, etc.)")]
    public int manaDiceType = 4;
    [Tooltip("Flat bonus added to dice roll (can be negative)")]
    public int manaBonus = 0;
    public int temporaryStrengthBonus;
    public int temporaryDexterityBonus;
    public int temporaryConstitutionBonus;
    public int temporaryWisdomBonus;
    public float effectDuration; // In seconds, 0 for permanent

    public MiscellaneousItem()
    {
        itemType = ItemType.Miscellaneous;
    }

    public virtual void UseItem(Character character)
    {
        if (!isConsumable)
        {
            Debug.Log($"{itemName} is not consumable");
            return;
        }

        // Apply effects
        if (healthRestoreMax > 0 || healthDiceCount > 0)
        {
            int healthRestored = CalculateHealthRestore();
            character.CurrentHealth = Mathf.Min(character.MaxHealth, character.CurrentHealth + healthRestored);

            string rollInfo = GetHealthRollDescription();
            Debug.Log($"{character.CharacterName} restored {healthRestored} health from {itemName} {rollInfo}");
        }

        if (manaRestoreMax > 0 || manaDiceCount > 0)
        {
            int manaRestored = CalculateManaRestore();
            string rollInfo = GetManaRollDescription();
            Debug.Log($"{character.CharacterName} restored {manaRestored} mana from {itemName} {rollInfo}");
            // TODO: Apply mana restoration when mana system is implemented
        }

        // Add temporary stat bonuses (would need a buff system to implement properly)
        if (temporaryStrengthBonus > 0 || temporaryDexterityBonus > 0 ||
            temporaryConstitutionBonus > 0 || temporaryWisdomBonus > 0)
        {
            Debug.Log($"{character.CharacterName} gained temporary stat bonuses from {itemName}");
            // TODO: Implement temporary buff system
        }
    }

    /// <summary>
    /// Calculate health restoration using dice rolls or min/max range
    /// </summary>
    private int CalculateHealthRestore()
    {
        if (healthDiceCount > 0 && healthDiceType > 0)
        {
            // Use dice system: XdY + bonus
            int total = healthBonus;
            for (int i = 0; i < healthDiceCount; i++)
            {
                total += Random.Range(1, healthDiceType + 1);
            }
            return total;
        }
        else if (healthRestoreMax > 0)
        {
            // Use min/max range
            return Random.Range(healthRestoreMin, healthRestoreMax + 1);
        }
        return 0;
    }

    /// <summary>
    /// Calculate mana restoration using dice rolls or min/max range
    /// </summary>
    private int CalculateManaRestore()
    {
        if (manaDiceCount > 0 && manaDiceType > 0)
        {
            // Use dice system: XdY + bonus
            int total = manaBonus;
            for (int i = 0; i < manaDiceCount; i++)
            {
                total += Random.Range(1, manaDiceType + 1);
            }
            return total;
        }
        else if (manaRestoreMax > 0)
        {
            // Use min/max range
            return Random.Range(manaRestoreMin, manaRestoreMax + 1);
        }
        return 0;
    }

    /// <summary>
    /// Get a description of the item's effects for tooltips
    /// </summary>
    public string GetEffectDescription()
    {
        string description = "";

        if (healthDiceCount > 0 && healthDiceType > 0)
        {
            description += $"Restores {healthDiceCount}d{healthDiceType}";
            if (healthBonus > 0) description += $"+{healthBonus}";
            else if (healthBonus < 0) description += $"{healthBonus}";
            description += " health";
        }
        else if (healthRestoreMax > 0)
        {
            if (healthRestoreMin == healthRestoreMax)
                description += $"Restores {healthRestoreMax} health";
            else
                description += $"Restores {healthRestoreMin}-{healthRestoreMax} health";
        }

        if (manaDiceCount > 0 && manaDiceType > 0)
        {
            if (description.Length > 0) description += "\n";
            description += $"Restores {manaDiceCount}d{manaDiceType}";
            if (manaBonus > 0) description += $"+{manaBonus}";
            else if (manaBonus < 0) description += $"{manaBonus}";
            description += " mana";
        }
        else if (manaRestoreMax > 0)
        {
            if (description.Length > 0) description += "\n";
            if (manaRestoreMin == manaRestoreMax)
                description += $"Restores {manaRestoreMax} mana";
            else
                description += $"Restores {manaRestoreMin}-{manaRestoreMax} mana";
        }

        return description;
    }

    /// <summary>
    /// Get description of the health roll for debug/UI purposes
    /// </summary>
    private string GetHealthRollDescription()
    {
        if (healthDiceCount > 0 && healthDiceType > 0)
        {
            string desc = $"({healthDiceCount}d{healthDiceType}";
            if (healthBonus > 0) desc += $"+{healthBonus}";
            else if (healthBonus < 0) desc += $"{healthBonus}";
            desc += ")";
            return desc;
        }
        else if (healthRestoreMax > 0)
        {
            return $"({healthRestoreMin}-{healthRestoreMax})";
        }
        return "";
    }

    /// <summary>
    /// Get description of the mana roll for debug/UI purposes
    /// </summary>
    private string GetManaRollDescription()
    {
        if (manaDiceCount > 0 && manaDiceType > 0)
        {
            string desc = $"({manaDiceCount}d{manaDiceType}";
            if (manaBonus > 0) desc += $"+{manaBonus}";
            else if (manaBonus < 0) desc += $"{manaBonus}";
            desc += ")";
            return desc;
        }
        else if (manaRestoreMax > 0)
        {
            return $"({manaRestoreMin}-{manaRestoreMax})";
        }
        return "";
    }
}
