using System;
using UnityEngine;

namespace Project.Script.Tools
{
    public class DebugTools : MonoBehaviour
    {
        [SerializeField] private GameObject inspectorGameObject;
        private                  bool       _isShowDebugToolButton = false;

        private bool IsShowDebugToolButton
        {
            get { return _isShowDebugToolButton; }
            set
            {
                if (_isShowDebugToolButton == value)
                    return;
                _isShowDebugToolButton = value;
                if (!_isShowDebugToolButton)
                {
                    inspectorGameObject.SetActive(false);
                }
            }
        }

        private                  float _timer;
        private                  float _clickCount;
        [SerializeField] private float _time          = 0.8f;
        [SerializeField] private uint  _clickCountSum = 5;
        private                  bool  _isStartClick  = false;

        private void Awake()
        {
#if UNITY_EDITOR || DEVELOPMENT_BUILD
            DontDestroyOnLoad(gameObject);
            inspectorGameObject.SetActive(false);
#else
            Destroy(gameObject);
#endif
        }

        private void Update()
        {
            if (_isStartClick)
            {
                _timer += Time.deltaTime;
                if (_timer > _time)
                {
                    _timer        = 0;
                    _clickCount   = 0;
                    _isStartClick = false;
                }
            }

            // 3秒内连续点击屏幕5次设置 _isShowDebugToolButton = true 或者 false
            if (!Input.GetMouseButtonDown(0)) return;
            if (!_isStartClick)
                _isStartClick = true;

            if (_timer < _time)
            {
                _clickCount++;
                if (!(_clickCount >= _clickCountSum)) return;
                IsShowDebugToolButton = !IsShowDebugToolButton;
                _clickCount           = 0;
            }
            else
            {
                _timer      = 0;
                _clickCount = 0;
            }
        }

        private void OnGUI()
        {
            if (!IsShowDebugToolButton)
                return;
            // 屏幕顶部居中按钮用来显示或隐藏Inspector
            if (GUI.Button(new Rect(Screen.width / 2f - 50, 0, 150, 50),
                           "对象监视面板" + (inspectorGameObject.activeSelf ? "(关闭)" : "(打开)")))
            {
                inspectorGameObject.SetActive(!inspectorGameObject.activeSelf);
            }
        }
    }
}