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

namespace PkmnLibSharp.Library
{
    public class TypeLibrary : PointerWrapper
    {
        private readonly Dictionary<string, byte> _cache =
            new Dictionary<string, byte>(StringComparer.InvariantCultureIgnoreCase);

        public TypeLibrary(ulong initialCapacity) : base(
            Creatureliblibrary.Generated.TypeLibrary.Construct(initialCapacity))
        {
        }

        public byte GetTypeId(string typeName)
        {
            if (_cache.TryGetValue(typeName, out var b))
                return b;
            Creatureliblibrary.Generated.TypeLibrary.GetTypeId(ref b, Ptr, typeName.ToPtr()).Assert();
            _cache.Add(typeName, b);
            return b;
        }

        public byte RegisterType(string typeName)
        {
            byte b = 0;
            Creatureliblibrary.Generated.TypeLibrary.RegisterType(ref b, Ptr, typeName.ToPtr()).Assert();
            _cache.Add(typeName, b);
            return b;
        }

        public void SetEffectiveness(byte attackingId, byte defensiveId, float effectiveness)
        {
            Creatureliblibrary.Generated.TypeLibrary.SetEffectiveness(Ptr, attackingId, defensiveId, effectiveness);
        }

        public float GetSingleEffectiveness(byte attackingId, byte defensiveId)
        {
            float f = 0;
            Creatureliblibrary.Generated.TypeLibrary.GetSingleEffectiveness(ref f, Ptr, attackingId, defensiveId)
                .Assert();
            return f;
        }

        public float GetEffectiveness(byte attackingId, byte[] defensiveIds)
        {
            float f = 0;
            Creatureliblibrary.Generated.TypeLibrary.GetEffectiveness(ref f, Ptr, attackingId, defensiveIds.ArrayPtr(),
                (ulong) defensiveIds.Length).Assert();
            return f;
        }

        protected override void DeletePtr()
        {
            Creatureliblibrary.Generated.TypeLibrary.Destruct(Ptr);
        }
    }
}