using UnityEngine;


namespace GameEngine
{
	public class CameraFollowBehaviour : MonoBehaviour
	{
		[SerializeField] private Transform m_Target = default(Transform);
		[SerializeField] private Vector3 m_Offset = default(Vector3);
		[SerializeField] private float m_Height = default(float);
		[SerializeField, Range(0f, 1f)] private float m_HeightWeight = default(float);
		[SerializeField] private float m_SmoothTime = 1f;
		[SerializeField] private float m_DepthSmoothTime = 1f;
		[SerializeField] private float m_TargetDistance = -20f;
		private Vector2 _velocity;
		private float _zVelocity;




		private void OnEnable()
		{
			Vector2 position = GetTargetPosition();
			float distance = m_TargetDistance;

			Vector3 newPos = position;
			newPos.z = distance;

			transform.position = newPos;

			_velocity = Vector3.zero;
		}


		private void FixedUpdate()
		{
			if (m_Target == null)
			{
				return;
			}

			Vector2 position = transform.position;
			float distance = transform.position.z;

			position = Vector2.SmoothDamp(position, GetTargetPosition(), ref _velocity, m_SmoothTime, float.MaxValue,
			   Time.deltaTime);
			distance = Mathf.SmoothDamp(distance, m_TargetDistance, ref _zVelocity, m_SmoothTime, float.MaxValue, Time.deltaTime);

			Vector3 newPos = position;
			newPos.z = distance;

			transform.position = newPos;
		}




		private Vector3 GetTargetPosition()
		{
			if (m_Target == null)
			{
				return transform.position;
			}

			Vector3 targetPosition = m_Target.position;
			targetPosition.y = Mathf.Lerp(targetPosition.y, m_Height, m_HeightWeight);
			targetPosition += m_Offset;

			return targetPosition;
		}
	}
}
