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

public class CameraScroll : MonoBehaviour {

	float minZoom = 4;
	float maxZoom = 15;

	Vector3 dragStart;
	bool isDragging = false;

	float keyScrollSpeed = 15f;

	void Start() {
	}

	public void ZoomToDemon() {
		DemonLord dl = GameObject.FindObjectOfType<DemonLord>();
		if(dl == null)
			return;

		Vector3 pos = dl.transform.position;
		pos.z = transform.position.z;
		transform.position = pos;
	}

	void Update () {
		if(Input.GetKey(KeyCode.Space)) {
			ZoomToDemon();
		}

		if(EventSystem.current.IsPointerOverGameObject() != true && 
		   Game.current.mouseMode == Game.MouseMode.POINTER && 
		   (Input.GetMouseButton(0) || Input.GetMouseButton(1) || Input.GetMouseButton(2))) {
			if(isDragging == false) {
				isDragging = true;
				dragStart = Camera.main.ScreenToWorldPoint( Input.mousePosition );
			}
			else {
				Vector3 mousePos = Camera.main.ScreenToWorldPoint( Input.mousePosition );
			
				this.transform.Translate( dragStart - mousePos );

			}
		}
		else {
			isDragging = false;
		}

		if(isDragging == false) {
			this.transform.Translate( new Vector3( Input.GetAxis("Horizontal")*keyScrollSpeed*Time.deltaTime, Input.GetAxis("Vertical")*keyScrollSpeed*Time.deltaTime, 0 ) );
		}

		if(EventSystem.current.IsPointerOverGameObject() != true) {
			Camera.main.orthographicSize =  Mathf.Clamp(Camera.main.orthographicSize - Input.mouseScrollDelta.y, minZoom, maxZoom);
		}

	}
}
