using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
namespace HyperspaceGame
{
    public class UIManager : MonoBehaviour
    {
        public static UIManager _ins;
        /// 
        /// 记录一个总分
        /// 
        public static int mHyperspaceScore { get; set; } = 0;
        [SerializeField] Button _btnQuit;
        [SerializeField] Button _btnReload;
        [SerializeField] Button _btnCrosshair;
        [SerializeField] Button _btnCalibrationOffset;
        [SerializeField] Material outlight;
        //准心部分
        private bool open = false;
        private bool visiable = false;
        private bool onlyShow = true;
        private Image image = null;
        private void Awake()
        {
            if (_ins != null && _ins != this)
            {
                //Destroy(this.gameObject); // 如果已经存在实例,则销毁当前对象
                return;
            }
            _ins = this;
            //DontDestroyOnLoad(this.gameObject); // 保证对象在场景切换时不被销毁
            //生成时候初始化一下状态
            if (ShootCheck.ins) ShootCheck.ins.bluetoothDeviceStatus = SmartBowSDK.BluetoothDeviceStatus.MagazineLoading;
        }
        private void OnDestroy()
        {
            if (_ins == this)
            {
                _ins = null; // 在销毁时释放单例
            }
            GlobalEventCenter.ins.onSimulateMouseAwakeChanged -= UpdateHideShow;
        }
        // Start is called before the first frame update
        void Start()
        {
            //关闭原来准心
            SimulateMouseController.ins?.RemoveOpenLocker("NotGame");
            image = GeneratingTarget.gm.shootingEvent.GetComponent();
            //b端单人
            if (CommonConfig.StandaloneModeOrPlatformB) 
            {
                _btnCrosshair.gameObject.SetActive(false);
                _btnCalibrationOffset.gameObject.SetActive(false);
            } else {
                //获取设置值和一个存储值
                bool onInitOpen = InfraredDemo._ins ? InfraredDemo._ins.bInitCrosshairShow() : true;
                Image crossHairImage = _btnCrosshair.GetComponentInChildren();
                crossHairImage.material = onInitOpen ? outlight : null;
                _btnCrosshair.onClick.AddListener(delegate () {
                    AudioMgr.ins.PlayBtn();
                    bool onlyShow = !GetOnlyShow();
                    SetOnlyShow(onlyShow);
                    //记录准心值
                    if (InfraredDemo._ins) InfraredDemo._ins.setCrosshairValue(onlyShow);
                    if (onlyShow)
                    {
                        crossHairImage.material = outlight;
                    }
                    else
                    {
                        crossHairImage.material = null;
                    }
                });
                //校准
                _btnCalibrationOffset.onClick.AddListener(delegate () {
                    AudioMgr.ins.PlayBtn();
                    AutoResetView.DoIdentity();
                });
            }
            _btnQuit.onClick.AddListener(OnShutDown);
            _btnReload.onClick.AddListener(Reload);
            SetOpen(UserSettings.ins.openCrossHair);
            visiable = open;
            OnValueChanged(visiable);
            UpdateHideShow(SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked);
            GlobalEventCenter.ins.onSimulateMouseAwakeChanged += UpdateHideShow;
            //CrossHairOutBoundChecker1 outBoundChecker = Instantiate(Resources.Load("Prefabs/CrossHairOutBoundChecker1")).GetComponent();
            //outBoundChecker.crossHair = transform as RectTransform;
            //读取准心值
            if (AimHandler.ins && !AimHandler.ins.bRuning9Axis() && InfraredDemo._ins) SetOnlyShow(InfraredDemo._ins.getCrosshairValue() == 1);
        }
        void UpdateHideShow(bool mouseAwaked)
        {
            //transform.Find("Image").GetComponent().enabled = !mouseAwaked;
            SetOnlyShow(!mouseAwaked);
        }
        public void ShowQuit()
        {
            _btnQuit.gameObject.SetActive(true);
        }
        void OnShutDown()
        {
            //SceneManager.LoadScene("Home",LoadSceneMode.Single);
            GeneratingTarget.gm.onUploadScore();
            //结束游戏页面
            GeneratingTarget.gm.userGameAnalyse1.showResultView(() =>
            {
                SceneManager.LoadScene("Home", LoadSceneMode.Single);
            });
        }
        public void Reload()
        {
            GeneratingTarget.gm.OnSeparation();
            GeneratingTarget.gm.OnLoading();
        }
        public bool GetArrowBtnState()
        {
            if (open)
                return true;
            return false;
        }
        // Update is called once per frame
        void Update()
        {
            if (AutoResetView.ins != null)
            {
                //进入校准时候,一定显示准心
                SetVisiable(true);
            }
            else
            {
                if (open) SetVisiable(onlyShow);
            }
        }
        void SetVisiable(bool value)
        {
            if (this.visiable == value) return;
            this.visiable = value;
            OnValueChanged(this.visiable);
        }
        void OnValueChanged(bool isOn)
        {
            var color = image.color;
            if (isOn)
            {
                color.a = 1;
                image.color = color;
                //_togImge.sprite = _togSprites[0];
        
            }
            else
            {
                color.a = 0;
                image.color = color;
                //_togImge.sprite = _togSprites[1];
            }
        }
        public void SetOpen(bool open)
        {
            this.open = open;
            if (!this.open)
            {
                SetVisiable(false);
            }
        }
        public bool GetOpen()
        {
            return this.open;
        }
        public void SetOnlyShow(bool onlyShow)
        {
            this.onlyShow = onlyShow;
        }
        public bool GetOnlyShow()
        {
            return this.onlyShow;
        }
    }
}