using System;
using System.Linq;
using PkmnLibSharp.Utilities;

namespace PkmnLibSharp.Library.Items
{
    public class Item : PointerWrapper
    {
        private string _name;

        public string Name => _name ??= Creatureliblibrary.Generated.Item.GetName(Ptr).PtrString();
        public ItemCategory Category => (ItemCategory) Creatureliblibrary.Generated.Item.GetCategory(Ptr);

        public BattleItemCategory BattleCategory =>
            (BattleItemCategory) Creatureliblibrary.Generated.Item.GetBattleCategory(Ptr);

        public int Price => Creatureliblibrary.Generated.Item.GetPrice(Ptr);
        public byte FlingPower => Pkmnlib.Generated.Item.GetFlingPower(Ptr);

        public bool HasFlag(string s)
        {
            return Creatureliblibrary.Generated.Item.HasFlag(Ptr, s.ToPtr()) == MarshalHelper.True;
        }

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

        public static Item Create(string name, ItemCategory category, BattleItemCategory battleCategory,
            int price,
            string[] flags, byte flingPower)
        {
            var convertedFlags = flags.Select(x => x.ToPtr()).ToArray().ArrayPtr();
            var p = Pkmnlib.Generated.Item.Construct(name.ToPtr(), (Pkmnlib.ItemCategory) category,
                (Pkmnlib.BattleItemCategory) battleCategory, price, convertedFlags, (ulong) flags.Length, flingPower);
            return new Item(p);
        }

        protected override void DeletePtr()
        {
            Pkmnlib.Generated.Item.Destruct(Ptr);
        }
    }
}