using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class ClickMe : MonoBehaviour {

	float timer;
	float loadTimer;
	SpriteRenderer sr;
	bool loadingNextLevel;
	public GameObject clickSoundPrefab;

	// Use this for initialization
	void Start () {
		sr = this.gameObject.GetComponent<SpriteRenderer> ();
		sr.enabled = false;
		loadingNextLevel = false;
	}
	
	// Update is called once per frame
	void Update () {
		timer += Time.deltaTime;

		if (loadingNextLevel)
		{
			if (timer > (loadTimer + 0.666f))
			{
				SceneManager.LoadScene (2);
			}
		}
		else
		{
			if (timer > (6.66f / 2))
			{
				sr.enabled = true;
			}

			if (Input.GetMouseButtonDown (0))
			{
				if (Camera.main.ScreenToWorldPoint (Input.mousePosition).x > -6.2f && Camera.main.ScreenToWorldPoint (Input.mousePosition).x < 5.3f
				  && Camera.main.ScreenToWorldPoint (Input.mousePosition).y > -6.2f && Camera.main.ScreenToWorldPoint (Input.mousePosition).y < -4.5f)
				{
					loadingNextLevel = true;
					sr.enabled = false;
					Destroy (GameObject.Find ("introMusic"));
					Instantiate (clickSoundPrefab, this.transform.position, Quaternion.Euler(0,0,0));
					loadTimer = timer;
				}

			}
		}
	}
}
