using UnityEngine;
using System.Collections;

public class ChangeRoomController : MonoBehaviour {

	[SerializeField]
	private string DoorDirection = null;
	[SerializeField]
	private GameObject PlayerObject = null;
	[SerializeField]
	private GameObject NextRoomSpawn = null;
	private CharacterMovementController MovementController = null;
	private Vector3 CameraPosition = Vector3.zero;
	private bool MovedCamera = false;
	private float LerpSpeed = 0.2f;
	private Vector3 TargetPos = Vector3.zero;
	// Use this for initialization
	void Start () 
	{
	}

	void OnTriggerEnter2D(Collider2D other)
	{		
		if ((other.tag == "Player") && (other.isTrigger == false)) {
						CameraPosition = Camera.main.transform.position; //Update the camera position for a lerp
						PlayerObject = other.gameObject;
						MovementController = PlayerObject.GetComponent<CharacterMovementController> ();
						MovementController.resetDirections();
						PlayerObject.GetComponent<DeathSpawnController>().UpdateSpawnPoint(NextRoomSpawn);
				}
	}

	// Update is called once per frame
	void OnTriggerStay2D (Collider2D other) 
	{
		if ((other.tag != "Player") || (other.isTrigger)) return; //So only the player can leave the game
		if (PlayerObject == null) 
						throw new System.Exception ("No player attached to door");
		MovementController.enabled = false;
		switch (DoorDirection) {
			case "Right":
				if (MovedCamera == false)
				{
					TargetPos = CameraPosition + new Vector3 (15.0f, 0.0f, 0.0f);
					Debug.Log ("Door entered going right");
					Camera.main.transform.position = Vector3.Lerp (Camera.main.transform.position, TargetPos, LerpSpeed);
					if (Camera.main.transform.position.x - TargetPos.x > -0.1f) { //Solve lerping moving issues
						MovedCamera = true;
						CameraPosition = Camera.main.transform.position = TargetPos;//Update the camera position for a lerp
					TargetPos = PlayerObject.transform.position + new Vector3(2.5f,0.0f,0.0f);
					}
				}
				else
				{
				PlayerObject.transform.position = Vector3.Lerp (PlayerObject.transform.position, TargetPos, LerpSpeed / 4);
				if (PlayerObject.transform.position.x - TargetPos.x > -0.1f)
					{
					PlayerObject.transform.position = TargetPos;
						MovementController.enabled = false;
						this.enabled = false;
					}
				}
				break;
			case "Left":
				if (MovedCamera == false)
				{
					TargetPos = CameraPosition + new Vector3 (-15.0f, 0.0f, 0.0f);
					Debug.Log ("Door entered going left");
					Camera.main.transform.position = Vector3.Lerp (Camera.main.transform.position, TargetPos, LerpSpeed);
					if (Camera.main.transform.position.x - TargetPos.x < 0.1f) { //Solve lerping moving issues
						MovedCamera = true;
						CameraPosition = Camera.main.transform.position = TargetPos;//Update the camera position for a lerp
					TargetPos = PlayerObject.transform.position + new Vector3(-2.5f,0.0f,0.0f);
					}
				}
				else
				{
				PlayerObject.transform.position = Vector3.Lerp (PlayerObject.transform.position, TargetPos, LerpSpeed / 4);
				if (PlayerObject.transform.position.x - TargetPos.x < 0.1f)
					{
					PlayerObject.transform.position = TargetPos;
						MovementController.enabled = false;
						this.enabled = false;
					}
				}
			break;
			case "Up":
				if (MovedCamera == false)
				{
					TargetPos = CameraPosition + new Vector3 (0.0f, 9.0f, 0.0f);
					Debug.Log ("Door entered going up");
					Camera.main.transform.position = Vector3.Lerp (Camera.main.transform.position, TargetPos, LerpSpeed);
					if (Camera.main.transform.position.y - TargetPos.y > -0.1f) { //Solve lerping moving issues
						MovedCamera = true;
						CameraPosition = Camera.main.transform.position = TargetPos;//Update the camera position for a lerp
					TargetPos = PlayerObject.transform.position + new Vector3(0.0f,2.5f,0.0f);
					}
				}
				else
				{
				PlayerObject.transform.position = Vector3.Lerp (PlayerObject.transform.position, TargetPos, LerpSpeed / 4);
				if (PlayerObject.transform.position.y - TargetPos.y > -0.1f)
					{
					PlayerObject.transform.position = TargetPos;
						MovementController.enabled = false;
						this.enabled = false;
					}
				}
					break;
			case "Down":
				if (MovedCamera == false)
				{
					TargetPos = CameraPosition + new Vector3 (0.0f, -9.0f, 0.0f);
					Debug.Log ("Door entered going down");
					Camera.main.transform.position = Vector3.Lerp (Camera.main.transform.position, TargetPos, LerpSpeed);
					if (Camera.main.transform.position.y - TargetPos.y < 0.1f) { //Solve lerping moving issues
						MovedCamera = true;
						CameraPosition = Camera.main.transform.position = TargetPos;//Update the camera position for a lerp
					TargetPos = PlayerObject.transform.position + new Vector3(0.0f, -2.5f,0.0f);
					}
				}
				else
				{
				PlayerObject.transform.position = Vector3.Lerp (PlayerObject.transform.position, TargetPos, LerpSpeed / 4);
				if (PlayerObject.transform.position.y - TargetPos.y < 0.1f)
					{
					PlayerObject.transform.position = TargetPos;
						MovementController.enabled = false;
						this.enabled = false;
					}
				}
					break;
			default:
					Debug.LogWarning ("Door entered with no direction");
					break;
			}
		///has cam moves
		///is player back on screen
	}

	void OnTriggerExit2D(Collider2D other)
	{
		if (MovementController)
			MovementController.enabled = true;
		this.enabled = false;
	}
}
