namespace PkmnLib.Plugin.Gen7.Scripts.Abilities;

/// <summary>
/// Power Construct is an ability that allows Zygarde to change form when its HP drops below half.
///
/// <see href="https://bulbapedia.bulbagarden.net/wiki/Power_Construct_(Ability)">Bulbapedia - Power Construct</see>
/// </summary>
[Script(ScriptCategory.Ability, "power_construct")]
public class PowerConstruct : Script, IScriptOnEndTurn
{
    private IPokemon? _pokemon;

    /// <inheritdoc />
    public override void OnAddedToParent(IScriptSource source)
    {
        if (source is not IPokemon pokemon)
            throw new InvalidOperationException("PowerConstruct script must be attached to a Pokemon.");
        _pokemon = pokemon;
    }

    /// <inheritdoc />
    public void OnEndTurn(IScriptSource owner, IBattle battle)
    {
        if (_pokemon?.BattleData?.Battle == null)
            return;
        if (_pokemon.Species.Name != "zygarde")
            return;
        if (_pokemon.CurrentHealth > _pokemon.BoostedStats.Hp / 2)
            return;
        if (_pokemon.Form.Name != "10" || _pokemon.Form.Name != "50")
            return;
        if (!_pokemon.Species.TryGetForm("complete", out var completeForm))
            return;

        _pokemon.ChangeForm(completeForm);
    }
}