using System;
using PkmnLibSharp.Utilities;

namespace PkmnLibSharp.Battling.ChoiceTurn
{
    public class MoveTurnChoice : BaseTurnChoice
    {
        public MoveTurnChoice(Pokemon user, LearnedMove move, byte targetSide, byte targetIndex)
            : base(Creaturelib.Generated.AttackTurnChoice.Construct(user.Ptr, move.Ptr, targetSide, targetIndex))
        {
        }
        
        internal MoveTurnChoice(IntPtr ptr) : base(ptr){} 

        public LearnedMove Move
        {
            get
            {
                if (_move != null) return _move;
                var ptr = Creaturelib.Generated.AttackTurnChoice.GetAttack(Ptr);
                if (TryResolvePointer(ptr, out _move))
                    return _move!;
                _move = new LearnedMove(ptr);
                return _move;
            }
        }

        public sbyte Priority
        {
            get
            {
                sbyte b = 0;
                Creaturelib.Generated.AttackTurnChoice.GetPriority(ref b, Ptr).Assert();
                return b;
            }
        }

        public byte TargetSide => Creaturelib.Generated.AttackTurnChoice.GetTargetSideIndex(Ptr);
        public byte TargetIndex => Creaturelib.Generated.AttackTurnChoice.GetTargetCreatureIndex(Ptr);

        // TODO: Move Script getter

        private LearnedMove? _move;

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