using UnityEngine;
using BingoBrain.HotFix;
using System.Collections;
using BingoBrain;

namespace BingoBrain.Core
{
    public class BingoBea : Bea
    {
        private static BingoBea m_instance;

public string attribution = "organic";
        public static BingoBea Instance
        {
            get
            {
                if (m_instance == null)
                {
                    if (IsAppQuit)
                    {
                        return null;
                    }

                    OCConst.ApplicationGo = new GameObject(OCConst.ApplicationGoName);
                    OCConst.ApplicationGo.SetParent(OCConst.bfdn);
                    m_instance = OCConst.ApplicationGo.AddComponent<BingoBea>();
                }

                return m_instance;
            }
        }
        private void OnApplicationFocus(bool focus)
        {
            //IsAppFocus = focus;
            if (focus)
            {
                // MainThreadDispatcher.Instance.Dispatch(GinInfoC.App_Focus_True, focus);
                AppDispatcher.Instance.Dispatch(GinInfoC.App_Focus_True, focus);

            }
            else
            {
               // LastFocusFlaseTime = DateTimeBoardk.Instance.GetServerCurrTimestamp();
                AppDispatcher.Instance.Dispatch(CsjInfoC.App_Focus_False, focus);
            }
        }
        public override void Init()
        {
            base.Init();
            if (!RrysKit.HasKey(FsyConst.App_isNewInstall))
            {
                RrysKit.WriteInt(FsyConst.App_isNewInstall, 1);
            }
            else
            {
            }

            AppDispatcher.Instance.AddListener(CsjInfoC.AppManagerRegister,
                (obj) => { BingoBoard.SetData(); });
        }

        public override void Enable()
        {
            base.Enable();

            InitPlugin();
            InitAppSetting();

            ManagerBoard.Register();
            BingoBoard.Board();
            ManagerBoard.RegisterData();

            Ard.Instance.Init();
            ModuleBoardk.Instance.StartUpAllModule();

            InitSettingMode();
            StartUpGameMain();
        }

        private void OnDestroy()
        {
            m_instance = null;
        }

        #region Enable

        private void InitPlugin()
        {
            Bsaddd.Init();
        }

        private void InitAppSetting()
        {
            if (!BingoConst.UseInternalSetting) return;


            Physics.autoSimulation = true;
            Physics.autoSyncTransforms = true;

            Physics2D.simulationMode = SimulationMode2D.Script;
            Physics2D.autoSyncTransforms = true;

            Debug.unityLogger.logEnabled = BingoConst.IsEnabledEngineLog;
            Debug.unityLogger.filterLogType = BingoConst.EnabledFilterLogType;
            Screen.sleepTimeout = BingoConst.SleepTimeoutMode;
            Application.runInBackground = BingoConst.IsRunInBG;
            QualitySettings.vSyncCount = 0;
            QualitySettings.lodBias = 1;
            QualitySettings.antiAliasing = BingoConst.AntiAliasing;
            Application.targetFrameRate = BingoConst.LowFrameRate;
        }

        private void StartUpGameMain()
        {
            if (!IsRestart)
            {
                GameBoardk.Instance.InitialMain();
            }
            else
            {
                GameBoardk.Instance.EnterMain();
            }
        }

        #endregion Enable

        #region SettingMode

        private void InitSettingMode()
        {
            if (!BingoConst.UseInternalSetting) return;
            InitResolutionMode();
            InitFrameRateMode();
        }

        private bool isHDMode;

        public bool IsHDMode
        {
            get { return isHDMode; }
            set
            {
                isHDMode = value;
                RrysKit.WriteBool(FsyConst.Application_isHDMode, isHDMode);
                SetResolutionMode(isHDMode);
            }
        }

        private bool isHFRMode;

        public bool IsHFRMode
        {
            get { return IsHFRMode; }
            set
            {
                isHFRMode = value;
                RrysKit.WriteBool(FsyConst.Application_isHFRMode, isHFRMode);
                SetFrameRateMode(isHFRMode);
            }
        }

        private void InitResolutionMode()
        {
            isHDMode = RrysKit.ReadBool(FsyConst.Application_isHDMode, true);
            SetResolutionMode(isHDMode);
        }

        private void InitFrameRateMode()
        {
            isHFRMode = RrysKit.ReadBool(FsyConst.Application_isHFRMode, true);
            SetFrameRateMode(isHFRMode);
        }

        private void SetResolutionMode(bool isHDMode)
        {
            if (isHDMode)
            {
                CenConst.CurrResolution.x = CenConst.RawResolution.x * BingoConst.HDHighViewScale;
                CenConst.CurrResolution.y = CenConst.RawResolution.y * BingoConst.HDHighViewScale;
            }
            else
            {
                CenConst.CurrResolution.x = CenConst.RawResolution.x * BingoConst.HDLowViewScale;
                CenConst.CurrResolution.y = CenConst.RawResolution.y * BingoConst.HDLowViewScale;
            }

            SetScreenResolution(CenConst.CurrResolution.x, CenConst.CurrResolution.y, true);
        }

        private void SetFrameRateMode(bool isHFRMode)
        {
            Application.targetFrameRate = isHFRMode ? BingoConst.HighFrameRate : BingoConst.LowFrameRate;


            QualitySettings.vSyncCount = 0;
            QualitySettings.lodBias = 1;
        }

        private void SetScreenResolution(float width, float height, bool isFullScreen)
        {
            StartCoroutine(OnSetScreenResolution(width, height, isFullScreen));
        }

        private IEnumerator OnSetScreenResolution(float width, float height, bool isFullScreen)
        {
            yield return vbadConst.WaitForEndOfFrame;
            var allCams = Camera.allCameras;
            if (allCams == null)
            {
                yield break;
            }

            foreach (var cam in allCams)
            {
                cam.enabled = false;
            }

            Screen.SetResolution((int)width, (int)height, isFullScreen);
            Screen.fullScreen = true;

            yield return vbadConst.WaitForEndOfFrame;
            if (allCams == null)
            {
                yield break;
            }

            foreach (var cam in allCams)
            {
                cam.enabled = true;
            }
        }

        #endregion SettingMode
    }
}