using PkmnLib.Plugin.Gen7.Scripts.Side;

namespace PkmnLib.Plugin.Gen7.Scripts.Moves;

[Script(ScriptCategory.Move, "double_power_if_target_damaged_in_turn")]
public class DoublePowerIfTargetDamagedInTurn : Script, IScriptOnBeforeTurnStart, IScriptChangeBasePower
{
    /// <inheritdoc />
    public void OnBeforeTurnStart(ITurnChoice choice)
    {
        if (choice is IMoveChoice moveChoice)
        {
            var battle = choice.User.BattleData?.Battle;
            if (battle == null)
                return;
            var side = battle.Sides[moveChoice.TargetSide];
            side.VolatileScripts.Add(new DoublePowerIfTargetDamagedInTurnData());
        }
    }

    /// <inheritdoc />
    public void ChangeBasePower(IExecutingMove move, IPokemon target, byte hit, ref ushort basePower)
    {
        var battle = move.User.BattleData?.Battle;
        if (battle == null)
            return;
        if (target.BattleData == null)
            return;
        var side = battle.Sides[target.BattleData.SideIndex];
        var data = side.VolatileScripts.Get<DoublePowerIfTargetDamagedInTurnData>();
        if (data == null)
            return;
        if (data.HitPokemon.Contains(target))
            basePower *= 2;
    }
}