using System;
using PkmnLibSharp.Utilities;

namespace PkmnLibSharp.Battling
{
    public class BattleParty : PointerWrapper
    {
        public BattleParty(PokemonParty party, byte[] responsibleIndices)
        {
            var ptr = IntPtr.Zero;
            Creaturelib.Generated.BattleParty.Construct(ref ptr, party.Ptr, responsibleIndices.ArrayPtr(),
                (ulong) responsibleIndices.Length / 2).Assert();
            Initialize(ptr);
        }

        private PokemonParty? _party;
        public PokemonParty Party
        {
            get
            {
                if (_party != null) return _party;
                var ptr = Creaturelib.Generated.BattleParty.GetParty(Ptr);
                if (TryResolvePointer(ptr, out _party))
                    return _party!;
                _party = new PokemonParty(ptr);
                return _party;
            }
        }

        public bool IsResponsibleForIndex(byte side, byte index)
        {
            byte result = 0;
            Creaturelib.Generated.BattleParty.IsResponsibleForIndex(ref result, Ptr, side, index).Assert();
            return result == 1;
        }
        
        public bool HasPokemonNotInField()
        {
            return Creaturelib.Generated.BattleParty.HasCreaturesNotInField(Ptr) == 1;
        }

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