// -*- C++ -*- generated by wxGlade 0.6.3 on Fri May 15 18:40:59 2009

#include "DBQueryObject.h"

#include "Toolkit.h"

#include "MainFrame.h"

// begin wxGlade: ::extracode
// end wxGlade



MyFrame::MyFrame(wxWindow* parent, int id, const wxString& title, const wxPoint& pos, const wxSize& size, long style):
    wxFrame(parent, id, title, pos, size, wxDEFAULT_FRAME_STYLE)
{
    // begin wxGlade: MyFrame::MyFrame
    label_3 = new wxStaticText(this, wxID_ANY, wxT("MPQ: "));
    m_editMPQ = new wxTextCtrl(this, wxID_ANY, wxEmptyString);
    label_4 = new wxStaticText(this, wxID_ANY, wxT("DBC: "));
    m_editDBC = new wxTextCtrl(this, wxID_ANY, wxEmptyString);
    label_5 = new wxStaticText(this, wxID_ANY, wxT("WDB:"));
    m_editWDB = new wxTextCtrl(this, wxID_ANY, wxEmptyString);
    m_radioDBC = new wxRadioButton(this, wxID_ANY, wxT("DBC Query"));
    m_radioWDB = new wxRadioButton(this, wxID_ANY, wxT("WDB Query"));
    static_line_1 = new wxStaticLine(this, wxID_ANY);
    label_1 = new wxStaticText(this, wxID_ANY, wxT("QUERY:"));
    label_6 = new wxStaticText(this, wxID_ANY, wxT("Pos: "));
    m_editQueryPos = new wxTextCtrl(this, wxID_ANY, wxEmptyString);
    label_7 = new wxStaticText(this, wxID_ANY, wxT("Type: "));
    m_editQueryType = new wxTextCtrl(this, wxID_ANY, wxEmptyString);
    label_8 = new wxStaticText(this, wxID_ANY, wxT("Value: "));
    m_editQueryValue = new wxTextCtrl(this, wxID_ANY, wxEmptyString);
    static_line_3 = new wxStaticLine(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLI_VERTICAL);
    label_2 = new wxStaticText(this, wxID_ANY, wxT("RESULT:"));
    label_9 = new wxStaticText(this, wxID_ANY, wxT("Pos: "));
    m_editResultPos = new wxTextCtrl(this, wxID_ANY, wxEmptyString);
    label_10 = new wxStaticText(this, wxID_ANY, wxT("Type: "));
    m_editResultType = new wxTextCtrl(this, wxID_ANY, wxEmptyString);
    label_11 = new wxStaticText(this, wxID_ANY, wxT("Value:"));
    m_editResultValue = new wxTextCtrl(this, wxID_ANY, wxEmptyString);
    static_line_2 = new wxStaticLine(this, wxID_ANY);
    m_btnClose = new wxButton(this, wxID_ANY + 100, wxT("&Close"));
    panel_1 = new wxPanel(this, wxID_ANY);
    m_btnQuery = new wxButton(this, wxID_ANY + 101, wxT("&Query!"));

    set_properties();
    do_layout();
    // end wxGlade
}


BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    // begin wxGlade: MyFrame::event_table
    EVT_RADIOBUTTON(wxID_ANY, MyFrame::OnRadioDBC)
    EVT_RADIOBUTTON(wxID_ANY, MyFrame::OnRadioWDB)
    EVT_BUTTON(wxID_ANY + 100, MyFrame::OnBtnClose)
    EVT_BUTTON(wxID_ANY + 101, MyFrame::OnBtnQuery)
    // end wxGlade
END_EVENT_TABLE();


void MyFrame::OnRadioDBC(wxCommandEvent &event)
{
    event.Skip();
    wxLogDebug(wxT("Event handler (MyFrame::OnRadioDBC) not implemented yet")); //notify the user that he hasn't implemented the event handler yet
}


void MyFrame::OnRadioWDB(wxCommandEvent &event)
{
    event.Skip();
    wxLogDebug(wxT("Event handler (MyFrame::OnRadioWDB) not implemented yet")); //notify the user that he hasn't implemented the event handler yet
}


void MyFrame::OnBtnClose(wxCommandEvent &event)
{
	Close();
    event.Skip();
    wxLogDebug(wxT("Event handler (MyFrame::OnBtnClose) not implemented yet")); //notify the user that he hasn't implemented the event handler yet
}


void MyFrame::OnBtnQuery(wxCommandEvent &event)
{
	if(m_radioDBC->GetValue())
	{
		DBCQuery();
	}
	else
	{
		WDBQuery();
	}

    event.Skip();
    wxLogDebug(wxT("Event handler (MyFrame::OnBtnQuery) not implemented yet")); //notify the user that he hasn't implemented the event handler yet
}


// wxGlade: add MyFrame event handlers


void MyFrame::set_properties()
{
    // begin wxGlade: MyFrame::set_properties
    SetTitle(wxT("WOW - DBC/WDB Query!"));
    SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
    // end wxGlade
}


