namespace PkmnLib.Plugin.Gen7.Scripts.Weather;

[Script(ScriptCategory.Weather, "rain")]
public class Rain : Script, ILimitedTurnsScript, IScriptChangeBasePower, IScriptOnEndTurn, IScriptChangeAccuracy
{
    private int? _duration;

    /// <inheritdoc />
    public int TurnsRemaining => _duration ?? 0;

    /// <inheritdoc />
    public void SetTurns(int turns)
    {
        _duration = turns;
    }

    /// <inheritdoc />
    public virtual void OnEndTurn(IScriptSource owner, IBattle battle)
    {
        _duration--;
        if (_duration <= 0)
        {
            battle.SetWeather(null, 0);
        }
    }

    /// <inheritdoc />
    public void ChangeBasePower(IExecutingMove move, IPokemon target, byte hit, ref ushort basePower)
    {
        var hitType = move.GetHitData(target, hit).Type;
        if (hitType?.Name == "water")
        {
            // Increase Water-type move power by 50% in rain
            basePower = (ushort)(basePower * 1.5);
        }
        else if (hitType?.Name == "fire")
        {
            // Decrease Fire-type move power by 50% in rain
            basePower = (ushort)(basePower * 0.5);
        }
    }

    /// <inheritdoc />
    public void ChangeAccuracy(IExecutingMove executingMove, IPokemon target, byte hitIndex, ref int modifiedAccuracy)
    {
        modifiedAccuracy = executingMove.UseMove.Name.ToString() switch
        {
            "thunder" or "hurricane" or "bleakwind_storm" or "windbolt_storm" or "sandsear_storm" => 1000,
            _ => modifiedAccuracy,
        };
    }
}