using UnityEngine;
using VOID.Generic.Objects.Abstract;

namespace VOID.Generic.Objects
{
    public abstract class ObjectComponent<T> : ObjectComponent where T : AbstractObject
    {
        protected T parent => (T)objectParent;
    }
    
    public abstract class ObjectComponent
    {
        protected AbstractObject objectParent;
        private bool _didSelfInitialize;
        private bool _didEventInitialize;
        private bool _didInitialize;

        public void SetParent(AbstractObject objectParent)
        {
            this.objectParent = objectParent;
        }

        public void DoSelfInitialize()
        {
            if (_didSelfInitialize)
            {
                Debug.LogError($"{GetType().Name} already did {nameof(SelfInitialize)}", objectParent);
                return;
            }

            _didSelfInitialize = true;
            SelfInitialize();
        }

        public void DoEventInitialize()
        {
            if (_didEventInitialize)
            {
                Debug.LogError($"{GetType().Name} already did {nameof(DoEventInitialize)}", objectParent);
                return;
            }

            _didEventInitialize = true;
            EventInitialize();
        }

        public void DoInitialize()
        {
            if (_didInitialize)
            {
                Debug.LogError($"{GetType().Name} already did {nameof(Initialize)}", objectParent);
                return;
            }
            
            _didInitialize = true;
            Initialize();
        }
        
        protected virtual void SelfInitialize()
        {
        }
        
        protected virtual void EventInitialize()
        {
        }
        
        protected virtual void Initialize()
        {
        }
    }
}