using PkmnLib.Dynamic.BattleFlow;

namespace PkmnLib.Plugin.Gen7.Scripts.Pokemon;

[Script(ScriptCategory.Pokemon, "pursuit")]
public class PursuitEffect : Script, IScriptOnSwitchOut
{
    private readonly IMoveChoice _choice;

    public PursuitEffect(IMoveChoice choice)
    {
        _choice = choice;
    }

    /// <inheritdoc />
    public void OnSwitchOut(IPokemon oldPokemon, byte position)
    {
        var battleData = oldPokemon.BattleData;
        if (battleData == null)
            return;
        if (battleData.Battle.HasEnded)
            return;

        if (battleData.Position != _choice.TargetPosition || battleData.SideIndex != _choice.TargetSide)
            return;
        if (!_choice.User.IsUsable)
            return;
        if (_choice.User.BattleData?.IsOnBattlefield != true)
            return;

        var choiceQueue = battleData.Battle.ChoiceQueue;

        var choice = choiceQueue?.FirstOrDefault(x => Equals(x, _choice));
        if (choice == null)
            return;
        choiceQueue!.Remove(choice);
        _choice.Volatile.Add(new PursuitDoublePowerEffect());
        RemoveSelf();
        TurnRunner.ExecuteChoice(battleData.Battle, _choice);
    }

    [Script(ScriptCategory.Pokemon, "pursuit_double_power")]
    private class PursuitDoublePowerEffect : Script, IScriptChangeBasePower
    {
        /// <inheritdoc />
        public void ChangeBasePower(IExecutingMove move, IPokemon target, byte hit, ref ushort basePower)
        {
            basePower = basePower.MultiplyOrMax(2);
        }
    }
}