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

public class AbsorbEffectInstance : StatusEffectInstance
{
    public float startingAmount;

    public float currentAmount;

    public UnityEvent OnAbsorbDamage = new UnityEvent();

    float income;

    private void Awake()
    {
        owner = GetComponent<PhotonView>();

        if (owner.IsMine)
        {
            OnEffectStackAddedEvent.AddListener(() => owner.RPC(nameof(RPC_AbsorbStarted), RpcTarget.Others));
            OnEffectEnded.AddListener(() => owner.RPC(nameof(RPC_AbsorbEnded), RpcTarget.Others));
            OnAbsorbDamage.AddListener(() => owner.RPC(nameof(RPC_AbsorbHit), RpcTarget.Others));
        }
    }

    public float AbsorbDamage(float income)
    {
        this.income = income;
        this.income += currentAmount;
        if (this.income >= 0)
        {
            currentAmount = this.income;
        }
        else
        {
            StopCoroutine(effectStateCoroutine);

            EffectStateEnded();
        }

        OnAbsorbDamage.Invoke();

        return this.income;
    }

    public float GetHighestAmount()
    {
        float highestAmount = 0;
        for (int i = activeStacks.Count - 1; i >= 0; i--)
        {
            if (((AbsorbEffect)activeStacks[i]).amount > highestAmount)
                highestAmount = ((AbsorbEffect)activeStacks[i]).amount;
        }

        return highestAmount;
    }

    [PunRPC]
    public void RPC_ApplyAbsorbEffect(int effectIndex)
    {
        ApplyEffect(StatusEffectIndexer.Instance.StatusEffects[effectIndex]);
    }

    [PunRPC]
    public void RPC_AbsorbStarted()
    {
        OnEffectStackAddedEvent.Invoke();
    }
    [PunRPC]
    public void RPC_AbsorbEnded()
    {
        OnEffectEnded.Invoke();
    }
    [PunRPC]
    public void RPC_AbsorbHit()
    {
        OnAbsorbDamage.Invoke();
    }

    public override void ApplyEffect(StatusEffect effect)
    {
        base.ApplyEffect(effect);
    }

    protected override void AddStack(StatusEffect addedEffect)
    {
        if (activeStacks.Count <= 0)
        {
            startingAmount = ((AbsorbEffect)addedEffect).amount;
            currentAmount = startingAmount;
        }
        else if (canStack)
        {
            currentAmount += ((AbsorbEffect)addedEffect).amount;
        }

        base.AddStack(addedEffect);
    }

    protected override void RefreshEffect(StatusEffect effect)
    {
        base.RefreshEffect(effect);

        startingAmount = GetHighestAmount();
        currentAmount = startingAmount;

        OnEffectStackAdded();
    }

    protected override IEnumerator EffectStateCoroutine()
    {
        return base.EffectStateCoroutine();
    }

    protected override void EffectStateStarted()
    {
        base.EffectStateStarted();
    }

    protected override void EffectStateEnded()
    {
        startingAmount = 0;
        currentAmount = startingAmount;

        base.EffectStateEnded();
    }

    /*
   private CharacterStat GetCorrectStat(CharacterStats stats) //TODO: make it use multiple stats correctly
   {
       for (int i = 0; i < influencingStats.Count; i++)
       {
           if (stats.statsDictionary.ContainsKey(influencingStats[i].name.ToLower()))
           {
               Debug.Log($"Influencing effect using: {influencingStats[i].name.ToLower()}");
               return stats.statsDictionary[influencingStats[i].name.ToLower()];
           }
       }
       return null;
   }

   private void GetFinalValue(CharacterStat stat)
   {
       if (stat == null)
           finalValue = baseValue;
       else
           finalValue = baseValue + stat.Value * percentStatInfluence;
   }*/
}
