using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DamageIncomeModifierEffectInstance : StatusEffectInstance
{

    public float startingIncomeModifierPercent;

    public float currentIncomeModifierPercent;


    public float ModifyDamageIncome(float income)
    {
        return income = income * (1 + currentIncomeModifierPercent);
    }

    public float GetHighestAmount()
    {
        float highestAmount = 0;
        for (int i = 0; i < activeStacks.Count; i++)
        {
            if (((DamageIncomeModifierEffect)activeStacks[i]).damageIncomeModifierPercentage > highestAmount)
                highestAmount = ((DamageIncomeModifierEffect)activeStacks[i]).damageIncomeModifierPercentage;
        }

        return highestAmount;
    }

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

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

    protected override void AddStack(StatusEffect addedEffect)
    {
        if (activeStacks.Count <= 0)
        {
            startingIncomeModifierPercent = ((DamageIncomeModifierEffect)addedEffect).damageIncomeModifierPercentage;
            currentIncomeModifierPercent = startingIncomeModifierPercent;
        }
        else if (canStack && !activeStacks.Contains(addedEffect))
        {
            currentIncomeModifierPercent += ((DamageIncomeModifierEffect)addedEffect).damageIncomeModifierPercentage;
        }
        else
        {
            OnEffectStackAdded();
            return;
        }

        base.AddStack(addedEffect);
    }

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

        startingIncomeModifierPercent = GetHighestAmount();
        currentIncomeModifierPercent = startingIncomeModifierPercent;

        OnEffectStackAdded();
    }

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

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

    protected override void EffectStateEnded()
    {
        startingIncomeModifierPercent = 0;
        currentIncomeModifierPercent = startingIncomeModifierPercent;

        base.EffectStateEnded();
    }
}
