using System;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace WS
{
    ///<summary>场景管理</summary>
    public class SceneComponent : SingleComponent<SceneComponent>
    {
        ///<summary>当前场景</summary>
        public string ThisScene { get { return SceneManager.GetActiveScene().name; } }

        ///<summary>加载场景</summary>
        ///<param name="name">场景名称</param>
        public void LoadScene(string name)
        {
            Release();
            SceneManager.LoadScene(name);
        }

        /// <summary>加载场景</summary>
        /// <param name="name">场景名称</param>
        /// <param name="oncallback">加载回调</param>
        public void LoadScene(string name, Action oncallback = null)
        {
            LoadScene(name, null, ViewShowModeType.Screen, oncallback);
        }

        /// <summary>加载场景</summary>
        /// <param name="name">场景名称</param>
        /// <param name="describe">加载描述</param>
        /// <param name="oncallback">加载回调</param>
        public void LoadScene(string name, string describe, Action oncallback = null)
        {
            LoadScene(name, describe, ViewShowModeType.Screen, oncallback);
        }

        /// <summary>加载场景</summary>
        /// <param name="name">场景名称</param>
        /// <param name="describe">加载描述</param>
        /// <param name="oncallback">加载回调</param>
        public void LoadScene(string name, string describe, ViewShowModeType viewShowMode, Action oncallback = null)
        {
            //LoadEffectComponent effectComponent = Game.Scene.GetComponent<LoadEffectComponent>();
            //effectComponent?.PlayLoadEffect(true);
            Release();
            SceneChange change = ComponentFactory.CreateWithParent<SceneChange>(this);
            if (string.IsNullOrEmpty(describe))
            {
                describe = "加载场景中,请稍后...";
            }
            LoadingController controller = FacadeComponent.Instance.CreateController<LoadingController>();
            controller.OpenView(viewShowMode, change, describe);
            Action action = delegate { };
            action += delegate
            {
                controller.CloseView();
                //effectComponent?.PlayLoadEffect(false);
                change.Dispose();
            };
            if (oncallback != null)
            {
                action += oncallback;
            }
            change.LoadScene(name, action);
        }

        /// <summary>异步加载场景</summary>
        /// <param name="name">场景名称</param>
        public async Task LoadSceneAsync(string name)
        {
            await LoadSceneAsync(name, null);
        }

        /// <summary>异步加载场景</summary>
        /// <param name="name">场景名称</param>
        /// <param name="describe">加载描述</param>
        public async Task LoadSceneAsync(string name, string describe)
        {
            //LoadEffectComponent effectComponent = Game.Scene.GetComponent<LoadEffectComponent>();
            //effectComponent?.PlayLoadEffect(true);
            Release();
            SceneChange change = ComponentFactory.CreateWithParent<SceneChange>(this);
            if (string.IsNullOrEmpty(describe))
            {
                describe = "加载场景中,请稍后...";
            }
            LoadingController controller = FacadeComponent.Instance.CreateController<LoadingController>();
            controller.OpenView(ViewShowModeType.Screen, change, describe);
            Action action = delegate
            {
                controller.CloseView();
                //effectComponent?.PlayLoadEffect(false);
                change.Dispose();
            };
            await change.LoadSceneAsync(name, action);
        }

        ///<summary>释放无用资源/内存</summary>
        private void Release()
        {
            GC.Collect();
            Resources.UnloadUnusedAssets();
        }
    }
}