using System;
using PkmnLibSharp.Library;
using PkmnLibSharp.Utilities;

namespace PkmnLibSharp.Battling.Events
{
    public class ChangeSpeciesEvent : EventData
    {
        internal ChangeSpeciesEvent(EventDataKind kind, IntPtr ptr) : base(kind, ptr)
        {
        }
        
        public Pokemon Pokemon
        {
            get
            {
                if (_pokemon != null) return _pokemon;
                var ptr = Creaturelib.Generated.ChangeSpeciesEvent.GetCreature(Ptr);
                if (TryResolvePointer(ptr, out _pokemon))
                    return _pokemon!;
                _pokemon = Constructor.Active.ConstructPokemon(ptr)!;
                return _pokemon;
            }
        }
        
        public Species NewSpecies
        {
            get
            {
                if (_newSpecies != null) return _newSpecies;
                var ptr = Creaturelib.Generated.ChangeSpeciesEvent.GetNewSpecies(Ptr);
                if (TryResolvePointer(ptr, out _newSpecies))
                    return _newSpecies!;
                _newSpecies = Constructor.Active.ConstructSpecies(ptr)!;
                return _newSpecies;
            }
        }


        private Pokemon? _pokemon;
        private Species? _newSpecies;
        protected override void DeletePtr()
        {
            Creaturelib.Generated.ChangeSpeciesEvent.Destruct(Ptr);
        }
    }
}