using UnityEngine;
using System.Collections.Generic;

public class yanarliDonerli : MonoBehaviour
{

		public List<Material> Mats;
		// Use this for initialization
		void Start ()
		{
				if (PlayerPrefs.GetInt ("highscore", 0) <= Globals.forwPoints + Globals.coinPoints * 5) {
						PlayerPrefs.SetInt ("highscore", Globals.forwPoints + Globals.coinPoints * 5);
						isHigh = true;
				}

				PlayerPrefs.SetInt ("gold", PlayerPrefs.GetInt ("gold", 0) + Globals.coinPoints);
		}

		float last = -0.5f;
		// Update is called once per frame
		void Update ()
		{
				if (Time.timeSinceLevelLoad - last > 0.5f) {
						foreach (GameObject go in GameObject.FindGameObjectsWithTag("tile")) {
								go.renderer.material = Mats [Random.Range (0, Mats.Count)];
						}
						last = Time.timeSinceLevelLoad;
				}

				if ((int)Time.timeSinceLevelLoad % 2 == 0) {
						rotate.last = (rotate.last + 1) % 4;
				}
		}

		public GUITexture coinTexture;
		public GUIText coinText;
		public GUITexture pointTexture;
		public GUIText pointText;
		public GUITexture lifesTexture;
		public Texture2D exitText, newgText;
		public GUIText lifesText;
		public bool isHigh = false;

		void OnGUI ()
		{
				Vector2 coinTextOffset = new Vector2 (0, -Screen.height / 4.5f - Screen.width / 40 + 20);
				coinText.pixelOffset = coinTextOffset;
				coinText.text = Globals.coinPoints.ToString ();

				Rect coinInset = new Rect (-Screen.width / 20 - 20, -Screen.height / 1.5f - Screen.width / 20 - 10, Screen.width / 20, Screen.width / 20);
				coinTexture.pixelInset = coinInset;

				Vector2 pointTextOffset = new Vector2 (0, -Screen.height / 4.5f + Screen.width / 40 + 20);
				pointText.pixelOffset = pointTextOffset;
				pointText.text = (Globals.forwPoints + Globals.coinPoints * 5).ToString () + (isHigh ? "\r\nNEW HIGHSCORE!" : "");

				Rect pointInset = new Rect (-Screen.width / 20 - 20, -Screen.height / 1.5f, Screen.width / 20, Screen.width / 20);
				pointTexture.pixelInset = pointInset;

				if (GUI.Button (new Rect (Screen.width / 2 - (newgText.width + exitText.width) / 2, Screen.height - newgText.height, newgText.width, newgText.height), newgText)) {
						Globals.Lifes = 5;
						Globals.coinPoints = 0;
						Test.AtyaraBegin = -11.0f;
						Globals.forwPoints = 0;
						Messenger.Broadcast ("startgame");
						Application.LoadLevel ("sceneTest");
				}
				if (GUI.Button (new Rect (Screen.width / 2 - (newgText.width + exitText.width) / 2 + newgText.width, Screen.height - exitText.height, exitText.width, exitText.height), exitText)) {
						Globals.IsGamePaused = true;
						Globals.Lifes = 5;
						Test.AtyaraBegin = -11.0f;
						Globals.coinPoints = 0;
						Globals.forwPoints = 0;
						Globals.dying = false;
						Application.LoadLevel ("sceneTest");
				}
		}
}
