#ifndef __SCENEIDCONTROL_H__
#define __SCENEIDCONTROL_H__

#include <wx/wx.h>
#include <wx/combobox.h>
#include <wx/popupwin.h>
#include <wx/xrc/xmlres.h>

#include "kernel.h"
#include "scenemanager.h"
#include "eventhandler.h"


namespace VRUT
{
	class SceneIDControl;

	class SceneIDDetailPopup : public wxPopupTransientWindow
	{
	private:

		wxScrolledWindow *m_panel;
		std::vector<wxCheckBox *> m_items;

	public:

		SceneIDDetailPopup(wxWindow *parent, int controlMode) : wxPopupTransientWindow(parent, wxBORDER_NONE)
		{
			m_panel = new wxScrolledWindow(this, wxID_ANY);
			m_panel->SetBackgroundColour(*wxLIGHT_GREY);
			wxBoxSizer *topSizer = new wxBoxSizer(wxVERTICAL);

			m_items.push_back(new wxCheckBox(m_panel, wxID_ANY, wxT("Auto from node")));
			m_items[0]->SetValue(((1<<0) & controlMode)!=0);
			topSizer->Add(m_items[0], 0, wxALL, 5);

			m_items.push_back(new wxCheckBox(m_panel, wxID_ANY, wxT("Auto")));
			m_items[1]->SetValue(((1<<1) & controlMode)!=0);
			topSizer->Add(m_items[1], 0, wxALL, 5);

			m_panel->SetSizer(topSizer);
			topSizer->Fit(m_panel);
			topSizer->Fit(this);
		}

		void OnDismiss();
	};

	class SceneIDControl : public wxComboBox
	{
		int controlMode;

		wxString formatChoice(Scene *scene);

		void OnLeftDown(wxMouseEvent &evt);
		void OnRightDown(wxMouseEvent &evt);
		void OnTextUpdated(wxCommandEvent &evt);

		void LocalInit(void) {
			this->Connect(wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler(SceneIDControl::OnLeftDown), NULL, this);
			this->Connect(wxID_ANY, wxEVT_RIGHT_DOWN, wxMouseEventHandler(SceneIDControl::OnRightDown), NULL, this);
			this->Connect(wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler(SceneIDControl::OnTextUpdated), NULL, this);
			//this->SetSelection(0);
		}

	protected:

		void SetControlMode(int newControlMode);

	public:
		// Default constructor (required for two-step construction when loading from XRC => default contructor -> create)
		SceneIDControl() : wxComboBox(), controlMode(0) {}

		SceneIDControl(wxWindow *parent, wxWindowID id, const wxString &value, const wxPoint &pos, const wxSize &size, const wxArrayString &choices, long style=0, const wxValidator &validator=wxDefaultValidator,
			const wxString &name=wxComboBoxNameStr) : wxComboBox(parent, id, value, pos, size, choices, style, validator, name), controlMode(0)
		{
			LocalInit();
		}

		bool Create(wxWindow *parent, wxWindowID id, const wxString &value, const wxPoint & pos, const wxSize & size, const wxArrayString & choices, long style = 0, const wxValidator &validator = wxDefaultValidator,
			const wxString & name = wxComboBoxNameStr)
		{
			 if ( !wxComboBox::Create(parent, id, value, pos, size, choices, style, validator, name) )
				return false;

			LocalInit();

			return true;
		}

		virtual void SetValue(const wxString& value);

		friend class SceneIDDetailPopup;

		DECLARE_DYNAMIC_CLASS(SceneIDControl)
	};

	class SceneIDControlXmlHandler : public wxXmlResourceHandler
	{
	public:
		// Constructor.
		SceneIDControlXmlHandler()
		{
			// this call adds support for all wxWidgets class styles
			// (e.g. wxBORDER_SIMPLE, wxBORDER_SUNKEN, wxWS_EX_* etc etc)
			XRC_ADD_STYLE(wxCB_SIMPLE);
			XRC_ADD_STYLE(wxCB_SORT);
			XRC_ADD_STYLE(wxCB_READONLY);
			XRC_ADD_STYLE(wxCB_DROPDOWN);
			XRC_ADD_STYLE(wxTE_PROCESS_ENTER);

			AddWindowStyles();
		}

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

			wxArrayString choices;

			control->Create( m_parentAsWindow,
				GetID(),
				GetText(wxT("value")),
				GetPosition(), GetSize(), 
				choices,
				GetStyle() | wxCB_READONLY, 
				wxDefaultValidator,
				GetName() );	

			// find the selection
			long selection = GetLong( wxT("selection"), -1 );
			if (selection != -1)
				control->SetSelection(selection);

			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("SceneIDControl"));
		}

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

}

#endif /* __SCENEIDCONTROL_H__ */
