using System;
using XGame.Framework.Interfaces;

namespace XGame.Framework.Components
{
    /// <summary>
    /// 逻辑行为组件基类
    /// </summary>
    /// <typeparam name="TContext"></typeparam>
    public abstract class Component<TContext> : IComponent, IReference, IActiveable, IDisposable
    {
        public TContext Context { get; private set; }
        public virtual int Order => 0;

        void IComponent.Bind(object handle)
        {
            Context = (TContext)handle;
        }
        public bool Active { get; private set; }
        void IActiveable.Enable(object intent)
        {
            if (Active) return;
            Active = true;
            OnEnable(intent);
        }


        void IActiveable.Disable()
        {
            if (!Active) return;
            Active = false;
            OnDisable();
        }
        void IReference.Clear()
        {
            OnDispose();
        }
        void IDisposable.Dispose()
        {
            OnDispose();
            Context = default;
        }
        protected virtual void OnEnable(object intent)
        {
        }
        protected virtual void OnDisable()
        {
        }
        /// <summary>
        /// 销毁事件回调
        /// 缓存池回收也会触发OnDispose，但不真正销毁
        /// </summary>
        protected abstract void OnDispose();


    }
}
