using UnityEngine;
using System.Collections;

public class KeycodeScript : MonoBehaviour {
	
	
	public GameObject key0;
	public GameObject key1;
	public GameObject key2;
	public GameObject key3;
	public GameObject key4;
	public GameObject key5;
	public GameObject key6;
	public GameObject key7;
	public GameObject key8;
	public GameObject key9;
	public GameObject keyC;
	
	private string codeString = "";
	
	public TextMesh screenText;
	
	private bool won = false;
	
	// Use this for initialization
	void Start () {
	
	}
	
	void PunchKey(string inkey){
		
		if (won) return;
		
		codeString += inkey;
		
		screenText.text = codeString;
		
		if (codeString.Length == 4){
			if (codeString == "4682"){
				screenText.text = "4682 - YOU WIN!";
				won = true;
			}else{
				screenText.text = codeString + "\nWRONG CODE, TRY AGAIN";
			}
			
			codeString = "";
		}
		
	}
	
	// Update is called once per frame
	void Update () {
		
		if (Input.GetKeyDown("escape")){
			Application.LoadLevel(0);
		}
		
		
		
		Ray screenray = Camera.main.ScreenPointToRay(Input.mousePosition);
		
		RaycastHit screenHit = new RaycastHit();
		if (key0.collider.Raycast ( screenray, out screenHit, 99f)){
			if (Input.GetKeyDown("mouse 0")){
				PunchKey("0");
			}
		}
		
		 screenHit = new RaycastHit();
		if (key1.collider.Raycast ( screenray, out screenHit, 99f)){
			if (Input.GetKeyDown("mouse 0")){
				PunchKey("1");
			}
		}
		
		 screenHit = new RaycastHit();
		if (key2.collider.Raycast ( screenray, out screenHit, 99f)){
			if (Input.GetKeyDown("mouse 0")){
				PunchKey("2");
			}
		}
		
		 screenHit = new RaycastHit();
		if (key3.collider.Raycast ( screenray, out screenHit, 99f)){
			if (Input.GetKeyDown("mouse 0")){
				PunchKey("3");
			}
		}
		
		 screenHit = new RaycastHit();
		if (key4.collider.Raycast ( screenray, out screenHit, 99f)){
			if (Input.GetKeyDown("mouse 0")){
				PunchKey("4");
			}
		}
		
		 screenHit = new RaycastHit();
		if (key5.collider.Raycast ( screenray, out screenHit, 99f)){
			if (Input.GetKeyDown("mouse 0")){
				PunchKey("5");
			}
		}
		
		 screenHit = new RaycastHit();
		if (key6.collider.Raycast ( screenray, out screenHit, 99f)){
			if (Input.GetKeyDown("mouse 0")){
				PunchKey("6");
			}
		}
		
		 screenHit = new RaycastHit();
		if (key7.collider.Raycast ( screenray, out screenHit, 99f)){
			if (Input.GetKeyDown("mouse 0")){
				PunchKey("7");
			}
		}
		
		 screenHit = new RaycastHit();
		if (key8.collider.Raycast ( screenray, out screenHit, 99f)){
			if (Input.GetKeyDown("mouse 0")){
				PunchKey("8");
			}
		}
		
		 screenHit = new RaycastHit();
		if (key9.collider.Raycast ( screenray, out screenHit, 99f)){
			if (Input.GetKeyDown("mouse 0")){
				PunchKey("9");
			}
		}
		
		 screenHit = new RaycastHit();
		if (keyC.collider.Raycast ( screenray, out screenHit, 99f)){
			if (Input.GetKeyDown("mouse 0")){
				if (!won){
					
			codeString = "";
			screenText.text = "Enter Code:";
				}
			}
		}
	}
}
