using Cysharp.Threading.Tasks;
using UnityEngine;
using YachtDice.Modifiers.Core;
using YachtDice.Modifiers.Definition;
using YachtDice.Modifiers.Runtime;

namespace YachtDice.Modifiers.Effects
{
    [CreateAssetMenu(fileName = "MultiplyPerDieEffect", menuName = "YachtDice/Modifiers/Effects/Multiply Per Die")]
    public class MultiplyPerDieEffect : Effect
    {
        [Tooltip("Multiplier to apply per matching die.")]
        [SerializeField] private float multiplierPerDie = 1f;

        [Tooltip("Die face value to match (1-6). 0 = any/all dice.")]
        [SerializeField, Range(0, 6)] private int targetDieValue;

        public override UniTask Apply(ModifierContext context, ModifierInstance instance)
        {
            if (context.DiceValues == null) return UniTask.CompletedTask;

            for (int i = 0; i < context.DiceValues.Length; i++)
            {
                if (targetDieValue == 0 || context.DiceValues[i] == targetDieValue)
                    context.Multiplier *= multiplierPerDie;
            }

            return UniTask.CompletedTask;
        }

#if UNITY_EDITOR
        public static MultiplyPerDieEffect CreateForTest(float multiplierPerDie, int targetDieValue = 0,
            ModifierPhase phase = ModifierPhase.Multiplicative, int priority = 0)
        {
            var so = CreateInstance<MultiplyPerDieEffect>();
            so.multiplierPerDie = multiplierPerDie;
            so.targetDieValue = targetDieValue;
            so.SetPhaseForTest(phase);
            so.SetPriorityForTest(priority);
            return so;
        }
#endif
    }
}
