using UnityEngine;
using System.Collections;

public class Submit : FunObject {

	tk2dSprite _sprite;
	bool _hover = false;
	// Use this for initialization
	void Start () {
		_sprite = GetComponent<tk2dSprite>();
		_sprite.color = new Color(_sprite.color.r, _sprite.color.g, _sprite.color.b, 0.0f);
	}
	
	public bool IsHovered()
	{
		return _hover;	
	}
	
	// Update is called once per frame
	void Update () {
		
		float col = _hover ? 1.0f : 0.8f;
		Color c = new Color(_sprite.color.r, _sprite.color.g, _sprite.color.b, (Globals.Submitting ? 1.0f : 0.0f) * col);
		_sprite.color = Color.Lerp(_sprite.color, c, Time.deltaTime * 8.0f);
		
		Vector3 mouse = Camera.main.ScreenToWorldPoint(Input.mousePosition);
		if(mouse.x > x - 12 && mouse.x < x + 12 && mouse.y > y - 12 && mouse.y < y + 12) 
			_hover = true;
		else 
			_hover = false;
		
		if(_hover && Input.GetMouseButtonDown(0))
		{
			string name = Globals.Name.Substring(0, Mathf.Min(Globals.Name.Length, 12));
			name = name.Replace(" ", string.Empty);
			HSController.shared.PostScore(name, Globals.Score);
			GameObject.Find("SubmitWindow").GetComponent<SubmitWindow>().Finish();
			GameObject.Find("SubmitWindow").GetComponent<SubmitWindow>().gameObject.SetActive(false);
			//Globals.Submitting = false;
			//GameObject.Find("LetterBoard(Clone)").GetComponent<LetterBoard>().Restart();
		}
		
	}
}
