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

[CreateAssetMenu(fileName = "InstantEffect", menuName = "RiftMayhem/AbilitySystem/Effects/Instant Effect", order = 1)]
public class InstantValueEffect : BaseEffect
{
    public float baseValue;
    public float percentStatInfluence;

    [Header("Optional Modifiers for damage / healing based on target")]
    public float AlliedTargetMultiplier = 1f;
    public float EnemyTargetMultiplier = 1f;

    Health targetHealth;

    private float finalValue;
    private CharacterStats stats;

    public override void ApplyEffect(Taggable user, List<Taggable> targets)
    {
        foreach (Taggable target in targets)
        {
            Debug.Log($"Applied instant effect of {GetCorrectValueSign(user, target)} to target {target.name}");
            targetHealth = target.GetComponent<Health>();
            if (targetHealth != null)
                targetHealth.photonView.RPC(nameof(targetHealth.RPC_ChangeValueHealth), targetHealth.photonView.Owner, GetCorrectValueSign(user, target));
            //targetHealth.ChangeValue(target.targetTag == TargettingTag.Enemy ? -value : value);
        }
    }

    private float GetCorrectValueSign(Taggable user, Taggable target)
    {
        stats = user.GetComponent<CharacterStats>();
        GetFinalValue(GetCorrectStat(stats));

        return user.targetTag.AlliedTags.Contains(target.targetTag) ? (finalValue * AlliedTargetMultiplier) : (-finalValue * EnemyTargetMultiplier);
    }

    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;
    }
}
