using PkmnLib.Dynamic.BattleFlow;

namespace PkmnLib.Plugin.Gen7.Scripts.Moves;

[Script(ScriptCategory.Move, "instruct")]
public class Instruct : Script, IScriptOnSecondaryEffect
{
    /// <inheritdoc />
    public void OnSecondaryEffect(IExecutingMove move, IPokemon target, byte hit)
    {
        if (!target.IsUsable)
            return;
        var battleData = move.User.BattleData;
        if (battleData == null)
            return;

        var lastMoveChoiceByTarget = target.BattleData?.LastMoveChoice;

        if (lastMoveChoiceByTarget == null || !battleData.Battle.CanUse(lastMoveChoiceByTarget))
        {
            move.GetHitData(target, hit).Fail();
            return;
        }

        TurnRunner.ExecuteChoice(battleData.Battle, lastMoveChoiceByTarget);
    }
}