using System;
using Framework.Scripts.Runtime.Engine.Engine.Camera;
using Framework.Scripts.Runtime.Engine.Engine.Camera.CameraControl;
using MotionFramework;
using MotionFramework.Event;
using NaughtyAttributes;
using UnityEngine;

namespace Framework.Scripts.Runtime.Module.Module.Camera
{
    public class CameraManager :  MonoBehaviour
    {
        private CameraRaycastComponent _raycastComponent;
        private CameraControlComponent _cameraControlComponent;


        [SerializeField] [Label("摄像头控制")] public bool showCamControl;
        [SerializeField] [Label("射线")] public bool showCamRay;

        [ShowIf("showCamControl")] [BoxGroup("参数")] [SerializeField] [Label("鼠标中间移动速度")]
        private float middleSpeed = 2;

        [ShowIf("showCamControl")] [MinMaxSlider(-90, 90)] [BoxGroup("参数")] [SerializeField] [Label("角度最大限制")]
        private Vector2 minMaxSlider;

        [ShowIf("showCamControl")] [BoxGroup("参数")] [SerializeField] [Label("滚轮灵敏度设置")]
        private int MouseWheelSensitivity = 1; //滚轮灵敏度设置

        [ShowIf("showCamControl")] [BoxGroup("参数")] [SerializeField] [Label("相机距离最小值")]
        private int MouseZoomMin = 10; //相机距离最小值

        [ShowIf("showCamControl")] [BoxGroup("参数")] [SerializeField] [Label("相机距离最大值")]
        private int MouseZoomMax = 9999; //相机距离最大值

        [ShowIf("showCamControl")] [BoxGroup("参数")] [SerializeField] [Label("旋转视角时相机x轴转速")]
        private float xSpeed = 250.0f; //旋转视角时相机x轴转速

        [ShowIf("showCamControl")] [BoxGroup("参数")] [SerializeField] [Label("旋转视角时相机y轴转速")]
        private float ySpeed = 120.0f; //旋转视角时相机y轴转速

        [ShowIf("showCamControl")] [BoxGroup("参数")] [SerializeField] [Label("相机和target之间的距离")]
        private float Distance = 20; //相机和target之间的距离，因为相机的Z轴总是指向target，也就是相机z轴方向上的距离

        [SerializeField]  private Transform MainCam;
        [SerializeField]  private Transform MainCamTarget;

        void Awake()
        {
            // 为组件赋值，可以通过查找对象或者直接赋值的方式
            _raycastComponent = new CameraRaycastComponent();
            _cameraControlComponent = new CameraControlComponent();

            if (_raycastComponent == null || _cameraControlComponent == null)
            {
                Debug.LogError("缺少必要的组件");
            }
        }

        private void OnEnable()
        {
            _raycastComponent.OnRaycastHit += HandleRaycastHit;
        }

        private void Start()
        {
            _cameraControlComponent.Init(MainCam, MainCamTarget);
            _raycastComponent.Init();
        }

        private void OnDisable()
        {
            _raycastComponent.OnRaycastHit -= HandleRaycastHit;
        }

        // 处理射线命中事件并打印物体名称
        void HandleRaycastHit(RaycastHit hit)
        {
            Debug.Log("射线检测到物体--->" + hit.collider.name);
        }
        void Update()
        {
            // 调用射线功能
            if (showCamRay)
            {
                if (Input.GetMouseButtonDown(0)) // 比如当按下鼠标左键时
                {
                    _raycastComponent?.PerformRaycast();
                }
            }

           

            _cameraControlComponent.UpdateValue(minMaxSlider);
            // 调用镜头控制功能
            _cameraControlComponent?.ControlCamera();
        }
    }
}