using System.Collections;
using System.Collections.Generic;
using System.Linq;
using DG.Tweening;
using MyFrameworkPure;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
using UnityEngine.UI;


namespace CommonScript
{
    public class KXScanOperator : MonoBehaviour
    {
        //public UnityAction onResetScanTarget;


        //The speed of the "attach" process


        //Controller in use


        //The focus of the object's "attach" movement


        //[SerializeField]
        //private GameObject[] scanTargets;

        //[SerializeField]
        //private ToggleGroup toggleGroup;

        //[SerializeField]
        //private Button resetBtn;

        [SerializeField]
        private Bounds areaBounds;

        private bool stayInScanPoint;

        private Camera playercamera;
        
        private GameObject curScanTarget;

        private GestureTool gestureTool;
        public float m_s = 1f;
        public float m_r = 1f;
        private const float MoveSpeed = 0.01f;

        private const float RotSpeed = 1f;
        // Start is called before the first frame update
        void Start()
        {

            curScanTarget = gameObject;
            gestureTool = GestureTool.Instance;
            gestureTool.onHorizontalMove.AddListener(HandleHorizontalGesture);
            gestureTool.onVerticalMove.AddListener(HandleVerticalGesture);
        }

        void HandleHorizontalGesture(float delta)
        {
           
          
                    curScanTarget.transform.Rotate(Vector3.down * delta * RotSpeed*m_r, Space.World);
              
        }

        void HandleVerticalGesture(float delta)
        {
           
        }

        void Update()
        {
            //RaycastHit hitInfo = new RaycastHit();



           // Ray ray = playercamera.ScreenPointToRay(Input.mousePosition);
        //    bool isClickNewTarget = Input.GetMouseButtonDown(0) && Physics.Raycast(ray, out hitInfo)
                                                                

            //if (isClickNewTarget)
            //{
            //    if (curScanTarget == hitInfo.transform.gameObject)
            //        return;
            //    if (curScanTarget)
            //        ResetCurScanTarget();

            //    curScanTarget = hitInfo.transform.gameObject;
            //    GameObject targetPoint = GameObject.Find($"初始位置/观察/{curScanTarget.name}");
            //    if (!targetPoint)
            //              {
            //        targetPoint = GameObject.Find("初始位置/观察/默认");
            //    }
            //    Debug.Assert(targetPoint != null, "场景中不存在默认观察点!");
            //    DOTween.Kill(curScanTarget.transform);
            //    curScanTarget.transform.DOMove(targetPoint.transform.position, 1);
            //}

            //ClampScanTargetPos();

           // OperatorToggleSwitcher.Switch(toggleGroup);
        }

        void OnDisable()
        {
            if(curScanTarget)
                DOTween.Kill(curScanTarget);
            curScanTarget = null;
        }

        void OnDestroy()
        {
            if (gestureTool)
            {
                gestureTool.onHorizontalMove.RemoveListener(HandleHorizontalGesture);
                gestureTool.onVerticalMove.RemoveListener(HandleVerticalGesture);
            }
        }

        //public void ResetCurScanTarget()
        //{
        //    DOTween.Kill(curScanTarget.transform);
        //    Transform point = GameObject.Find($"初始位置/材料/{curScanTarget.name}")?.transform;
        //    Debug.Assert(point != null, $"没有找到路径:初始位置/材料/{curScanTarget.name}");
        //    curScanTarget.transform.DOMove(point.position, 1);
        //    curScanTarget.transform.DORotate(point.eulerAngles, 1);
        //    curScanTarget = null;
        //}

        //void ClampScanTargetPos()
        ////{
        ////    if(!curScanTarget)
        ////        return;
        ////    if(areaBounds.extents == Vector3.zero)
        ////        return;
        ////    if (!areaBounds.Contains(curScanTarget.transform.position))
        ////    {
        ////        curScanTarget.transform.position = areaBounds.ClosestPoint(curScanTarget.transform.position);
        ////    }
        //}

        void OnDrawGizmosSelected()
        {
            Gizmos.color = Color.yellow;
            Gizmos.DrawWireCube(areaBounds.center,areaBounds.size);
        }
    }
}