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

public class TouchInput : MonoBehaviour
{
	private Vector3 inputPosition;

	[SerializeField]
	private Camera inputCamera;

	private float inputDepth;

	private void Start()
	{
		Initialize();
	}

	private void Initialize()
	{
		if(inputCamera == null)
		{
			inputCamera = Camera.main;
		}
	}

	private void Update()
	{
		GetTouchInput();
	}

	private void GetTouchInput()
	{
		if(Input.GetMouseButtonDown(0))
		{
			OnInputDown();
		}

		if(Input.GetMouseButton(0))
		{
			OnInputHold();
		}

		if(Input.GetMouseButtonUp(0))
		{
			OnInputUp();
		}
	}

	private void UpdateInputPosition()
	{
		inputPosition = inputCamera.ScreenToWorldPoint(
			new Vector3(Input.mousePosition.x, Input.mousePosition.y, GetInputDepth()), 
			Camera.MonoOrStereoscopicEye.Mono);

		Debug.DrawRay(inputPosition, Vector3.up, Color.green, 0.1f);
		Debug.DrawRay(inputPosition, Vector3.right, Color.red, 0.1f);
	}

	private float GetInputDepth()
	{
		return -inputCamera.transform.position.z + inputDepth;
	}

	private void OnInputDown()
	{
		UpdateInputPosition();
		EventManager.TriggerEvent(EventNames.TOUCHINPUT_DOWN, inputPosition);
	}

	private void OnInputHold()
	{

	}

	private void OnInputUp()
	{
		EventManager.TriggerEvent(EventNames.TOUCHINPUT_UP);
	}
}
