using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class CameraMovement : MonoBehaviour {
    public float mouseSensitivity = 1.0f;
    private Vector3 lastPosition;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void LateUpdate () {
		if(Input.GetMouseButtonDown(1))
        {
            lastPosition = Input.mousePosition;
        }

        // pan with right mouse button
        if (Input.GetMouseButton(1) && rayCastEventSystemFromMouse().Count == 0)
        {
            Vector3 delta = mouseSensitivity * (Input.mousePosition - lastPosition);
            Vector3 cameraPosition = Camera.main.transform.position;
            Vector3 newPosition = new Vector3(cameraPosition.x + delta.x, cameraPosition.y + delta.y, cameraPosition.z);
            Vector3 newInterpolatedPosition = Vector3.Lerp(cameraPosition, newPosition, Time.deltaTime);

            Camera.main.transform.position = newInterpolatedPosition;

            lastPosition = Input.mousePosition;
        }

        if (Input.GetKey(KeyCode.D))
        {
            Debug.Log(Camera.main.ScreenToWorldPoint(Input.mousePosition));
        }
    }

    private List<RaycastResult> rayCastEventSystemFromMouse()
    {
        PointerEventData ped = new PointerEventData(EventSystem.current);
        List<RaycastResult> raycastResults = new List<RaycastResult>();
        ped.position = Input.mousePosition;

        EventSystem.current.RaycastAll(ped, raycastResults);
        return raycastResults;
    }
}
