using System;
using PkmnLibSharp.Battling.ChoiceTurn;
using PkmnLibSharp.Utilities;

namespace PkmnLibSharp.Battling
{
    public class BattleSide : PointerWrapper
    {
        internal BattleSide(IntPtr ptr) : base(ptr){}

        public byte SideIndex => Creaturelib.Generated.BattleSide.GetSideIndex(Ptr);
        public bool AllChoicesSet => Creaturelib.Generated.BattleSide.AllChoicesSet(Ptr) == 1;
        public bool IsDefeated => Creaturelib.Generated.BattleSide.IsDefeated(Ptr) == 1;
        public bool HasFled => Creaturelib.Generated.BattleSide.HasFled(Ptr) == 1;
        public bool AllPossibleSlotsFilled
        {
            get
            {
                byte b = 0;
                Creaturelib.Generated.BattleSide.AllPossibleSlotsFilled(ref b, Ptr).Assert();
                return b == 1;
            }
        }

        public byte IndexOf(Pokemon pokemon)
        {
            byte b = 0;
            Creaturelib.Generated.BattleSide.GetCreatureIndex(ref b, Ptr, pokemon.Ptr).Assert();
            return b;
        }

        public void SetChoice(BaseTurnChoice choice)
        {
            Creaturelib.Generated.BattleSide.SetChoice(Ptr, choice.Ptr).Assert();
        }

        public void ResetChoice()
        {
            Creaturelib.Generated.BattleSide.ResetChoices(Ptr);
        }

        public void SetPokemon(Pokemon pokemon, byte index)
        {
            Creaturelib.Generated.BattleSide.SetCreature(Ptr, pokemon.Ptr, index);
        }

        public Pokemon GetPokemon(byte index)
        {
            var ptr = IntPtr.Zero;
            Creaturelib.Generated.BattleSide.GetCreature(ref ptr, Ptr, index);
            if (TryResolvePointer(ptr, out Pokemon? pokemon))
                return pokemon!;
            return Constructor.Active.ConstructPokemon(ptr)!;
        }

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