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

public class TapMechanix : MonoBehaviour {

	public GameObject Wave;
	public GameObject Wind;

	public RectTransform CoolDownElement1;
	public RectTransform CoolDownElement2;

	public GameObject Menu;
	public GameObject HUD;

	public GameObject Results;

	private enum Directions {
		East,
		West,
		North,
		South
	}

	private float _timer;
	private float _coolDown;

	private float _force;
	private float _awakens;

	private Rigidbody _actorBody;
	private Transform _actorTransform;

	private float _chronometer;

	private const float _COOLDOWN_LIMIT_ = 1.9f;
	private const float _COOLDOWN_THRESHOLD = 0.9f;

	public void Start () {
		_chronometer = 501;
		_timer = 0f;
		_coolDown = 0f;
		_force = 1f;
		_awakens = 0.5f;
		_actorBody = GameObject.FindGameObjectWithTag ("Player").GetComponent<Rigidbody> ();
		_actorTransform = GameObject.FindGameObjectWithTag ("Player").transform;
		CoolDownElement1.sizeDelta = new Vector3 (125f, 20f);
		CoolDownElement2.sizeDelta = new Vector3 (125f, 20f);
		CoolDownElement1.GetComponent<Image> ().color = new Color (19f / 255f, 143f / 255f, 86f / 255f, 1f);
		CoolDownElement2.GetComponent<Image> ().color = new Color (19f / 255f, 143f / 255f, 86f / 255f, 1f);
		Results.gameObject.SetActive (false);
		HUD.SetActive (true);
	}

	void Update () {
		_chronometer -= Time.deltaTime;
		if (_chronometer <= 0f) {
			Results.SetActive (true);
			Results.transform.GetChild (0).gameObject.SetActive (true);
			Results.transform.GetChild (1).gameObject.SetActive (false);
			HUD.gameObject.SetActive (false);
			Menu.transform.GetChild(1).gameObject.SetActive(true);
			gameObject.SetActive (false);
			return;
		}
		HUD.GetComponent<Text> ().text = Mathf.RoundToInt (_chronometer) + "";

		if (Input.GetMouseButtonDown (0) && _timer <= 0f) {
			float angle = FindTapAngle ();
			MoveActor (angle, _force);
			_timer = _COOLDOWN_LIMIT_;
			GameObject wave = Instantiate (Wave, new Vector3(transform.position.x, 0.3f, transform.position.z), Quaternion.identity);
			wave.transform.eulerAngles = new Vector3 (0f, 180f - angle * Mathf.Rad2Deg, 0f);
			CoolDownElement1.GetComponent<Image> ().color = new Color (201f / 255f, 46f / 255f, 75f / 255f, 1f);
		} else if (_timer > 0f) {
			_timer -= Time.deltaTime;
			CoolDownElement1.sizeDelta = new Vector3 (125f * (_COOLDOWN_LIMIT_ - _timer) / _COOLDOWN_LIMIT_, 20f);
		} else if (_timer <= 0f)
			CoolDownElement1.GetComponent<Image> ().color = new Color (19f / 255f, 143f / 255f, 86f / 255f, 1f);

		if (Input.GetMouseButtonDown (1) && _coolDown <= 0f) {
			float angle = FindTapAngle ();
			MoveActor (angle, _awakens);
			_coolDown = _COOLDOWN_THRESHOLD;
			GameObject wind = Instantiate (Wind, new Vector3 (transform.position.x, 0.3f, transform.position.z), Quaternion.identity);
			wind.transform.eulerAngles = new Vector3 (0f, -angle * Mathf.Rad2Deg, 0f);
			CoolDownElement2.GetComponent<Image> ().color = new Color (201f / 255f, 46f / 255f, 75f / 255f, 1f);
		} else if (_coolDown > 0f) {
			_coolDown -= Time.deltaTime;
			CoolDownElement2.sizeDelta = new Vector3 (125f * (_COOLDOWN_THRESHOLD - _coolDown) / _COOLDOWN_THRESHOLD, 20f);
		} else if (_coolDown < 0f)
			CoolDownElement2.GetComponent<Image> ().color = new Color (19f / 255f, 143f / 255f, 86f / 255f, 1f);
	}

	Directions FindTapDirection () {
		Vector3 touch = Camera.main.ScreenToViewportPoint (Input.mousePosition);
		Vector3 actor = Camera.main.WorldToViewportPoint (_actorTransform.position);
		float absX = Mathf.Abs (touch.x - actor.x);
		float absY = Mathf.Abs (touch.y - actor.y);
		if (absX > absY)
			return touch.x < actor.x ? Directions.West : Directions.East;
		else
			return touch.y < actor.y ? Directions.South : Directions.North;
	}

	float FindTapAngle () {
		Vector3 touch = Camera.main.ScreenToViewportPoint (Input.mousePosition);
		Vector3 actor = Camera.main.WorldToViewportPoint (_actorTransform.position);
		return Mathf.Atan2 (actor.y - touch.y, actor.x - touch.x);
	}

	void MoveActor (Directions direction) {
		if (direction == Directions.West)
			_actorBody.AddForce (_force, 0f, 0f);
		else if (direction == Directions.East)
			_actorBody.AddForce (-_force, 0f, 0f);
		else if (direction == Directions.North)
			_actorBody.AddForce (0f, 0f, -_force);
		else
			_actorBody.AddForce (0f, 0f, _force);
	}

	void MoveActor (float angle, float force) {
		_actorBody.velocity = new Vector3 (force * Mathf.Cos (angle), 0f, force * Mathf.Sin (angle));
	}
}
