namespace PkmnLib.Plugin.Gen7.Scripts.Abilities;

/// <summary>
/// Solar Power is an ability that boosts Special Attack in harsh sunlight but causes the Pokémon to lose HP each turn.
///
/// <see href="https://bulbapedia.bulbagarden.net/wiki/Solar_Power_(Ability)">Bulbapedia - Solar Power</see>
/// </summary>
[Script(ScriptCategory.Ability, "solar_power")]
public class SolarPower : Script, IScriptChangeOffensiveStatValue, IScriptOnEndTurn,
    IAIInfoScriptExpectedEndOfTurnDamage
{
    /// <inheritdoc />
    public void ChangeOffensiveStatValue(IExecutingMove move, IPokemon target, byte hit, uint defensiveStat,
        ImmutableStatisticSet<uint> targetStats, Statistic stat, ref uint value)
    {
        if ((stat == Statistic.SpecialAttack &&
             move.Battle.WeatherName == ScriptUtils.ResolveName<Weather.HarshSunlight>()) ||
            move.Battle.WeatherName == ScriptUtils.ResolveName<Weather.DesolateLands>())
        {
            value = value.MultiplyOrMax(1.5f);
        }
    }

    /// <inheritdoc />
    public void OnEndTurn(IScriptSource owner, IBattle battle)
    {
        if (owner is not IPokemon pokemon)
            return;

        if (!pokemon.IsUsable)
            return;

        var weatherName = pokemon.BattleData?.Battle.WeatherName;
        if (weatherName != ScriptUtils.ResolveName<Weather.HarshSunlight>() &&
            weatherName != ScriptUtils.ResolveName<Weather.DesolateLands>())
        {
            return;
        }

        pokemon.Damage(pokemon.MaxHealth / 8, DamageSource.Weather);
    }

    /// <inheritdoc />
    public void ExpectedEndOfTurnDamage(IPokemon pokemon, ref int damage)
    {
        var weatherName = pokemon.BattleData?.Battle.WeatherName;
        if (weatherName != ScriptUtils.ResolveName<Weather.HarshSunlight>() &&
            weatherName != ScriptUtils.ResolveName<Weather.DesolateLands>())
        {
            return;
        }

        damage += (int)(pokemon.MaxHealth / 8f);
    }
}