#include "filecontrol.h"

#include <wx/filedlg.h>
#include "../data/resources/open.xpm"
#include "../data/resources/save.xpm"

#include "kernel.h"

using namespace VRUT;

#include <wx/filename.h>


IMPLEMENT_DYNAMIC_CLASS(FileControl, wxWindow)

void FileControl::OnClick(wxCommandEvent &evt)
{
	wxString path = ConvertToNativePath(textCtrl->GetValue());
	
	if (type)
	{
		wxFileDialog *fileDialog = new wxFileDialog(this, message, wxEmptyString, path, wildcard, style);
		if (fileDialog->ShowModal() == wxID_OK)
		{
			textCtrl->SetValue(fileDialog->GetPath());
			SendEvent();
		}
	}
	else
	{
		wxDirDialog *dirDialog = new wxDirDialog(this, message, path, wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST);
		if (dirDialog->ShowModal() == wxID_OK)
		{
			textCtrl->SetValue(dirDialog->GetPath());
			SendEvent();
		}
	}
}

void FileControl::SendEvent()
{
	wxCommandEvent textEvent(wxEVT_COMMAND_TEXT_ENTER, this->GetId());
	textEvent.SetEventObject(this);
	textEvent.SetString(ConvertToNativePath(textCtrl->GetValue()));
	this->ProcessEvent(textEvent);
}

wxString FileControl::ConvertToNativePath(const wxString &path)
{
	wxString nativePath = path;
	wxString separ(wxFileName::GetPathSeparator());

	nativePath.Replace(wxT("\\"), separ);
	nativePath.Replace(wxT("/"), separ);

	return nativePath;
}

void FileControl::SetMode(long newStyle) {

	style = newStyle;

	if(!button)
		button = new wxBitmapButton(this, wxID_ANY, wxBitmap((style & wxFD_OPEN) ? open_xpm : save_xpm));
	else
		button->SetBitmap(wxBitmap((style & wxFD_OPEN) ? open_xpm : save_xpm));
}

void FileControl::LocalInit(const wxString &newValue, long newStyle, const wxString &newWildcard, bool newType)
{
	style = newStyle;
	wildcard = newWildcard;
	type = newType;

	wxString nativePath = ConvertToNativePath(newValue);

	textCtrl = new wxTextCtrl(this, wxID_ANY, nativePath, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
	button = new wxBitmapButton(this, wxID_ANY, wxBitmap((style & wxFD_OPEN) ? open_xpm : save_xpm));
	
	wxSize buttonSize = button->GetSize();
	buttonSize.x += 6;
	button->SetMinSize(buttonSize);

	textCtrl->Connect(wxID_ANY, wxEVT_COMMAND_TEXT_ENTER, wxCommandEventHandler(FileControl::OnTextEnter), NULL, this);
	button->Connect(wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(FileControl::OnClick), NULL, this);

	wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);

	sizer->Add(textCtrl);
	sizer->Add(button);
			
	SetSizerAndFit(sizer);
}

bool FileControl::Create(wxWindow * parent, wxWindowID id, const wxPoint & pos, const wxSize & size, long style, const wxString & name)
{
	if ( !wxWindow::Create(parent, id, pos, size, style, name) )
		return false;

	LocalInit();

	return true;
}

FileControl::FileControl(wxWindow *parent, wxWindowID id, const wxString &value, long style, const wxString &wildcard, bool type)
			: wxWindow(parent, id)
{
	LocalInit(value, style, wildcard, type);
}

// Register with wxWidgets' dynamic class subsystem.
IMPLEMENT_DYNAMIC_CLASS(FileControlXmlHandler, wxXmlResourceHandler)
