using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using PkmnLibSharp.Library.Items;
using PkmnLibSharp.Library.Moves;
using PkmnLibSharp.Utilities;

namespace PkmnLibSharp.Library.Evolution
{
    public sealed class EvolutionData : PointerWrapper
    {
        internal EvolutionData(IntPtr ptr) : base(ptr)
        {
        }
        
        public static EvolutionData CreateLevelEvolution(byte level, Species into)
        {
            return new EvolutionData(Pkmnlib.Generated.EvolutionData.CreateLevelEvolution(level, into.Ptr));
        }
        public static EvolutionData CreateFriendshipEvolution(byte level, Species into)
        {
            return new EvolutionData(Pkmnlib.Generated.EvolutionData.CreateFriendshipEvolution(level, into.Ptr));
        }
        public static EvolutionData CreateKnownMoveEvolution(MoveData move, Species into)
        {
            return new EvolutionData(Pkmnlib.Generated.EvolutionData.CreateKnownMoveEvolution(move.Ptr, into.Ptr));
        }
        public static EvolutionData CreateLocationEvolution(string location, Species into)
        {
            return new EvolutionData(
                Pkmnlib.Generated.EvolutionData.CreateLocationEvolution(location.ToPtr(), into.Ptr));
        }
        public static EvolutionData CreateTimeEvolution(TimeOfDay timeOfDay, Species into)
        {
            return new EvolutionData(
                Pkmnlib.Generated.EvolutionData.CreateTimeEvolution((Pkmnlib.TimeOfDay) timeOfDay, into.Ptr));
        }
        public static EvolutionData CreateItemEvolution(Item item, Species into)
        {
            return new EvolutionData(Pkmnlib.Generated.EvolutionData.CreateItemEvolution(item.Ptr, into.Ptr));
        }
        public static EvolutionData CreateGenderBasedEvolution(Gender gender, byte level, Species into)
        {
            return new EvolutionData(
                Pkmnlib.Generated.EvolutionData.CreateGenderBasedEvolution((Pkmnlib.Gender) gender, level, into.Ptr));
        }
        public static EvolutionData CreateItemUseEvolution(Item item, Species into)
        {
            return new EvolutionData(
                Pkmnlib.Generated.EvolutionData.CreateItemUseEvolution(item.Ptr, into.Ptr));
        }
        public static EvolutionData CreateItemUseWithGenderEvolution(Item item, Gender gender, Species into)
        {
            return new EvolutionData(
                Pkmnlib.Generated.EvolutionData.CreateItemUseWithGenderEvolution(item.Ptr, (Pkmnlib.Gender) gender, into.Ptr));
        }
        public static EvolutionData CreateTradeEvolution(Species into)
        {
            return new EvolutionData(
                Pkmnlib.Generated.EvolutionData.CreateTradeEvolution(into.Ptr));
        }
        public static EvolutionData CreateTradeWithItemEvolution(Item item, Species into)
        {
            return new EvolutionData(
                Pkmnlib.Generated.EvolutionData.CreateTradeWithItemEvolution(item.Ptr, into.Ptr));
        }
        public static EvolutionData CreateTradeWithSpeciesEvolution(Species with, Species into)
        {
            return new EvolutionData(
                Pkmnlib.Generated.EvolutionData.CreateTradeWithSpeciesEvolution(with.Ptr, into.Ptr));
        }
        public static EvolutionData CreateCustomEvolution(IList<EffectParameter> parameters, Species into)
        {
            var parametersPtrs = parameters.Select(x => x.Ptr).ToArray();
            GCHandle.Alloc(parametersPtrs, GCHandleType.Pinned);
            var ptr = parametersPtrs.ArrayPtr();
            return new EvolutionData(
                Pkmnlib.Generated.EvolutionData.CreateCustomEvolution(ptr, (ulong) parameters.Count, into.Ptr));
        }

        public EvolutionMethod Method => (EvolutionMethod) Pkmnlib.Generated.EvolutionData.GetMethod(Ptr);

        public Species NewSpecies
        {
            get
            {
                if (_species != null) return _species;
                var ptr = Pkmnlib.Generated.EvolutionData.GetNewSpecies(Ptr);
                if (TryResolvePointer(ptr, out _species))
                    return _species!;
                _species = Constructor.Active.ConstructSpecies(ptr)!;
                return _species;
            }
        }

        public ulong DataLength => Pkmnlib.Generated.EvolutionData.GetDataCount(Ptr);

        public EffectParameter GetData(ulong index)
        {
            var ptr = IntPtr.Zero;
            Pkmnlib.Generated.EvolutionData.GetData(Ptr, index, ref ptr).Assert();
            if (TryResolvePointer(ptr, out EffectParameter? parameter))
                return parameter!;
            return new EffectParameter(ptr);
        }

        private Species? _species;
        protected override void DeletePtr()
        {
            throw new NotImplementedException();
        }
    }
}