using UnityEngine;
using System.Collections.Generic;
using Kryz.CharacterStats.Examples;
using Kryz.CharacterStats;

[CreateAssetMenu(fileName = "DamageOverTimeEffect", menuName = "RiftMayhem/AbilitySystem/Effects/Damage Over Time Effect", order = 1)]
public class DamageOverTimeEffect : StatusEffect
{
    [Space]
    public float damagePerTick;
    public float percentStatInfluence;
    public float tickRate = 1f;
    public DamageOverTimeType damageType;

    private float finalTickValue;
    private CharacterStats stats;

    public override void ApplyEffect(Taggable user, List<Taggable> targets)
    {
        base.ApplyEffect(user, targets);

        if (applyToTargetsHit)
        {
            foreach (Taggable target in targets)
            {
                BaseDamageOverTimeEffectInstance dotInstance = GetOrAddCorrectInstance(target);
                dotInstance.owner.RPC((nameof(dotInstance.RPC_ApplyDamageOverTimeEffect)+damageType.ToString()), dotInstance.owner.Owner, StatusEffectIndexer.Instance.StatusEffects.IndexOf(this));
            }
        }
        if (applyToSelf)
        {
            BaseDamageOverTimeEffectInstance dotInstance = GetOrAddCorrectInstance(user);
            dotInstance.ApplyEffect(this);
        }
    }

    private BaseDamageOverTimeEffectInstance GetOrAddCorrectInstance(Taggable target)
    {
        switch (damageType)
        {
            case DamageOverTimeType.Burn:
                return target.GetComponent<BurnEffectInstance>() ?? target.gameObject.AddComponent<BurnEffectInstance>();
            case DamageOverTimeType.Poison:
                return target.GetComponent<PoisonEffectInstance>() ?? target.gameObject.AddComponent<PoisonEffectInstance>();
            case DamageOverTimeType.Bleed:
                return target.GetComponent<BleedEffectInstance>() ?? target.gameObject.AddComponent<BleedEffectInstance>();
            default:
                throw new System.ArgumentException("Unknown damage type");
        }
    }

    private void GetFinalValue(CharacterStat stat)
    {
        if (stat == null)
            finalTickValue = damagePerTick;
        else
            finalTickValue = damagePerTick + stat.Value * percentStatInfluence;
    }
}