using UnityEngine;
using DG.Tweening;
using System.Collections;
using UnityEngine.Android;

namespace BingoBrain.Core
{
    public class Bea : MonoBehaviour
    {
        public static bool IsAppQuit { get; private set; }

        public bool IsRestart { get; private set; }
        public int RestartCount { get; private set; }
        public bool IsAppFocus { get; private set; }
        public bool IsAppPause { get; private set; }
        public long LastFocusFlaseTime { get; private set; }

        public virtual void Init()
        {
            IsAppFocus = true;
        }

        public virtual void Enable()
        {
            CreateEnvironment();
        }

        public virtual void Restart()
        {
            Debug.Log("[Application]Restart");
            IsRestart = true;
            RestartCount++;
            StartCoroutine(OnRestart());
        }

        public virtual void Quit()
        {
#if UNITY_EDITOR
            UnityEditor.EditorApplication.isPlaying = false;
#else
            Application.Quit();
#endif
        }

        private void CreateEnvironment()
        {
            OCConst.ApplicationGo = gameObject;
            OCConst.EngineSingletonGo = new GameObject(OCConst.EngineSingletonGoName);
            OCConst.EngineSingletonGo.SetParent(OCConst.bfdn);
        }

        private IEnumerator OnRestart()
        {
            DOTween.KillAll();
            ClearAllObjects();
            yield return vbadConst.WaitForEndOfFrame;

            CloseApp();
            Uvsjk.Destroy(OCConst.EngineSingletonGo);
            yield return vbadConst.WaitForEndOfFrame;

            Enable();
        }

        private void CloseApp()
        {
            if (UI.Instance != null)
            {
                UI.Instance.DisposeAllUI();
            }

            ModuleBoardk.Instance.DisposeAllModule();
            Ard.Instance.DisposeAllManager();
            Ard.Instance.Dispose();
        }

        private void ClearAllObjects()
        {
            var otherGos = FindObjectsOfType<GameObject>();
            foreach (var otherGo in otherGos)
            {
                if (IsCanDestroyObj(otherGo))
                {
                    Uvsjk.Destroy(otherGo);
                }
            }
        }

        private bool IsCanDestroyObj(GameObject go)
        {
            if (go.transform.parent == null)
            {
                if (go.name is OCConst.LauncherGoName or
                    OCConst.EngineEventSystemGoName or
                    OCConst.ApplicationGoName or
                    OCConst.EngineSingletonGoName or
                    OCConst.DOTweenGoName or
                    OCConst.SuperInvokeGoName)
                {
                    return false;
                }

                return true;
            }

            return IsCanDestroyObj(go.transform.parent.gameObject);
        }





        private void OnApplicationPause(bool pause)
        {
            IsAppPause = pause;
            Debug.Log("??????????1222222222222");
            if (pause)
            {
                AppDispatcher.Instance.Dispatch(CsjInfoC.App_Pause_True, pause);
            }
            else
            {
                MainThreadDispatcher.Instance.Dispatch(GinInfoC.App_Pause_False, pause);
                AppDispatcher.Instance.Dispatch(CsjInfoC.App_Focus_False, pause);
            }
        }

        private void OnApplicationQuit()
        {
            Debug.Log("[ BingoBrain ] [ Application ] OnApplicationQuit");
            IsAppQuit = true;
            AppDispatcher.Instance.Dispatch(CsjInfoC.App_Focus_False);
            CloseApp();
            AppDispatcher.Instance.Dispatch(CsjInfoC.App_Quit);
            AssetBundle.UnloadAllAssetBundles(true);
        }

#if UNITY_ANDROID
        private void Update()
        {
            if (Application.platform == RuntimePlatform.Android && Input.GetKeyDown(KeyCode.Home))
            {
                OnClickKeyCodeHome();
            }

            if (Application.platform == RuntimePlatform.Android && Input.GetKeyDown(KeyCode.Escape))
            {
                OnClickKeyCodeEscape();
            }
        }
#endif
        protected virtual void OnClickKeyCodeHome()
        {
            AppDispatcher.Instance.Dispatch(CsjInfoC.KeyCode_Home);
            Debug.Log("[ BingoBrain ] [ Application ] OnClickAppHome");
        }

        protected virtual void OnClickKeyCodeEscape()
        {
            AppDispatcher.Instance.Dispatch(CsjInfoC.KeyCode_Escape);
            Debug.Log("[ BingoBrain ] [ Application ] OnClickAppEscape");
        }
    }
}