using UnityEngine;
using UnityEngine.EventSystems;

public class PlayerController : MonoBehaviour
{
    private Vector3 _lastMousePosition;
    private Camera _camera;
    private bool _canMove;

    private void Start() {
        _camera = Camera.main;
    }

    private void Update() {
        CheckMovementInput();
    }

    private void CheckMovementInput() {
        if (Input.GetMouseButtonDown(0))
        {
            _lastMousePosition = Input.mousePosition;
            _canMove = !IsMouseOverUI();
        }
        
        if (!Input.GetMouseButton(0) || !_canMove) return;

        Vector3 mouseWorldPoint = _camera.ScreenToWorldPoint(Input.mousePosition);
        Vector3 lastWorldPoint = _camera.ScreenToWorldPoint(_lastMousePosition);

        Vector3 delta = mouseWorldPoint - lastWorldPoint;

        _lastMousePosition = Input.mousePosition;

        _camera.transform.position -= delta;
    }

    private bool IsMouseOverUI() {
        return EventSystem.current.IsPointerOverGameObject();
    }
}

