#ifndef __SHORTCUTPICKER_H__
#define __SHORTCUTPICKER_H__

#include <wx/wx.h>
//#include <wx/spinctrl.h>
//#include <wx/tokenzr.h>
#include <wx/xrc/xmlres.h>
//#include <float.h>
//#include <vector>

#include "kernel.h"
//#include "spinsliderd.h"
#include "variant.h"

namespace VRUT
{

	/**
	* Dialog to record shortcut.
	*/
	class ShortCutDialog : public wxDialog 
	{
		private:

			ShortCut shortCut;

			void ShowKeyInfo(ShortCut sc);

		protected:

			wxStaticText* shortcutLabel;
			wxTextCtrl* shortcutText;
			wxStaticText* infoLabel;
			wxTextCtrl* infoText;
			wxButton* okButton;
			wxButton* cancelButton;

			/// This is where we will filter events before wxWidgets can process them
			void OnCharHook(wxKeyEvent& evt);

			/**
			* handling OK and CANCEL button
			*/
			void OnClick(wxCommandEvent &evt);

		public:
		
			ShortCutDialog( wxWindow* parent, wxWindowID id = wxID_ANY, ShortCut sc = ShortCut(), const wxString& title = wxT("ShortCut Selection Dialog"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 252,130 ), long style = wxDEFAULT_DIALOG_STYLE );
			~ShortCutDialog();

			ShortCut GetShortCut(void)
			{
				return shortCut;
			}
	};

	/**
	* wxWidget to capture shortcut
	*/
	class ShortCutPicker : public wxButton
	{
		wxString strValue;
		ShortCut shortCut;

		ShortCutDialog *dialog;

		/**
		* opens shortcut dialog
		*/
		void OnClick(wxMouseEvent &evt);

		/**
		* Generated event when shortcut is changed
		*/
		void SendEvent();

		void ShowKeyInfo(void);

		void LocalInit(const wxString &value) 
		{
			dialog = NULL;

			shortCut.modifiers = wxMOD_NONE;
			shortCut.keyCode = WXK_NONE;
			strValue = wxString::Format("%i,%i", shortCut.modifiers, shortCut.keyCode);

			wxString newValue = value;

			if (newValue.IsEmpty()) //  == wxT(""))
			{
				newValue = wxT("0,0");
			}

			this->SetValue(newValue);
			this->Connect(wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxMouseEventHandler(ShortCutPicker::OnClick), NULL, this);
		}

	public:

		void SetValue(const wxString &value);

		wxString GetValue()
		{
			return strValue;
		}


		ShortCutPicker() : wxButton(), dialog(NULL) {}

		ShortCutPicker(wxWindow *parent, wxWindowID id, const wxString &label, const wxString &value) : wxButton(parent, id, label)
		{
			LocalInit(value);
		}

		bool Create(wxWindow *parent, wxWindowID id, const wxString & label = wxEmptyString, const wxPoint & pos = wxDefaultPosition, const wxSize & size = wxDefaultSize, long style = 0, const wxValidator & validator = wxDefaultValidator, const wxString & name = wxButtonNameStr) 	
		{
			if ( !wxButton::Create(parent, id, label, pos, size, style, validator, name) )
				return false;

			LocalInit(wxT(""));

			return true;
		}

		DECLARE_DYNAMIC_CLASS(ShortCutPicker)
	};

	class ShortCutPickerXmlHandler : public wxXmlResourceHandler
	{
		public:
			// Constructor.
			ShortCutPickerXmlHandler()
			{
				XRC_ADD_STYLE(wxBU_LEFT);
				XRC_ADD_STYLE(wxBU_RIGHT);
				XRC_ADD_STYLE(wxBU_TOP);
				XRC_ADD_STYLE(wxBU_BOTTOM);
				XRC_ADD_STYLE(wxBU_EXACTFIT);

				AddWindowStyles();
			}

			// Creates the control and returns a pointer to it.
			virtual wxObject *DoCreateResource()
			{
				XRC_MAKE_INSTANCE(control, ShortCutPicker)

				control->Create(m_parentAsWindow,
					GetID(),
					GetText(wxT("label")),
					GetPosition(), GetSize(),
					GetStyle(),
					wxDefaultValidator,
					GetName()
				);

				if (GetBool(wxT("default"), 0))
					control->SetDefault();

				if ( GetParamNode("bitmap") )
				{
					control->SetBitmap(GetBitmap("bitmap", wxART_BUTTON), GetDirection("bitmapposition"));
				}

				SetupWindow(control);

				return control;
			}

			// Returns true if we know how to create a control for the given node.
			virtual bool CanHandle(wxXmlNode *node)
			{
				// this function tells XRC system that this handler can parse
				// the <object class="MyControl"> tags
				return IsOfClass(node, wxT("ShortCutPicker"));
			}

		// Register with wxWidgets' dynamic class subsystem.
		DECLARE_DYNAMIC_CLASS(ShortCutPickerXmlHandler)
	};

}

#endif
