using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

namespace InfraredManager
{
    public class UIManagerSingle : MonoBehaviour
    {

        //连接弓箭按钮
        public Button mConnectBow;
        private Text mConnectBowText;
        //滚动的日志
        public GameObject logObj;
        public Text mScrollViewLogTextBow;
        private int bowLogTextCount = 0;

        //清空日志按钮
        public Button mClearLogBow;
        public Button mControlLogBtn;


        //进入野鸭模拟场景按钮
        public Button EnterDuckHunterBtn;
        //进入射箭模拟场景按钮
        public Button EnterGameBtn;


        //发送数据
        public Toggle SyncVector;

        #region 需要隐藏的面板节点
        public GameObject[] uiArray;
        public bool bGetPanelActive
        {
            get {
                return bPanelActive;
            }
        }
        bool bPanelActive = true;
        #endregion

        #region 需要调整的材质信息
        [Range(0.1f, 3.0f)]
        public float brightness = 1.0f; // 亮度
        public Slider brightnessSlider;
        public Text brightnessText;
        [Range(0.1f, 3.0f)]
        public float saturation = 1.0f; // 饱和度
        public Slider saturationSlider;
        public Text saturationText;
        [Range(0.1f, 3.0f)]
        public float contrast = 1.0f; // 对比度
        public Slider contrastSlider;
        public Text contrastText;
        public Material material; // 材质
        //亮度
        public void SliderBrightness()
        {
            var _value = brightnessSlider.value;
            brightness = ((int)(_value * 10)) / 10.0f;
            brightnessText.text =  brightness + "";
            material.SetFloat("_Brightness", brightness); // 设置亮度
        }
        //饱和度
        public void SliderSaturation()
        {
            var _value = saturationSlider.value;
            saturation = ((int)(_value * 10)) / 10.0f;
            saturationText.text = saturation + "";
            material.SetFloat("_Saturation", saturation); // 设置饱和度
        }
        //对比度
        public void SliderContrast()
        {
            var _value = contrastSlider.value;
            contrast = ((int)(_value * 10)) / 10.0f;
            contrastText.text = contrast + "";
            material.SetFloat("_Contrast", contrast); // 设置对比度
        }
        public void onReset() {
            brightnessSlider.value = 1.0f;
            saturationSlider.value = 1.0f;
            contrastSlider.value = 1.0f;
            SliderBrightness();
            SliderSaturation();
            SliderContrast();
        }
        #endregion

        void Awake()
        {
            if (ConnetDevicesSingle.ins)
                ConnetDevicesSingle.ins.OnLogAction += SetLogText;
            Debug.Log("SetLogText！");
        }

        // Start is called before the first frame update
        void Start()
        {

            mConnectBow.onClick.AddListener(onConnectBowDevice);
            mConnectBowText = mConnectBow.GetComponentInChildren<Text>();

            mClearLogBow.onClick.AddListener(clearLogText);
            mControlLogBtn.onClick.AddListener(onControlLog);

            EnterGameBtn.onClick.AddListener(enterGame);
            EnterDuckHunterBtn.onClick.AddListener(enterDuckHunter);


            EnterDuckHunterBtn.interactable = true;
            EnterGameBtn.interactable = true;

            mConnectBow.interactable = false;

            //默认显示并且在start 读取
            SyncVector.isOn = PlayerPrefs.GetInt("CrossHairImageActive", 1) == 1;
        }

        void OnDestroy()
        {
            if (ConnetDevicesSingle.ins)
                ConnetDevicesSingle.ins.OnLogAction -= SetLogText;
        }

        // Update is called once per frame
        void Update()
        {
            //更新ui
            if (ConnetDevicesSingle.ins)
            {
                //进入游戏
                if (ConnetDevicesSingle.ins.mBluetoothHelperBow != null)
                {


                    bool mBowConnected = ConnetDevicesSingle.ins.mBluetoothHelperBow.isConnected();
                    bool mBowIsScanning = ConnetDevicesSingle.ins.mIsBowScanning;
                    bool mBowIsConnecting = ConnetDevicesSingle.ins.mIsBowConnecting;
                    if (!mBowConnected && !mBowIsScanning && !mBowIsConnecting)
                    {
                        //未连接，未扫描，不是连接中
                        mConnectBowText.text = "点击连接弓箭";
                        if (!mConnectBow.interactable)
                            mConnectBow.interactable = true;
                    }
                    else if (!mBowConnected && mBowIsScanning)
                    {
                        //扫描中
                        mConnectBowText.text = "扫描中...";
                        if (mConnectBow.interactable)
                            mConnectBow.interactable = false;
                    }
                    else if (mBowConnected)
                    {
                        //已连接
                        mConnectBowText.text = "已连接";
                        //if (mConnectBow.interactable)
                        //    mConnectBow.interactable = false;
                        if (!mConnectBow.interactable)
                            mConnectBow.interactable = true;
                    }


                    //if (mBowConnected)
                    //{
                    //    if (!EnterDuckHunterBtn.interactable)
                    //        EnterDuckHunterBtn.interactable = true;
                    //    if (!EnterGameBtn.interactable)
                    //        EnterGameBtn.interactable = true;
                    //}
                    //else
                    //{
                    //    if (EnterDuckHunterBtn.interactable)
                    //        EnterDuckHunterBtn.interactable = false;
                    //    if (EnterGameBtn.interactable)
                    //        EnterGameBtn.interactable = false;

                    //}

                }
            }
        }


        //连接弓箭设备
        void onConnectBowDevice()
        {
            ConnetDevicesSingle.ins?.OnBowScanNearbyDevices();
        }

        public void SetLogText(string text)
        {
            //Debug.Log("SetLogText:" + text);
            if (bowLogTextCount > 200)
            {
                clearLogText();
            }
            mScrollViewLogTextBow.text += text + "\n";
            bowLogTextCount++;


        }
        public void clearLogText()
        {
            mScrollViewLogTextBow.text = "";
            bowLogTextCount = 0;
        }

        public void onControlLog() {
            logObj.SetActive(!logObj.activeSelf);
        }



        public void enterGame()
        {

            SceneManager.LoadScene("Game_InfraredCamera", LoadSceneMode.Single);
            hideManager();
        }

        public void enterDuckHunter()
        {
            SceneManager.LoadScene("DuckHunter_Test2", LoadSceneMode.Single);
            hideManager();
        }

        public void SendGetConfig()
        {
        }
        public void SendGetShoot()
        {
            ConnetDevicesSingle.ins?.ButtonShoot();
        }


        public void onHideCrosshair()
        {
            ConnetDevicesSingle.ins?.onHideCrosshair();
            SyncVector.isOn = false;
        }
        public void onShowCrosshair()
        {
            ConnetDevicesSingle.ins?.onShowCrosshair();
            SyncVector.isOn = true;
        }

        public void onToggleSyncVector() {

            if (SyncVector.isOn)
            {
                ConnetDevicesSingle.ins?.onShowCrosshair();
            }
            else {
                ConnetDevicesSingle.ins?.onHideCrosshair();
            }
        }

        //隐藏控制面板UI
        public void hideManager()
        {
            // transform.parent.GetComponent<Canvas>().gameObject.SetActive(false);
            bPanelActive = false;
            for (int i = 0; i < uiArray.Length; i++)
            {
                uiArray[i].SetActive(false);
            }

        }
        //显示控制面板UI
        public void showManager()
        {
            //  transform.parent.GetComponent<Canvas>().gameObject.SetActive(true);
            bPanelActive = true;
            for (int i = 0; i < uiArray.Length; i++)
            {
                uiArray[i].SetActive(true);
            }
        }

    }
}