
#include "sceneidcontrol.h"

using namespace VRUT;


// Register with wxWidgets' dynamic class subsystem.
IMPLEMENT_DYNAMIC_CLASS(SceneIDControl, wxComboBox)
IMPLEMENT_DYNAMIC_CLASS(SceneIDControlXmlHandler, wxXmlResourceHandler)

void SceneIDDetailPopup::OnDismiss()
{
	int	controlMode = 0;

	for (size_t i=0; i<m_items.size(); ++i)
		if (m_items[i]->GetValue())
			controlMode += 1<<i;

	((SceneIDControl*)GetParent())->SetControlMode(controlMode);

	wxPopupTransientWindow::OnDismiss();
}

wxString SceneIDControl::formatChoice(Scene *scene)
{
	if (scene)
		return wxString::Format(wxT("%lu ("), (unsigned long)scene->GetID())+scene->GetName()+wxT(")");

	return wxString::Format(wxT("%lu (NoneScene)"), (unsigned long)SCENE_ID_NONE);
}

void SceneIDControl::OnLeftDown(wxMouseEvent &evt)
{
	int selSceneID;
	if (GetStringSelection() != wxT(""))
		TextParser(GetStringSelection().utf8_str()).ParseInt(&selSceneID);
	else
		TextParser(GetValue().utf8_str()).ParseInt(&selSceneID);

	Clear();
	std::vector<smart_pointer_v2<Scene> > scenes;
	KERNEL->sceneManager->GetScenes(scenes);
	wxArrayString tmpList;
	tmpList.push_back(formatChoice(NULL));

	int selection = 0;
	for (size_t i = 0; i < scenes.size(); i++)
	{
		tmpList.push_back(formatChoice(scenes[i].getPtr()));
		if (selSceneID == scenes[i]->GetID())
			selection = i + 1;
	}
	Append(tmpList);
	SetSelection(selection);
	evt.Skip();
}

void SceneIDControl::OnRightDown(wxMouseEvent &evt)
{
	SceneIDDetailPopup *detailPopup = new SceneIDDetailPopup(this, controlMode);
	wxWindow *btn = (wxWindow*) evt.GetEventObject();
	wxPoint pos = btn->ClientToScreen(wxPoint(0,0));
	wxSize sz = btn->GetSize();
	detailPopup->Position(pos, sz);
	detailPopup->Popup(this);
	evt.Skip();
}

void SceneIDControl::OnTextUpdated(wxCommandEvent &evt)
{
	wxWindow * win = dynamic_cast<wxWindow *>(evt.GetEventObject());
	Parameter::ParameterIdentifier pi = Parameter::ParameterIdentifier::FromParamName(win->GetName());
	//KERNEL->environment.SetParam(pi, evt.GetString());
	KERNEL->GetMessageSink()->PostEvent(GET_EVT_PARAM_SET(pi, evt.GetString()));
}

void SceneIDControl::SetValue(const wxString& value)
{
	int valSceneID;
	TextParser(value.utf8_str()).ParseInt(&valSceneID);
	smart_pointer_v2<Scene> scene;
	KERNEL->sceneManager->GetScene(scene, (SCENE_ID)valSceneID);
	wxString sceneStr(formatChoice(scene.getPtr()));
	wxArrayString choices = GetStrings();

	for (size_t i = 0; i < choices.size(); i++)
	{
		int choiceSceneID;
		TextParser(choices[i].utf8_str()).ParseInt(&choiceSceneID);

		if (choiceSceneID == valSceneID)
		{
			if (choices[i] != sceneStr) //workaround for wxWidgets 3.0 and gtk
				wxComboBox::SetString(i, sceneStr);
			wxComboBox::SetSelection(i);
			//wxComboBox::SetValue(choices[i]);
			return;
		}
	}

	Append(sceneStr);
	wxComboBox::SetValue(sceneStr);
}

void SceneIDControl::SetControlMode(int newControlMode)
{
	if (newControlMode != controlMode)
	{
		if(	(newControlMode & (1<<0)) != (controlMode & (1<<0)))
		{
			Parameter::ParameterIdentifier pi = Parameter::ParameterIdentifier::FromParamName(GetName());
			wxString paramValue = wxString::Format("AutoFromNodeMode %i", ((newControlMode & (1<<0))) ? 1 : 0);
			KERNEL->GetMessageSink()->PostEvent(GET_EVT_PARAM_SET(pi, paramValue));
		}

		if(	(newControlMode & (1<<1)) != (controlMode & (1<<1)))
		{
			Parameter::ParameterIdentifier pi = Parameter::ParameterIdentifier::FromParamName(GetName());
			wxString paramValue = wxString::Format("AutoMode %i", ((newControlMode & (1<<1))) ? 1 : 0);
			KERNEL->GetMessageSink()->PostEvent(GET_EVT_PARAM_SET(pi, paramValue));
		}
	}

	controlMode = newControlMode;
}
