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

[System.Serializable]
public class TagsClass{
	public string ItemTag = "Item";
	public string SocketTag = "Socket";
	public string InteractTag = "Interact";
}

public class InteractionScript : MonoBehaviour {

	public GameObject playerCam;
	public TagsClass Tags = new TagsClass();

	public float interactRange = 3f;
	public float holdDistance = 3f;
	private float holdDistanceStart;
	public float minDistance = 1f;
	public float maxDistance = 4f;
	public float throwStrength = 50f;

	private Ray playerAim;
	private RaycastHit hit;

	[HideInInspector] public GameObject objectHeld = null;
	private bool isObjectHeld = false;

	public float rotateSpeed = 20.0f;
	public float zoomSpeed = 1.0f;

	public GameObject instructions;

	/*public float transitionTime = 1.0f;
	[HideInInspector] public bool inInteraction = false;
	private Vector3 camStartPos;
	private Quaternion camStartRot;
	private float exitTimer = 0.0f;
	private bool exitInitiated = false;*/

	//------------------------------------------------------------------------------------------------------------------------

	void Start()
	{
		holdDistanceStart = holdDistance;
		instructions.SetActive (false);
	}


	void Update()
	{
		playerAim = playerCam.GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
		int layerMask = ~(1 << 8);

		if(!isObjectHeld &&/* !inInteraction &&*/ Input.GetMouseButtonDown(0) && Physics.Raycast(playerAim, out hit, interactRange, layerMask)){
			tryInteraction (hit.collider.gameObject);
		}

		//Item-Funktionen
		if (isObjectHeld){
			
			Vector3 nextPos = playerCam.transform.position + playerAim.direction * holdDistance;
			Vector3 currPos = objectHeld.transform.position;
			objectHeld.GetComponent<Rigidbody> ().velocity = (nextPos - currPos) * 5;

			if (Vector3.Distance(objectHeld.transform.position, playerCam.transform.position) > maxDistance){
				dropObject();
			}

			if (Input.GetMouseButtonUp (0) && !Input.GetMouseButton (1)) {
				dropObject ();
			}

			if (Input.GetMouseButton (1)) {
				objectHeld.GetComponent<Rigidbody> ().constraints = RigidbodyConstraints.None;
				rotateObject ();
			}

			if (Input.GetMouseButtonUp (1)) {
				objectHeld.GetComponent<Rigidbody> ().constraints = RigidbodyConstraints.FreezeRotation;
			}

			if (Mathf.Abs(Input.GetAxis("Mouse ScrollWheel")) > 0) {
				holdDistance += Input.GetAxis ("Mouse ScrollWheel") * zoomSpeed;
				if (holdDistance >= maxDistance) {
					holdDistance = maxDistance;
				}
				if (holdDistance <= minDistance) {
					holdDistance = minDistance;
				}
			}

		}

		//Interact-Funktionen
		/*if (inInteraction) {
			if(Input.GetMouseButtonDown(1)){
				exitInteraction();
			}
		}
		if (exitInitiated) {
			exitTimer += Time.deltaTime;
			if (exitTimer >= transitionTime) {
				inInteraction = false;
				exitInitiated = false;
			}
		}*/

	}

	//------------------------------------------------------------------------------------------------------------------------

	//Checken, ob Interaktion erlaubt ist
	private void tryInteraction(GameObject interaction)
	{

		if(interaction.tag == Tags.ItemTag || interaction.tag == Tags.SocketTag){
			holdObject (interaction);
		}
		/*if (interaction.tag == Tags.InteractTag) {
			enterInteraction (interaction);
		}*/
		//do nothing
	}

	//------------------------------------------------------------------------------------------------------------------------

	//Item-Funktionen
	public void holdObject(GameObject interaction)
	{
		objectHeld = interaction;
		isObjectHeld = true;
		objectHeld.GetComponent<Rigidbody> ().useGravity = false;
		Debug.Log ("Holding " + interaction.name);
		objectHeld.GetComponent<Rigidbody> ().constraints = RigidbodyConstraints.None;
		objectHeld.GetComponent<Rigidbody> ().constraints = RigidbodyConstraints.FreezeRotation;

		instructions.SetActive (true);
	}

	public void dropObject()
	{
		objectHeld.GetComponent<Rigidbody>().useGravity = true;
		objectHeld.GetComponent<Rigidbody> ().constraints = RigidbodyConstraints.None;
		objectHeld = null;
		isObjectHeld = false;
		holdDistance = holdDistanceStart;

		instructions.SetActive (false);
	}

	private void rotateObject()
	{
		float v = Input.GetAxis ("Mouse X");
		float h = Input.GetAxis ("Mouse Y");
		objectHeld.transform.Rotate (new Vector3 (h, v, 0) * Time.deltaTime * rotateSpeed, Space.World);
	}

	//------------------------------------------------------------------------------------------------------------------------

	/*//Interact-Funktionen
	private void enterInteraction(GameObject interaction)
	{
		foreach (Transform child in interaction.transform) {
			if (child.CompareTag("CamPos")){
				camStartPos = Camera.main.transform.position;
				camStartRot = Camera.main.transform.rotation;
				StartCoroutine (Transition (child.transform.position, child.transform.rotation));
				inInteraction = true;
			}
		}
	}

	private void exitInteraction()
	{
		StartCoroutine (Transition(camStartPos, camStartRot));
		exitInitiated = true;
		exitTimer = 0.0f;
	}

	//------------------------------------------------------------------------------------------------------------------------
		
	IEnumerator Transition(Vector3 zielPosition, Quaternion zielRotation){

		float t = 0.0f;
		Vector3 startingPos = objectHeld.transform.position;
		Quaternion startingRot = objectHeld.transform.rotation;

		while (t < 1.0f) {

			t += Time.deltaTime * (Time.timeScale / transitionTime);

			Camera.main.transform.position = Vector3.Lerp (startingPos, zielPosition, t);
			Camera.main.transform.rotation = Quaternion.Lerp (startingRot, zielRotation, t);
			yield return 0;
		}

		if (transform.position == zielPosition) {
			yield return null;
		}
	}*/
}
