using Kryz.CharacterStats.Examples;
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class Mana : Resource
{
    CharacterStats character;

    public UnityEvent<float> onMaxManaChanged = new UnityEvent<float>();

    [HideInInspector]
    public PhotonView photonView;

    private void Awake()
    {
        character = GetComponent<CharacterStats>();
        photonView = GetComponent<PhotonView>();

        baseMaxValue = maxValue;
        baseFlatRegen = flatRegen;

        character.onUpdateStatValues.AddListener(CalculateMaxValueBasedOnStat);
    }

    protected override void Start()
    {
        if (!photonView.IsMine) return;

        base.Start();
    }

    public override void ChangeValue(float value)
    {
        if (!photonView.IsMine) return;

        currentValue += value;

        currentValue = Mathf.Clamp(currentValue, 0, maxValue);

        onResourceChanged.Invoke(currentValue);
    }

    [PunRPC]
    public void RPC_ChangeValueMana (float value)
    {
        if (!photonView.IsMine) return;
        Debug.Log("Received ChangeValue from RPC from someone");
        ChangeValue(value);
    }

    public override float GetMaxValue()
    {

        return base.GetMaxValue();
    }

    public bool EnoughMana(float cost)
    {
        return cost <= currentValue;
    }

    public void CalculateMaxValueBasedOnStat()
    {
        maxValue = baseMaxValue + character.Intelligence.Value * 10f;

        CalculateRegenValueBasedOnStat();

        onMaxManaChanged.Invoke(maxValue);
    }
    public void CalculateRegenValueBasedOnStat()
    {
        flatRegen = baseFlatRegen + (character.Spirit.Value - character.Spirit.BaseValue) * 0.5f;
        percentRegen = (character.Spirit.Value - character.Spirit.BaseValue) * 0.01f;
    }

    public override void SetMaxValue(float value)
    {
        base.SetMaxValue(value);

        onMaxManaChanged.Invoke(maxValue);
    }
}
