using PkmnLib.Static.Species;

namespace PkmnLib.Plugin.Gen7.Scripts.Items;

[ItemScript("evolution_use")]
public class EvolutionItem : ItemScript
{
    /// <inheritdoc />
    public EvolutionItem(IItem item) : base(item)
    {
    }

    /// <inheritdoc />
    public override bool IsItemUsable => true;

    /// <inheritdoc />
    public override ItemTargetType TargetType => ItemTargetType.OwnPokemon;

    /// <inheritdoc />
    public override bool IsTargetValid(IPokemon target)
    {
        foreach (var x in target.Species.EvolutionData)
        {
            switch (x)
            {
                case ItemUseEvolution itemUseEvolution when itemUseEvolution.Item == Item.Name:
                    return true;
                case ItemGenderEvolution itemGenderEvolution when itemGenderEvolution.Item == Item.Name:
                    return itemGenderEvolution.Gender == target.Gender;
            }
        }
        return false;
    }

    /// <inheritdoc />
    public override void OnUseWithTarget(IPokemon target, EventHook eventHook)
    {
        var evolutionData = target.Species.EvolutionData.FirstOrDefault(x =>
        {
            switch (x)
            {
                case ItemUseEvolution itemUseEvolution when itemUseEvolution.Item == Item.Name:
                case ItemGenderEvolution itemGenderEvolution when itemGenderEvolution.Item == Item.Name:
                    return true;
                default:
                    return false;
            }
        });
        if (evolutionData == null)
            return;

        target.EvolveTo(evolutionData, eventHook);
    }
}