using System;
using System.Collections.Generic;
using PkmnLibSharp.Utilities;

namespace PkmnLibSharp.Library
{
    public class SpeciesLibrary : PointerWrapper
    {
        private readonly Dictionary<string, Species> _cache = new Dictionary<string, Species>(StringComparer.InvariantCultureIgnoreCase);
        public ulong Count => Creaturelib.Generated.SpeciesLibrary.GetCount(Ptr);

        public void Insert(string key, Species species)
        {
            Creaturelib.Generated.SpeciesLibrary.Insert(Ptr, key.ToPtr(), species.Ptr).Assert();
            _cache.Add(key, species);
        }

        public void Delete(string key)
        {
            Creaturelib.Generated.SpeciesLibrary.Delete(Ptr, key.ToPtr()).Assert();
            _cache.Remove(key);
        }

        public bool TryGet(string key, out Species? species)
        {
            if (_cache.TryGetValue(key, out species))
                return true;
            var ptr = IntPtr.Zero;
            if (Creaturelib.Generated.SpeciesLibrary.TryGet(Ptr, key.ToPtr(), ref ptr) != MarshalHelper.True) 
                return false;
            if (TryResolvePointer(ptr, out species))
            {
                _cache.Add(key, species!);
                return true;
            }
            species = Constructor.Active.ConstructSpecies(ptr)!;
            _cache.Add(key, species);
            return true;
        }

        public Species Get(string key)
        {
            if (_cache.TryGetValue(key, out var species))
                return species;
            var ptr = IntPtr.Zero;
            Creaturelib.Generated.SpeciesLibrary.Get(Ptr, key.ToPtr(), ref ptr).Assert();
            if (TryResolvePointer(ptr, out Species? s))
            {
                _cache.Add(key, s!);
                return s!;
            }
            species = Constructor.Active.ConstructSpecies(ptr)!;
            _cache.Add(key, species);
            return species;
        }

        public Species GetById(ushort id)
        {
            var ptr = Creaturelib.Generated.SpeciesLibrary.GetById(Ptr, id);
            if (TryResolvePointer(ptr, out Species? s))
            {
                return s!;
            }
            s = Constructor.Active.ConstructSpecies(ptr)!;
            return s;
        }

        public Species? FindPreEvolution(Species species)
        {
            var ptr = Pkmnlib.Generated.SpeciesLibrary.FindPreEvolution(Ptr, species.Ptr);
            if (ptr == IntPtr.Zero) return null;
            return TryResolvePointer(ptr, out Species? prevoSpecies)
                ? prevoSpecies
                : Constructor.Active.ConstructSpecies(ptr);
        }

        public IEnumerable<Species> GetEnumerator()
        {
            var count = Creaturelib.Generated.SpeciesLibrary.GetCount(Ptr);
            var ptr = IntPtr.Zero;
            for (ulong i = 0; i < count; i++)
            {
                Creaturelib.Generated.SpeciesLibrary.GetAtIndex(Ptr, i, ref ptr).Assert();
                if (TryResolvePointer(ptr, out Species? species))
                    yield return species!;
                else
                    yield return Constructor.Active.ConstructSpecies(ptr)!;
            }
        }

        internal SpeciesLibrary(IntPtr ptr) : base(ptr)
        {
        }

        public SpeciesLibrary(ulong initialCapacity) : base(
            Creaturelib.Generated.SpeciesLibrary.Construct(initialCapacity))
        {
        }

        protected override void DeletePtr()
        {
            Creaturelib.Generated.SpeciesLibrary.Destruct(Ptr);
        }

        protected internal override void MarkAsDeleted()
        {
            base.MarkAsDeleted();
            foreach (var species in _cache)
            {
                species.Value.MarkAsDeleted();
            }
        }
    }
}