namespace PkmnLib.Plugin.Gen7.Scripts.Abilities;

/// <summary>
/// Color Change is an ability that changes the user's type to match the type of the last move it was hit by.
/// This effect applies to any damaging move that hits the Pokémon.
///
/// <see href="https://bulbapedia.bulbagarden.net/wiki/Color_Change_(Ability)">Bulbapedia - Color Change</see>
/// </summary>
[Script(ScriptCategory.Ability, "color_change")]
public class ColorChange : Script, IScriptOnIncomingHit
{
    /// <inheritdoc />
    public void OnIncomingHit(IExecutingMove move, IPokemon target, byte hit)
    {
        var hitData = move.GetHitData(target, hit);
        if (hitData.Type != null && (hitData.Type != target.Types.FirstOrDefault() || target.Types.Count > 1))
        {
            target.BattleData?.Battle.EventHook.Invoke(new AbilityTriggerEvent(target));
            target.SetTypes([hitData.Type.Value]);
        }
    }
}