using PkmnLib.Plugin.Gen7.Scripts.MoveVolatile;
using PkmnLib.Plugin.Gen7.Scripts.Utils;

namespace PkmnLib.Plugin.Gen7.Scripts.Moves;

[Script(ScriptCategory.Move, "sleep_talk")]
public class SleepTalk : Script, IScriptChangeMove
{
    /// <inheritdoc />
    public void ChangeMove(IMoveChoice choice, ref StringKey moveName)
    {
        if (!choice.User.HasStatus(ScriptUtils.ResolveName<Status.Sleep>()))
        {
            choice.Fail();
            return;
        }
        var battleData = choice.User.BattleData;
        if (battleData == null)
            return;

        var moves = choice.User.Moves.WhereNotNull().Where(x => x.MoveData.CanCopyMove())
            .Where(x => x != choice.ChosenMove).OrderBy(_ => battleData.Battle.Random.GetInt()).FirstOrDefault();
        if (moves == null)
        {
            choice.Fail();
            return;
        }
        moveName = moves.MoveData.Name;
        choice.Volatile.Add(new BypassSleepVolatile());
    }
}