using System;
using XGame.Framework.Quality;
using XGame.Framework.Time;

namespace XGame.Framework.UI
{
    /// <summary>
    /// 每个UIView一个UIContext实例
    /// </summary>
    public sealed class UIContext : IUIViewAdapter, IDisposable
    {
        public IQualityModule Quality { get; private set; }

        private ITimeModule _timeModule;
        private ITimeProxy _timeProxy;
        public ITimeProxy Time => _timeProxy ?? (_timeProxy = new TimeProxy(_timeModule));
        public IUIModule UI { get; private set; }
        /// <summary>
        /// 资源加载模块，需要时才创建
        /// </summary>
        private IUIAssetModule _asset;
        /// <summary>
        /// 资源加载模块
        /// </summary>
        public IUIAssetModule Asset => _asset ?? (_asset = (UI as IUIAssetModuleCreator).Create(this));

        public UIContext(IQualityModule qualityModule, ITimeModule timeModule, IUIModule uiModule)
        {
            Quality = qualityModule;
            _timeModule = timeModule;
            UI = uiModule;
        }
        public UIContext Clone()
        {
            return new UIContext(Quality, _timeModule, UI);
        }
        /// <summary>
        /// 关闭UI
        /// </summary>
        /// <param name="isDestroy"></param>
        public void ClosePanel(bool isDestroy = false)
        {
            UI.Close(_uikey, isDestroy);
        }
        /// <summary>
        /// 取消定时器、清理资源加载监听等
        /// </summary>
        public void Clear()
        {
            _timeProxy?.CancelAll();
            _asset?.StopAllAsyncs();
        }

        void IDisposable.Dispose()
        {
            Quality = null;
            _timeModule = null;
            UI = null;
            _timeProxy?.CancelAll();
            _timeProxy = null;
            (_asset as IDisposable)?.Dispose();
            _asset = null;
            _ctrlGroup = null;
            _uikey = null;
        }
        /// <summary>
        /// Context对应的UIKey
        /// 业务不要赋值
        /// </summary>
        private UIKey _uikey;
        public UIKey Key => _uikey;
        UIKey IUIViewAdapter.Key { set => _uikey = value; }

        private IUIControllerGroup _ctrlGroup;
        IUIControllerGroup IUIViewAdapter.CtrlGroup { get => _ctrlGroup; set => _ctrlGroup = value; }

        TNestedView IUIViewAdapter.CreateNested<TNestedView>(IUINested nested, bool isStatic)
        {
            var nestedView = Activator.CreateInstance<TNestedView>();
            (nestedView as INestedInternal).Init(this, nested, isStatic);
            return nestedView;
        }
        INestedView IUIViewAdapter.CreateNested(Type nestedViewType, IUINested nested, bool isStatic)
        {
            var nestedView = Activator.CreateInstance(nestedViewType) as INestedInternal;
            if (nestedView == null)
            {
                Log.Error($"CreateNested 类型错误. Type:{nestedViewType} isStatic:{isStatic}");
            }
            else
            {
                nestedView.Init(this, nested, isStatic);
            }
            return nestedView;
        }
    }

    public interface IUIContextSetter
    {
        UIContext Context { set; }
    }
}
