namespace PkmnLib.Plugin.Gen7.Scripts.Battle;

[Script(ScriptCategory.Battle, "future_sight")]
public class FutureSightEffect : Script, IScriptOnEndTurn
{
    private int _turnsLeft = 3;
    private readonly IMoveChoice _moveChoice;

    public FutureSightEffect(IMoveChoice moveChoice)
    {
        _moveChoice = moveChoice;
    }

    /// <inheritdoc />
    public void OnEndTurn(IScriptSource owner, IBattle battle)
    {
        _turnsLeft -= 1;
        if (_turnsLeft <= 0)
        {
            var target = battle.GetPokemon(_moveChoice.TargetSide, _moveChoice.TargetPosition);
            if (target is not { IsUsable: true })
            {
                battle.EventHook.Invoke(new DialogEvent("move_failed"));
                return;
            }
            var damageCalculator = battle.Library.DamageCalculator;
            var executingMove = new ExecutingMoveImpl([target], 1, _moveChoice.ChosenMove,
                _moveChoice.ChosenMove.MoveData, _moveChoice, battle);
            var hitData = executingMove.GetHitData(target, 0);
            var damage = damageCalculator.GetDamage(executingMove, executingMove.UseMove.Category, executingMove.User,
                target, executingMove.TargetCount, 1, hitData);

            target.Damage(damage, DamageSource.Misc);
        }
    }
}