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

namespace XGame.Framework.UI
{
    /// <summary>
    /// 每个UIView一个UIContext实例
    /// </summary>
    public class UIContext : IDisposable
    {
        public IQualityModule Quality { get; private set; }
        public ITimeModule Time { get; private set; }
        public IUIModule UI { get; private set; }
        /// <summary>
        /// 资源加载模块，需要时才创建
        /// </summary>
        private IUIAssetModule _asset;
        /// <summary>
        /// 资源加载模块
        /// </summary>
        public IUIAssetModule Asset => _asset ?? (_asset = new UIAssetModule());

        public UIContext(IQualityModule qualityModule, ITimeModule timeModule, IUIModule uiModule)
        {
            Quality = qualityModule;
            Time = timeModule;
            UI = uiModule;
        }

        public UIContext Clone()
        {
            return new UIContext(Quality, Time, UI);
        }
        /// <summary>
        /// 停止所有的加载事件
        /// </summary>
        public void StopAllAsyncs()
        {
            _asset?.StopAllAsyncs();
        }

        void IDisposable.Dispose()
        {
            Quality = null;
            Time = null;
            UI = null;
            (_asset as IDisposable)?.Dispose();
            _asset = null;
        }
    }

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