using UnityEngine;
using XGame.Framework.Asset;
using XGame.Framework.Asyncs;

namespace XGame.Framework
{
    public partial class AppManager : IAppManager
    {
        private GameObject _go;
        private Launch _launch;
        private AppStatus _status = AppStatus.Closed;

        Transform IAppManager.Root => _go.transform;
        /// <summary>
        /// 启动
        /// </summary>
        void IAppManager.Start()
        {
            switch (_status)
            {
                case AppStatus.Started:
                    {
                        Log.Error("AppManager::Start  App已启动...");
                        break;
                    }
                case AppStatus.Restarting:
                    {
                        Log.Error("AppManager::Start  App正在重启中...");
                        break;
                    }
                case AppStatus.Closing:
                    {
                        Log.Error("AppManager::Start  App正在关闭中...");
                        break;
                    }
                case AppStatus.Closed:
                    {
                        DoStart();
                        _status = AppStatus.Started;
                        break;
                    }
                default:
                    break;
            }
        }

        /// <summary>
        /// 重启
        /// </summary>
        IAsync IAppManager.Restart()
        {
            switch (_status)
            {
                case AppStatus.Started:
                    {
                        _status = AppStatus.Restarting;
                        IAsync assetAsync = Shutdown();
                        if (assetAsync == null)
                        {
                            DoStart();
                            _status = AppStatus.Started;
                        }
                        else
                        {
                            assetAsync.Then(a =>
                            {
                                DoStart();
                                _status = AppStatus.Started;
                            });
                        }
                        return assetAsync;
                    }
                case AppStatus.Restarting:
                    {
                        Log.Warn("AppManager::Restart  App正在重启中....");
                        return null;
                    }
                case AppStatus.Closing:
                    {
                        Log.Warn("AppManager::Restart  App正在关闭中....");
                        return null;
                    }
                case AppStatus.Closed:
                    {
                        DoStart();
                        _status = AppStatus.Started;
                        return null;
                    }
                default:
                    return null;
            }
        }

        /// <summary>
        /// 关闭
        /// </summary>
        IAsync IAppManager.Close()
        {
            switch (_status)
            {
                case AppStatus.Started:
                    {
                        _status = AppStatus.Closing;

                        IAsync assetAsync = Shutdown();
                        if (assetAsync == null)
                            _status = AppStatus.Closed;
                        else
                            assetAsync.Then(a => _status = AppStatus.Closed);
                        return assetAsync;
                    }
                case AppStatus.Restarting:
                    {
                        Log.Error("AppManager::Close  App正在关闭中");
                        return null;
                    }
                case AppStatus.Closing:
                    {
                        Log.Error("AppManager::Close  App正在关闭中");
                        return null;
                    }
                case AppStatus.Closed:
                    {
                        Log.Error("AppManager::Close  App还未启动...");
                        return null;
                    }
                default:
                    return null;
            }
        }

        private void DoStart()
        {
            _go = new GameObject("Root");
            _launch = _go.AddComponent<Launch>();
            Object.DontDestroyOnLoad(_go);
        }


        private IAsync Shutdown()
        {
            _status = AppStatus.Closing;
            OnShutdown();
            return AssetManager.Dispose();
        }

        private void OnShutdown()
        {
            _launch?.Dispose();
            _launch = null;
            if (_go)
            {
                Object.Destroy(_go);
                _go = null;
            }
            System.GC.Collect();
        }
    }
}
