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

namespace PkmnLibSharp.Library.Items
{
    public class Item : PointerWrapper
    {
        private string? _name;
        
        public string Name => _name ??= Creaturelib.Generated.Item.GetName(Ptr).PtrString()!;
        public ItemCategory Category => (ItemCategory) Creaturelib.Generated.Item.GetCategory(Ptr);

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

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

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

        public Item (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);
            Initialize(p);
        }

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