void MyFrame::do_layout()
{
    // begin wxGlade: MyFrame::do_layout
    wxBoxSizer* sizer_1 = new wxBoxSizer(wxVERTICAL);
    wxBoxSizer* sizer_2 = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer* sizer_4 = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer* sizer_10 = new wxBoxSizer(wxVERTICAL);
    wxBoxSizer* sizer_16 = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer* sizer_15 = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer* sizer_14 = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer* sizer_5 = new wxBoxSizer(wxVERTICAL);
    wxBoxSizer* sizer_13 = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer* sizer_12 = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer* sizer_11 = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer* sizer_3 = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer* sizer_6 = new wxBoxSizer(wxVERTICAL);
    wxBoxSizer* sizer_7 = new wxBoxSizer(wxVERTICAL);
    wxBoxSizer* sizer_9 = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer* sizer_8 = new wxBoxSizer(wxHORIZONTAL);
    sizer_8->Add(label_3, 1, wxALIGN_CENTER_VERTICAL, 0);
    sizer_8->Add(m_editMPQ, 4, 0, 2);
    sizer_8->Add(label_4, 1, wxLEFT|wxALIGN_CENTER_VERTICAL, 4);
    sizer_8->Add(m_editDBC, 4, 0, 2);
    sizer_7->Add(sizer_8, 1, wxTOP|wxBOTTOM|wxEXPAND, 2);
    sizer_9->Add(label_5, 1, wxALIGN_CENTER_VERTICAL, 0);
    sizer_9->Add(m_editWDB, 9, 0, 0);
    sizer_7->Add(sizer_9, 1, wxTOP|wxEXPAND, 2);
    sizer_3->Add(sizer_7, 1, wxALL|wxEXPAND, 4);
    sizer_6->Add(m_radioDBC, 1, wxALL|wxALIGN_CENTER_VERTICAL, 4);
    sizer_6->Add(m_radioWDB, 1, wxALL|wxALIGN_CENTER_VERTICAL, 4);
    sizer_3->Add(sizer_6, 0, wxALL|wxEXPAND, 4);
    sizer_1->Add(sizer_3, 0, wxEXPAND, 0);
    sizer_1->Add(static_line_1, 0, wxALL|wxEXPAND, 4);
    sizer_5->Add(label_1, 0, wxBOTTOM|wxALIGN_CENTER_VERTICAL, 3);
    sizer_11->Add(label_6, 1, wxALIGN_CENTER_VERTICAL, 0);
    sizer_11->Add(m_editQueryPos, 4, 0, 2);
    sizer_5->Add(sizer_11, 0, wxLEFT|wxBOTTOM|wxEXPAND, 4);
    sizer_12->Add(label_7, 1, wxALIGN_CENTER_VERTICAL, 0);
    sizer_12->Add(m_editQueryType, 4, 0, 0);
    sizer_5->Add(sizer_12, 0, wxLEFT|wxBOTTOM|wxEXPAND, 4);
    sizer_13->Add(label_8, 1, wxALIGN_CENTER_VERTICAL, 0);
    sizer_13->Add(m_editQueryValue, 4, 0, 0);
    sizer_5->Add(sizer_13, 0, wxLEFT|wxEXPAND, 4);
    sizer_4->Add(sizer_5, 1, wxLEFT|wxRIGHT|wxEXPAND, 4);
    sizer_4->Add(static_line_3, 0, wxEXPAND, 0);
    sizer_10->Add(label_2, 0, wxBOTTOM|wxALIGN_CENTER_VERTICAL, 3);
    sizer_14->Add(label_9, 1, wxALIGN_CENTER_VERTICAL, 0);
    sizer_14->Add(m_editResultPos, 4, 0, 2);
    sizer_10->Add(sizer_14, 0, wxLEFT|wxBOTTOM|wxEXPAND, 4);
    sizer_15->Add(label_10, 1, wxALIGN_CENTER_VERTICAL, 0);
    sizer_15->Add(m_editResultType, 4, 0, 0);
    sizer_10->Add(sizer_15, 0, wxLEFT|wxBOTTOM|wxEXPAND, 4);
    sizer_16->Add(label_11, 1, wxALIGN_CENTER_VERTICAL, 0);
    sizer_16->Add(m_editResultValue, 4, 0, 0);
    sizer_10->Add(sizer_16, 0, wxLEFT|wxEXPAND, 4);
    sizer_4->Add(sizer_10, 1, wxLEFT|wxRIGHT|wxEXPAND, 4);
    sizer_1->Add(sizer_4, 1, wxEXPAND, 0);
    sizer_1->Add(static_line_2, 0, wxALL|wxEXPAND, 4);
    sizer_2->Add(m_btnClose, 0, wxALL, 4);
    sizer_2->Add(panel_1, 1, wxEXPAND, 0);
    sizer_2->Add(m_btnQuery, 0, wxALL, 4);
    sizer_1->Add(sizer_2, 0, wxEXPAND, 0);
    SetSizer(sizer_1);
    sizer_1->Fit(this);
    Layout();

	m_editMPQ->SetValue(wxT("E:\\Data\\zhCN\\locale-zhCN.MPQ"));
	m_editDBC->SetValue(wxT("DBFilesClient\\CharTitles.dbc"));
	m_editWDB->SetValue(wxT("E:\\Data\\cache\\WDB\\zhCN\\npccache.wdb"));
	m_radioDBC->SetValue(true);

    // end wxGlade
}

