using FairyGUI;
using UI.Launcher;
using UnityEngine;
namespace GFGGame
{
    public class LauncherView
    {
        private static LauncherView m_Instance = null;
        /// 
        /// 单例
        /// 
        public static LauncherView Instance
        {
            get
            {
                if (m_Instance == null)
                {
                    m_Instance = new LauncherView();
                }
                return m_Instance;
            }
        }
        private UI_LauncherUI _ui;
        /// 
        /// 每1%耗时,单位秒
        /// 
        private const float SPEED = 0.01f;
        /// 
        /// 界面是否打开状态
        /// 
        private bool isOpen = false;
        /// 
        /// FairyGUI包名
        /// 
        private string _packageName;
        #region private
        private void Dispose()
        {
            UIPackage.RemovePackage("UI/" + _packageName);
            _ui.Dispose(true);
            _ui = null;
        }
        #endregion
        public LauncherView()
        {
            _packageName = UI_LauncherUI.PACKAGE_NAME;
            UIPackage.AddPackage("UI/" + _packageName + "/" + _packageName);
            _ui = UI_LauncherUI.Create();
            _ui.target.MakeFullScreen();
            _ui.target.AddRelation(GRoot.inst, RelationType.Size);
            _ui.m_txtVersion.text = Application.version;
        }
        /// 
        /// 设置版本号文字
        /// 
        /// 
        public void SetVersion(string version)
        {
            if(!isOpen)
            {
                return;
            }
            _ui.m_txtVersion.text = version;
        }
        /// 
        /// 设置描述文字
        /// 
        /// 
        public void SetDesc(string desc, string descRight = null)
        {
            if (!isOpen)
            {
                return;
            }
            _ui.m_txtDescLeft.text = desc??"";
            _ui.m_txtDescRight.text = descRight??"";
        }
        /// 
        /// 设置进度0-100
        /// 
        /// 
        /// 
        public void SetProgress(long progress, GTweenCallback callback = null)
        {
            if (!isOpen)
            {
                return;
            }
            GTweener twener = GTween.GetTween(_ui.m_progressBar, TweenPropType.Progress);
            if (twener != null)
            {
                twener.Kill(true);
            }
            if(progress < _ui.m_progressBar.value)
            {
                _ui.m_progressBar.value = progress;
                callback?.Invoke();
            }
            else
            {
                float duration = (float)(progress - _ui.m_progressBar.value) * SPEED;
                GTweener gtweener = _ui.m_progressBar.TweenValue(progress, duration);
                if (callback != null)
                {
                    gtweener.OnComplete(callback);
                }
            }
        }
        /// 
        /// 打开界面
        /// 
        public void Open()
        {
            if (isOpen)
            {
                return;
            }
#if (UNITY_5 || UNITY_5_3_OR_NEWER)
            //Use the font names directly
            UIConfig.defaultFont = "Microsoft YaHei";
#else
        //Need to put a ttf file into Resources folder. Here is the file name of the ttf file.
        // UIExcelConfig.defaultFont = "afont";
#endif
            _ui.m_progressBar.value = 0;
            _ui.m_txtDescLeft.text = "";
            _ui.m_txtDescRight.text = "";
            GRoot.inst.AddChild(_ui.target);
            isOpen = true;
            _ui.m_imgLogo.visible = LauncherConfig.netType != LauncherConfig.EnumNetType.TEMP;
        }
        /// 
        /// 关闭界面
        /// 
        /// 
        public void Close(bool toDestroy = false)
        {
            if (!isOpen)
            {
                return;
            }
            isOpen = false;
            _ui.target.RemoveFromParent();
            if (toDestroy)
            {
                Dispose();
            }
        }
    }
}