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;
        private GameObject _gameObject;
        private GoWrapper _wrapper;
        private GameObject _gameObject1;
        private GoWrapper _wrapper1;
        private string[] resNames = { "11", "33" };
        /// 
        /// 每1%耗时,单位秒
        /// 
        private const float SPEED = 0.01f;
        /// 
        /// 界面是否打开状态
        /// 
        private bool isOpen = false;
        /// 
        /// FairyGUI包名
        /// 
        private string _packageName;
        #region private
        private void Dispose()
        {
            if (_gameObject != null)
            {
                GameObject.Destroy(_gameObject);
                _gameObject = null;
                _wrapper.Dispose();
                _wrapper = null;
            }
            if (_gameObject1 != null)
            {
                GameObject.Destroy(_gameObject1);
                _gameObject1 = null;
                _wrapper1.Dispose();
                _wrapper1 = null;
            }
            UIPackage.RemovePackage("UI/" + _packageName + "/" + _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;
            string resPath = "Effect/ui_login/Game_Open_jindut_T";
            var prefab = Resources.Load(resPath);
            _gameObject = GameObject.Instantiate(prefab);
            _gameObject.transform.localScale = new Vector3(100, 100, 100);
            _wrapper = new GoWrapper(_gameObject);
            _ui.m_progressBar1.m_holderStar.SetNativeObject(_wrapper);
            string resPath1 = "Effect/ui_login/Game_Open_jindut";
            var prefab1 = Resources.Load(resPath1);
            _gameObject1 = GameObject.Instantiate(prefab1);
            _gameObject1.transform.localScale = new Vector3(100, 100, 100);
            _wrapper1 = new GoWrapper(_gameObject1);
            _ui.m_progressBar1.m_bar1.m_holder.SetNativeObject(_wrapper1); 
            _ui.m_groupBar.visible = false;
        }
        /// 
        /// 设置版本号文字
        /// 
        /// 
        public void SetVersion(string version)
        {
            if (!isOpen)
            {
                return;
            }
            _ui.m_txtVersion.text = version;
        }
        /// 
        /// 设置描述文字
        /// 
        /// 
        public void SetDesc(string desc, string descRight = "", bool showBar = false)
        {
            if (!isOpen)
            {
                return;
            }
            _ui.m_groupBar.visible = showBar;
            string str = string.Format("{0}  {1}", desc, descRight);//  + descRight;
            _ui.m_txtDescLeft.text = str;
            // _ui.m_txtDescLeft.text = desc ?? "";
            // _ui.m_txtDescRight.text = descRight ?? "";
        }
        /// 
        /// 设置进度0-100
        /// 
        /// 
        /// 
        public void SetProgress(long progress, GTweenCallback callback = null)
        {
            if (!isOpen)
            {
                return;
            }
            double oldValule;
            GTweener twener = GTween.GetTween(_ui.m_progressBar1, TweenPropType.Progress);
            if (twener != null)
            {
                oldValule = twener.value.d;
            }
            else
            {
                oldValule = _ui.m_progressBar1.target.value;
            }
            if (progress < oldValule)
            {
                _ui.m_progressBar1.target.value = progress;
                float posX = _ui.m_progressBar1.target.width * (progress / 100);
                // _ui.m_progressBar1.m_holder.x = Mathf.Min(posX, _ui.m_progressBar1.target.width);
                _ui.m_progressBar1.m_bar1.m_mask.width = posX;
                _ui.m_progressBar1.m_holderStar.x = posX;
                callback?.Invoke();
            }
            else
            {
                float duration = (float)(progress - oldValule) * SPEED;
                GTweener gtweener = _ui.m_progressBar1.target.TweenValue(progress, duration).OnUpdate((GTweener t) =>
                {
                    float posX = _ui.m_progressBar1.target.width * (t.value.x / 100);
                    // _ui.m_progressBar1.m_holder.x = Mathf.Min(posX, _ui.m_progressBar1.target.width);
                    _ui.m_progressBar1.m_bar1.m_mask.width = posX;
                    _ui.m_progressBar1.m_holderStar.x = posX;
                });
                if (callback != null)
                {
                    gtweener.OnComplete(callback);
                }
            }
        }
        /// 
        /// 打开界面
        /// 
        public void Open()
        {
            if (isOpen)
            {
                return;
            }
            _ui.m_progressBar1.target.value = 0;
            _ui.m_txtDescLeft.text = "";
            _ui.m_txtDescRight.text = "";
            GRoot.inst.AddChild(_ui.target);
            isOpen = true;
        }
        /// 
        /// 关闭界面
        /// 
        /// 
        public void Close(bool toDestroy = true)
        {
            if (!isOpen)
            {
                return;
            }
            isOpen = false;
            _ui.target.RemoveFromParent();
            if (toDestroy)
            {
                Dispose();
            }
        }
    }
}