int MyFrame::DBCQuery()
{
	wxString mpq = m_editMPQ->GetValue();
	wxString dbc = m_editDBC->GetValue();

	wxString querypos = m_editQueryPos->GetValue();
	wxString queryvalue = m_editQueryValue->GetValue();

	wxString resultpos = m_editResultPos->GetValue();

	CDBQueryObject obj;
	obj.InitConfig(L"D:\\Studio\\DeWModel\\Apps\\wxDeMPQ\\DBCFields.xml");

	std::wstring strmpq = Toolkit::String2WString(Toolkit::wxString2String(mpq));
	std::string strdbc = Toolkit::wxString2String(dbc);

	TQueryData query;
	long i = 0;
	querypos.ToLong(&i, 10);
	query.m_iPos = i;
	queryvalue.ToLong(&i, 10);
	query.m_iValue = i;
	query.m_strValue = Toolkit::wxString2String(queryvalue);
	double d = 0.0;
	queryvalue.ToDouble(&d);
	query.m_fValue = d;
	
	TResultData result;
	resultpos.ToLong(&i, 10);
	result.m_iPos = i;

	if(obj.QueryDBC(strmpq, strdbc, query, result) != 0)
	{
		wxMessageBox(wxT("DBC query failed."));
	}
	else
	{
		m_editQueryType->SetValue(Field2Str(query.m_eFieldType));
		wxString value;
		m_editResultType->SetValue(ResultField2Str(result, value));
		m_editResultValue->SetValue(value);
	}

	return 0;
}

int MyFrame::WDBQuery()
{
	wxString wdb = m_editWDB->GetValue();

	wxString querypos = m_editQueryPos->GetValue();
	wxString queryvalue = m_editQueryValue->GetValue();

	wxString resultpos = m_editResultPos->GetValue();

	CDBQueryObject obj;
	obj.InitConfig(L"D:\\Studio\\DeWModel\\Apps\\wxDeMPQ\\WDBFields.xml");

	std::wstring strwdb = Toolkit::String2WString(Toolkit::wxString2String(wdb));

	TQueryData query;
	long i = 0;
	querypos.ToLong(&i, 10);
	query.m_iPos = i;
	queryvalue.ToLong(&i, 10);
	query.m_iValue = i;
	query.m_strValue = Toolkit::wxString2String(queryvalue);
	double d = 0.0;
	queryvalue.ToDouble(&d);
	query.m_fValue = d;
	
	TResultData result;
	resultpos.ToLong(&i, 10);
	result.m_iPos = i;

	if(obj.QueryWDB(strwdb, query, result) != 0)
	{
		wxMessageBox(wxT("WDB query failed."));
	}
	else
	{
		m_editQueryType->SetValue(Field2Str(query.m_eFieldType));
		wxString value;
		m_editResultType->SetValue(ResultField2Str(result, value));
		m_editResultValue->SetValue(value);
	}

	return 0;
}

wxString MyFrame::Field2Str(int type) const
{
	switch(type)
	{
	case 0:
		return wxT("Integer");
		break;
	case 1:
		return wxT("String");
		break;
	case 2:
		return wxT("Float");
		break;
	case 3:
		return wxT("Bit");
		break;
	case 4:
		return wxT("Byte");
		break;
	case 5:
		return wxT("CString");
		break;
	case 6:
		return wxT("Amount");
		break;
	default:
		return wxT("Undefined");
	};
	return wxT("NULL");
}

wxString MyFrame::ResultField2Str(const TResultData& data, wxString& value) const
{
	switch(data.m_eFieldType)
	{
	case 0:
		value = wxString::Format(wxT("%d"), data.m_iValue); 
		return wxT("Integer");
		break;
	case 1:
		value = Toolkit::String2wxString(data.m_strValue); 
		return wxT("String");
		break;
	case 2:
		value = wxString::Format(wxT("%f"), data.m_fValue); 
		return wxT("Float");
		break;
	case 3:
		value = wxString::Format(wxT("%d"), data.m_iValue); 
		return wxT("Bit");
		break;
	case 4:
		value = wxString::Format(wxT("%d"), data.m_iValue); 
		return wxT("Byte");
		break;
	case 5:
		value = Toolkit::String2wxString(data.m_strValue); 
		return wxT("CString");
		break;
	case 6:
		value = wxString::Format(wxT("%d"), data.m_iValue); 
		return wxT("Amount");
		break;
	default:
		value = wxString::Format(wxT("%d"), data.m_iValue); 
		return wxT("Undefined");
	};
	value = wxT("NULL");
	return wxT("NULL");
}

///////////////////////////////////////
class MyApp: public wxApp {
public:
    bool OnInit();
};

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()
{
    wxInitAllImageHandlers();
    MyFrame* frame_1 = new MyFrame(NULL, wxID_ANY, wxEmptyString);
    SetTopWindow(frame_1);
    frame_1->Show();
    return true;
}
