namespace PkmnLib.Plugin.Gen7.Scripts.Moves;

[Script(ScriptCategory.Move, "sketch")]
public class Sketch : Script, IScriptOnSecondaryEffect
{
    /// <inheritdoc />
    public void OnSecondaryEffect(IExecutingMove move, IPokemon target, byte hit)
    {
        // Defensive programming; If the move we're using is not the same as the one picked, something changed
        // our move to sketch. This should never happen, as moves are not allowed to change to sketch, but in the
        // case it does, we should fail the move.
        if (move.ChosenMove.MoveData != move.UseMove)
        {
            move.GetHitData(target, hit).Fail();
            return;
        }
        var moveSlot = move.User.Moves.IndexOf(move.ChosenMove);
        if (moveSlot == -1)
        {
            move.GetHitData(target, hit).Fail();
            return;
        }

        var lastMove = target.BattleData?.LastMoveChoice;
        if (lastMove == null || lastMove.ChosenMove.MoveData.HasFlag(MoveFlags.NotSketchable))
        {
            move.GetHitData(target, hit).Fail();
            return;
        }

        move.User.LearnMove(lastMove.ChosenMove.MoveData.Name, MoveLearnMethod.Sketch, (byte)moveSlot);
    }
}