using UnityEngine;
using System.Collections.Generic;

public class PlayerData : MonoBehaviour
{
    public new string name;
    [SerializeField]
    private int money;

    public List<PlayerStat> stats = new List<PlayerStat>();
    // Statistics
    

    public int Money
    {
        get { return money; }
        set
        {
            money = value;
            if (money < 0)
                money = 0;
        }
    }

    public InventoryObject inventory;

    public delegate void AddItemEvent();
    public static event AddItemEvent OnAddItemEvent;
    public delegate void StatChanged();
    public static event StatChanged OnStatChanged;

    public void AddItemToInventory(Item _item, int _count)
    {
        inventory.AddItem(_item.item, _count);
        OnAddItemEvent?.Invoke();
    }

    public void AddItemToInventory(ItemObject _item, int _count)
    {
        inventory.AddItem(_item, _count);
        OnAddItemEvent?.Invoke();
    }

    private void OnApplicationQuit()
    {
        inventory.Container.Clear();
    }

    public bool IsItemInInventory(ItemObject _item)
    {
        foreach (InventorySlotObject item in inventory.Container)
        {
            if (_item == item.item) return true;
        }
        return false;
    }

    public bool UseItemInInventory(ItemObject _item, int _count)
    {
        foreach (InventorySlotObject item in inventory.Container)
        {
            if (_item == item.item)
            {
                item.count -= 1;
                if(item.count <= 0)
                {
                    inventory.Container.Remove(item);
                }
                return true;
            }
        }
        return false;
    }

    public void SetUsername(string _username)
    {
        Debug.Log(_username);
        name = _username;
    }

    public PlayerStat GetPlayerStat(string key)
    {
        foreach (PlayerStat stat in stats)
        {
            if (key == stat.key) return stat;
        }

        return null;
    }

    public bool SetPlayerStat(string key, int value)
    {
        foreach (PlayerStat stat in stats)
        {
            if (key == stat.key)
            {
                OnStatChanged?.Invoke();
                stat.value = value;
                return true;
            }
        }
        return false;
    }

    public bool IncrementPlayerStat(string key, int value = 1)
    {
        foreach (PlayerStat stat in stats)
        {
            if (key == stat.key)
            {
                OnStatChanged?.Invoke();
                stat.value += value;
                return true;
            }
        }
        return false;
    }

    public bool AlterPlayerStat(string key, int value, char operation)
    {
        foreach (PlayerStat stat in stats)
        {
            switch (operation)
            {
                case '+':
                case '-':
                    stat.value = (operation == '+') ? stat.value + value : stat.value - value ;
                    break;
                case '*':
                case '/':
                    stat.value *= (operation == '*') ? stat.value * value : stat.value / value ;
                    break;
                case '%':
                    stat.value = stat.value % value;
                    break;
                default:
                    return false;
            }
            OnStatChanged?.Invoke();
            return true;
        }
        return false;
    }

}

[System.Serializable]
public class PlayerStat
{
    public string key;
    public string name;
    public int value;

}