using DG.Tweening;
using UnityEngine;
using UnityEngine.EventSystems;

public class CameraMovement : MonoBehaviour
{
    [Header("In game movement")]
    [SerializeField] private float moveSpeed = 1.0f;
    [SerializeField] private float zoomSpeed = 5f;
    [SerializeField] private float smoothTime = 0.2f;
    [SerializeField] private float maxZoom = 10f;
    [SerializeField] private float minZoom = 1f;

    [Header("In game shake")]
    [SerializeField] private float shakeStrenght = .1f;
    [SerializeField] private float shakeLenght = .1f;

    [Header("Start camera movement")]
    [SerializeField] private float startSpeed = 1.0f;
    [SerializeField] private float targetSize = 6.0f;
    [SerializeField] private Vector2 targetLocation = Vector2.zero;

    private float zoomVelocity = 0f;
    private float zoom = 6f;

    [Header("Camera bounds")]
    public Vector2 bottomLeftPoint;
    public Vector2 topRightPoint;
    private float startingOrthographicSize;

    private Camera mainCamera;

    public static CameraMovement Instance;
    private void Awake()
    {
        if (Instance == null) Instance = this;
        else Destroy(this);
        mainCamera = Camera.main;
        startingOrthographicSize = mainCamera.orthographicSize;
    }
    
    private void Update()
    {
        if (!GameManager.Instance.PauseGame)
        {
            float speed = (mainCamera.orthographicSize * moveSpeed) / startingOrthographicSize;
            float horizontal = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
            float vertical = Input.GetAxis("Vertical") * speed * Time.deltaTime;

            if (transform.position.x + horizontal < bottomLeftPoint.x ||
                transform.position.x + horizontal > topRightPoint.x)
                horizontal = 0;

            if (transform.position.y + vertical < bottomLeftPoint.y ||
                transform.position.y + vertical > topRightPoint.y)
                vertical = 0;

            transform.Translate(horizontal, vertical, 0);

            float scrollWheelInput = 0;

            if (!EventSystem.current.IsPointerOverGameObject())
                scrollWheelInput = Input.GetAxis("Mouse ScrollWheel");

            zoom -= scrollWheelInput * zoomSpeed;
            zoom = Mathf.Clamp(zoom, minZoom, maxZoom);
            mainCamera.orthographicSize = Mathf.SmoothDamp(mainCamera.orthographicSize, zoom, ref zoomVelocity, smoothTime);
        }
    }

    public void StartGame()
    {
        mainCamera.transform.DOMove(
            new Vector3(
                targetLocation.x,
                targetLocation.y,
                mainCamera.transform.position.z),
            startSpeed);
        mainCamera.DOOrthoSize(targetSize, startSpeed).onComplete += Events.GameStart;
        MenuManager.Instance.MenuTransition(MenuManager.Instance.inGameMenu);
    }

    public void CameraShake()
    {
        mainCamera.DOShakePosition(
                shakeLenght,
                strength: new Vector3(0, shakeStrenght, 0),
                vibrato: 1,
                randomness: 0,
                fadeOut: true,
                randomnessMode: ShakeRandomnessMode.Harmonic);
    }

}
