using System;
using Creaturelib;
using Creaturelib.Generated;
using PkmnLibSharp.Library.Moves;
using PkmnLibSharp.Utilities;

namespace PkmnLibSharp.Battling
{
    public class LearnedMove : PointerWrapper
    {
        internal LearnedMove(IntPtr ptr) : base(ptr){}
        
        public LearnedMove(MoveData move, byte maxUses, MoveLearnMethod learnMethod)
        {
            Initialize(CreatePtr(move, maxUses, learnMethod));
        }

        protected static IntPtr CreatePtr(MoveData move, byte maxUses, MoveLearnMethod learnMethod)
        {
            var ptr = IntPtr.Zero;
            LearnedAttack.Construct(ref ptr, move.Ptr, maxUses, (AttackLearnMethod) learnMethod).Assert();
            return ptr;
        }

        public MoveData Move
        {
            get
            {
                if (_move != null) return _move;
                var ptr = LearnedAttack.GetAttack(Ptr);
                if (TryResolvePointer(ptr, out _move))
                    return _move!;
                _move = Constructor.Active.ConstructMove(ptr)!;
                return _move;
            }
        }

        public byte MaxUses => Creaturelib.Generated.LearnedAttack.GetMaxUses(Ptr);
        public byte RemainingUses => Creaturelib.Generated.LearnedAttack.GetRemainingUses(Ptr);
        public MoveLearnMethod LearnMethod =>
            (MoveLearnMethod) Creaturelib.Generated.LearnedAttack.GetLearnMethod(Ptr);

        private MoveData? _move;
        
        protected override void DeletePtr()
        {
            LearnedAttack.Destruct(Ptr);
        }
    }
}