using UnityEngine;
using System.Collections;
using System;

public delegate void ClickEventHandler(object sender, EventArgs e);
public delegate void ReleaseEventHandler(object sender, EventArgs e);

public class GenericButton : MonoBehaviour 
{
	public event ClickEventHandler clickHandler;
	public event ReleaseEventHandler releaseHandler;
	private Camera hudCamera;

	protected virtual void OnClickButton(EventArgs e) 
	{
		if (clickHandler != null)
			clickHandler(this, e);
	}

	protected virtual void OnReleaseButton(EventArgs e) 
	{
		if (releaseHandler != null)
			releaseHandler(this, e);
	}
	
	private bool clickedButton = false;

	void Start () 
	{
		hudCamera = GameObject.Find("tk2dCamera").GetComponent<Camera>();
	}
	
	void Update () 
	{
		Ray shootRay = hudCamera.ScreenPointToRay(Input.mousePosition);
		RaycastHit shootRayHit;

		if (Physics.Raycast(shootRay, out shootRayHit, 50))
		{	
			if (shootRayHit.transform.tag == "MenuButton" &&
			    shootRayHit.transform == transform)
			{
				if (Input.GetMouseButtonDown(0))
				{
					OnClickButton(EventArgs.Empty);
					clickedButton = true;
				}
				else if (Input.GetMouseButtonUp(0) && clickedButton)
				{
					OnReleaseButton(EventArgs.Empty);
				}
				
				transform.localScale = new Vector3(1.1f, 1.1f, 1.1f);
			}
			else
			{
				transform.localScale = new Vector3(1, 1, 1);
			}
		}
		else
		{
			transform.localScale = new Vector3(1, 1, 1);
		}

		if (Input.GetMouseButtonUp(0))
		{
			clickedButton = false;
		}
	}
}
