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;

    [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)
    {
        if (user == null) return 0;
        if (target == null) return 0;

        stats = user.GetComponent<CharacterStats>();
        GetFinalValue(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].statTag.name.ToLower()))
            {
                Debug.Log($"Influencing effect using: {influencingStats[i].statTag.name.ToLower()}");
                return stats.statsDictionary[influencingStats[i].statTag.name.ToLower()];
            }
        }
        return null;
    }
    */

    private void GetFinalValue(CharacterStats stats)
    {
        finalValue = baseValue;
        if (stats != null)
        {
            foreach (var statInfluence in influencingStats)
            {
                if (stats.statsDictionary.TryGetValue(statInfluence.statTag.name.ToLower(), out CharacterStat stat))
                {
                    finalValue += stat.Value * statInfluence.percentInfluence;
                }
            }

        }
    }
}
