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

public class CameraMovement : MonoBehaviour
{
	public Transform[] targets;

	// Camera offset, z-pos
	[SerializeField]
	private Vector3 offset;

	[SerializeField]
	private Vector3 middlePoint;

	[SerializeField]
	private Camera myCamera;

	// Can be used for screen shake
	private ScreenShake shake;

	void Start ()
	{
		offset = new Vector3 (0, 0, -1);

		shake = gameObject.GetComponentInChildren<ScreenShake> ();

		myCamera = gameObject.GetComponentInChildren<Camera> ();
	}

	void Update ()
	{
		Vector3 tempVec = targets [0].position - targets [1].position;

		// Check zoom
		CheckZoom (tempVec);

		// Find middlepoint
		//middlePoint = Vector3.Lerp (waiters [activeWaiters[0]].position, waiters [activeWaiters[1]].position, 0.2f) + offset;
		middlePoint = Vector3.Lerp (targets [0].position, targets [1].position, 0.5f) + offset;
		
		// Set position
		this.transform.position = Vector3.MoveTowards (this.transform.position, middlePoint, 0.5f);
	}

	// Set targets
	public void SetTargets (Transform t1, Transform t2)
	{
		targets [0] = t1;
		targets [1] = t2;
	}

	// Get targets
	public Transform[] GetTargets ()
	{
		return targets;
	}

	// Check the camera zoom and set the offset
	void CheckZoom (Vector3 zoom)
	{
		if (Mathf.Abs(zoom.x * 0.4f) <= 5)
			SetOrthographicSize (5);
		else
			SetOrthographicSize (Mathf.Abs(zoom.x * 0.4f));
	}

	void SetOrthographicSize (float size)
	{
		myCamera.orthographicSize = size;
	}
}
