using System;
using Animators.Leonid_Animator;
using Animators.Leonid_Animator.Player;
using Unity.Mathematics;
using UnityEngine;

namespace CameraScripts
{
    public class CameraHandler : MonoBehaviour
    {
        public Transform targetTransform;
        private Transform _myTransform;
        [SerializeField] private Transform cameraTransform;

        public static CameraHandler Singleton;

        [SerializeField] private  float lookSpeed = 0.1f;
        [SerializeField] private  float followSpeed = 0.1f;
        [SerializeField] private float pivotSpeed = 0.03f;
        
        public float LookAngle;
        private float _pivotAngle;

        public float minimumPivot = -35;
        public float maximumPivot = 35;

        private AnimatorHandler _animatorHandler;


        private void Awake()
        {
            Singleton = this;
            _myTransform = transform;
        }

        private void Start()
        {
            _animatorHandler = gameObject.GetComponent<AnimatorHandler>();
            if (_animatorHandler == null)
                print(gameObject.name);
        }

        public void TargetPosition(float delta)
        {
            var toTargetPosition = Vector3.Lerp(_myTransform.position, targetTransform.position, delta /followSpeed);
            _myTransform.position = toTargetPosition;
        }

        public void HandleCameraRotation(float delta, float mouseX, float mouseY)
        {
            LookAngle += (mouseX * lookSpeed) / delta;
            _pivotAngle -= (mouseY * pivotSpeed) / delta;
            _pivotAngle = Mathf.Clamp(_pivotAngle, minimumPivot, maximumPivot);
            
            var rotation = Vector3.zero;
            rotation.y = LookAngle;
            transform.rotation = Quaternion.Euler(rotation);
        }
    }
}