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

public abstract class BaseDamageOverTimeEffectInstance : StatusEffectInstance
{
    public float currentDamagePerTick;
    public abstract DamageOverTimeType DamageType { get; }

    //public UnityEvent OnEffectApplied = new UnityEvent();
    public UnityEvent OnEffectTick = new UnityEvent();
    //public UnityEvent OnEffectEnded = new UnityEvent();

    protected float nextTickTime;

    protected override void Awake()
    {
        base.Awake();
        if (owner.IsMine)
        {
            OnEffectStackAddedEvent.AddListener(() => owner.RPC(nameof(RPC_EffectApplied), RpcTarget.Others));
            OnEffectEnded.AddListener(() => owner.RPC(nameof(RPC_EffectEnded), RpcTarget.Others));
        }
    }

    protected override void AddStack(StatusEffect addedEffect)
    {
        DamageOverTimeEffect dotEffect = (DamageOverTimeEffect)addedEffect;
        currentDamagePerTick += dotEffect.damagePerTick;
        base.AddStack(addedEffect);
    }

    protected override void RefreshEffect(StatusEffect effect)
    {
        base.RefreshEffect(effect);
        currentDamagePerTick = GetHighestDamagePerTick();
        OnEffectStackAdded();
    }

    protected override IEnumerator EffectStateCoroutine()
    {
        nextTickTime = Time.time + ((DamageOverTimeEffect)activeStacks[0]).tickRate;

        while (Time.time < endEffectTime)
        {
            if (Time.time >= nextTickTime)
            {
                ApplyDamage();
                nextTickTime = Time.time + ((DamageOverTimeEffect)activeStacks[0]).tickRate;
            }
            yield return null;
        }

        EffectStateEnded();
    }

    protected virtual void ApplyDamage()
    {
        Health targetHealth = GetComponent<Health>();
        if (targetHealth != null)
        {
            targetHealth.ChangeValue(-currentDamagePerTick);
        }
        OnEffectTick.Invoke();
    }

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

    protected override void EffectStateEnded()
    {
        currentDamagePerTick = 0;
        OnEffectEnded.Invoke();
        base.EffectStateEnded();
    }

    private float GetHighestDamagePerTick()
    {
        float highestDamage = 0;
        for (int i = activeStacks.Count - 1; i >= 0; i--)
        {
            if (((DamageOverTimeEffect)activeStacks[i]).damagePerTick > highestDamage)
                highestDamage = ((DamageOverTimeEffect)activeStacks[i]).damagePerTick;
        }
        return highestDamage;
    }

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

    [PunRPC]
    public void RPC_EffectApplied()
    {
        OnEffectStackAddedEvent.Invoke();
    }

    [PunRPC]
    public void RPC_EffectEnded()
    {
        OnEffectEnded.Invoke();
    }
}

public enum DamageOverTimeType
{
    Burn,
    Poison,
    Bleed
    // Add more types as needed
}