using System.Collections;
using System.Collections.Generic;
using Assets.Scripts.Constants;
using Assets.Scripts.Managers;
using UnityEngine;

public class RiftController : MonoBehaviour {

	#region variables
	private static bool _canRift = true;

	[SerializeField]
	private GameObject _riftPrefab;

	private GameObject _riftOnWorld;
	#endregion

	
	#region events
	void Update () {

		if (_canRift && PlayerMovement.ControlsEnabled) {

			if (Input.GetMouseButtonDown (1)) {
				Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
				RaycastHit hit;

				if (Physics.Raycast (ray, out hit, 100f, LayersManager.GetLayerMask (new string [] {Layers.FrontAngle, Layers.BackAngle}))) {

					if (hit.transform.name == "Rift") {
						Destroy (hit.transform.gameObject);
						_riftOnWorld = null;
						return;
					}
					
					Quaternion rot = hit.transform.rotation;

					if (_riftOnWorld == null) {

						_riftOnWorld = Instantiate <GameObject> (_riftPrefab, hit.point, rot);
						
					} else {

						_riftOnWorld.transform.position = hit.point;
						_riftOnWorld.transform.rotation = rot;
						
					}

					_riftOnWorld.transform.Translate (Vector3.zero);
					
				}
			}
			
		}
		
	}
	#endregion

}
