using UnityEngine;
using System.Collections;

public class Cancel : FunObject {

	tk2dSprite _sprite;
	bool _hover = false;
	Vector3 _pos;
	
	SubmitWindow _submit;
	
	float _offset = 0.0f;
	// Use this for initialization
	void Start () {
		_sprite = GetComponent<tk2dSprite>();
		_sprite.color = new Color(_sprite.color.r, _sprite.color.g, _sprite.color.b, 0.0f);
		_pos = transform.position;
		
		_submit = GameObject.Find("SubmitWindow").GetComponent<SubmitWindow>();
		_submit.gameObject.SetActive(false);
	}
	
	// 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.AcceptMode ? 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 && Globals.AcceptMode) 
			_hover = true;
		else 
			_hover = false;
		
		
		position = _pos + new Vector3(_offset, 0.0f, 0.0f);
		_offset = Mathf.Lerp(_offset, Globals.AcceptMode ? 33.0f : 0.0f, Time.deltaTime * 10.0f);	
		
		if(_hover && Input.GetMouseButtonDown(0))
		{
			Globals.AcceptMode = false;
		}
		
		if(_offset > 30.0f && Input.GetMouseButtonDown(0))
		{
			if(transform.FindChild("Accept").GetComponent<Accept>().IsHovered())
			{
				Globals.Submitting = true;
				_submit.gameObject.SetActive(true);
			}
		}
		
	}
}
