/*******************************************************************************/

#include "THPS2 PRE File EditorFrm.h"

/*******************************************************************************/

////Header Include Start
#include "Images/Self_THPS2_PRE_File_EditorFrm_XPM.xpm"
////Header Include End

/*******************************************************************************/

////Event Table Start
BEGIN_EVENT_TABLE(THPS2_PRE_File_EditorFrm,wxFrame)
	////Manual Code Start
	////Manual Code End
	
	EVT_CLOSE(THPS2_PRE_File_EditorFrm::OnClose)
	EVT_BUTTON(ID_BTNEXPORT,THPS2_PRE_File_EditorFrm::BTNExportClick)
	EVT_BUTTON(ID_BTNIMPORT,THPS2_PRE_File_EditorFrm::BTNImportClick)
	EVT_BUTTON(ID_BTNSAVE,THPS2_PRE_File_EditorFrm::BTNSaveClick)
	EVT_BUTTON(ID_BTNOPEN,THPS2_PRE_File_EditorFrm::BTNOpenClick)
	EVT_LISTBOX(ID_LSTFILES,THPS2_PRE_File_EditorFrm::LSTFilesDoubleClicked)
	EVT_LISTBOX_DCLICK(ID_LSTFILES,THPS2_PRE_File_EditorFrm::LSTFilesDoubleClicked)
END_EVENT_TABLE()
////Event Table End

/*******************************************************************************/

THPS2_PRE_File_EditorFrm::THPS2_PRE_File_EditorFrm(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint &position, const wxSize& size, long style)
: wxFrame(parent, id, title, position, size, style)
{
	CreateGUIControls();
	HTMLBmp -> SetBorders(0);
	presize = 0;
	pre = NULL;
	Print("Program Initialized...");
}

/*******************************************************************************/

THPS2_PRE_File_EditorFrm::~THPS2_PRE_File_EditorFrm()
{
    for(unsigned int x=0; x < presize; x++)
        delete[] pre[x].rawdata;
    delete[] pre;
}

/*******************************************************************************/

void THPS2_PRE_File_EditorFrm::CreateGUIControls()
{
	////GUI Items Creation Start

	DLGExport =  new wxFileDialog(this, wxT("Choose a file"), wxT(""), wxT(""), wxT("*.*"), wxSAVE);

	DLGSavePre =  new wxFileDialog(this, wxT("Choose a file"), wxT(""), wxT(""), wxT("*.PRE"), wxSAVE);

	DLGImport =  new wxFileDialog(this, wxT("Choose a file"), wxT(""), wxT(""), wxT("*.*"), wxOPEN | wxFILE_MUST_EXIST);

	DLGOpenPre =  new wxFileDialog(this, wxT("Choose a file"), wxT(""), wxT(""), wxT("*.PRE"), wxOPEN | wxFILE_MUST_EXIST);

	BTNExport = new wxButton(this, ID_BTNEXPORT, wxT("Export File"), wxPoint(75, 547), wxSize(75, 25), 0, wxDefaultValidator, wxT("BTNExport"));
	BTNExport->Enable(false);

	BTNImport = new wxButton(this, ID_BTNIMPORT, wxT("Import File"), wxPoint(0, 547), wxSize(75, 25), 0, wxDefaultValidator, wxT("BTNImport"));
	BTNImport->Enable(false);

	BTNSave = new wxButton(this, ID_BTNSAVE, wxT("Save .PRE"), wxPoint(75, 0), wxSize(75, 25), 0, wxDefaultValidator, wxT("BTNSave"));
	BTNSave->Enable(false);

	BTNOpen = new wxButton(this, ID_BTNOPEN, wxT("Open .PRE"), wxPoint(0, 0), wxSize(75, 25), 0, wxDefaultValidator, wxT("BTNOpen"));

	wxArrayString arrayStringFor_LSTFiles;
	LSTFiles = new wxListBox(this, ID_LSTFILES, wxPoint(0, 26), wxSize(149, 519), arrayStringFor_LSTFiles, wxLB_SINGLE);

	EDTConsole = new wxTextCtrl(this, ID_EDTCONSOLE, wxT(""), wxPoint(150, 440), wxSize(640, 131), wxTE_READONLY | wxTE_MULTILINE, wxDefaultValidator, wxT("EDTConsole"));
	EDTConsole->SetForegroundColour(wxColour(wxT("WHITE")));
	EDTConsole->SetBackgroundColour(wxColour(wxT("BLACK")));

	HTMLBmp = new wxHtmlWindow(this, ID_HTMLBMP, wxPoint(150, 0), wxSize(641, 438));

	SetTitle(wxT("THPS2 PRE File Editor"));
	SetIcon(Self_THPS2_PRE_File_EditorFrm_XPM);
	SetSize(8,8,800,600);
	Center();
	
	////GUI Items Creation End
}

/*******************************************************************************/

void THPS2_PRE_File_EditorFrm::OnClose(wxCloseEvent& event)
{
	Destroy();
}

/*******************************************************************************/

void THPS2_PRE_File_EditorFrm::LSTFilesDoubleClicked(wxCommandEvent& event)
{
	wxString ext;
	ext = pre[LSTFiles -> GetSelection()].name;
	if(ext.SubString(ext.Length() - 3,ext.Length() -1).MakeUpper() == "BMP")
	{
        FHandler.Open("Temp.Bmp",wxFile::write);
        FHandler.Write(pre[LSTFiles -> GetSelection()].rawdata,pre[LSTFiles -> GetSelection()].realsize);
        FHandler.Close();
        LoadBMP("Temp.Bmp");
    }
    else
        ClearBMP();
}

/*******************************************************************************/

void THPS2_PRE_File_EditorFrm::BTNImportClick(wxCommandEvent& event)
{
	// insert your code here
}

/*******************************************************************************/

void THPS2_PRE_File_EditorFrm::BTNExportClick(wxCommandEvent& event)
{
	// insert your code here
}

/*******************************************************************************/

void THPS2_PRE_File_EditorFrm::BTNOpenClick(wxCommandEvent& event)
{
	if(DLGOpenPre -> ShowModal() == wxID_OK)
	{
        bool strflag = true;
        unsigned short strsize = 0;
        unsigned char buf0;
        unsigned char buf1;
        unsigned char buf2;
        unsigned char buf3;
        unsigned int x = 0;
        for(unsigned int x=0; x < presize; x++)
            delete[] pre[x].rawdata;
        delete[] pre;
        pre = NULL;
        LSTFiles -> Clear();
        FHandler.Open(DLGOpenPre -> GetPath(),wxFile::read);
        Print("\nReading *.PRE File...");
        FHandler.Read(&presize,4);
        Print("\nFound ");Print(presize);Print(" Files...");
        pre = new prefile[presize];
        while(true)
        {
            if(x == presize)
                break;
            if(strflag)
            {
                FHandler.Read(&buf0,1);
                FHandler.Read(&buf1,1);
                FHandler.Read(&buf2,1);
                FHandler.Read(&buf3,1);
                strsize+=4;
                if(!buf0 || !buf1 || !buf2 || !buf3)
                {
                    FHandler.Seek(FHandler.Tell() - strsize);
                    FHandler.Read(&(pre[x].name[0]),strsize);
                    Print("\nName: ");
                    Print(pre[x].name);
                    pre[x].namesize = strsize;
                    LSTFiles -> Append(pre[x].name);
                    strsize = 0;
                    strflag = false;
                }
            }
            else
            {
                Print(" RealSize: ");
                FHandler.Read(&(pre[x].realsize),4);
                Print(pre[x].realsize);
                Print(" UIntSize: ");
                pre[x].bytesize = pre[x].realsize;
                while(pre[x].bytesize % 4 != 0)
                    pre[x].bytesize++;
                Print(pre[x].bytesize);
                pre[x].rawdata = new unsigned char[pre[x].bytesize];
                FHandler.Read(pre[x].rawdata,pre[x].bytesize);
                strflag = true;
                x++;
            } 
        }
        BTNSave -> Enable();
        BTNImport -> Enable();
        BTNExport -> Enable();
        FHandler.Close();
        Print("\nFile Readed OK!");
    }
}

/*******************************************************************************/

void THPS2_PRE_File_EditorFrm::BTNSaveClick(wxCommandEvent& event)
{
	// insert your code here
}

/*******************************************************************************/

void THPS2_PRE_File_EditorFrm::Print(const char* str)
{
    EDTConsole -> AppendText(str);
}

/*******************************************************************************/

void THPS2_PRE_File_EditorFrm::Print(unsigned int num)
{
    char aux[14];
    itoa(num,aux,10);
    EDTConsole -> AppendText(aux);
}

/*******************************************************************************/

void THPS2_PRE_File_EditorFrm::LoadBMP(const char* str)
{
    HTMLBmp -> SetPage("<HTML><BODY BGCOLOR=\"Grey\"><IMG SRC=\"");
    HTMLBmp -> AppendToPage(str);
    HTMLBmp -> AppendToPage("\"></IMG></BODY></HTML>");
}

/*******************************************************************************/

void THPS2_PRE_File_EditorFrm::ClearBMP()
{
    HTMLBmp -> SetPage("<HTML><BODY BGCOLOR=\"Grey\"></BODY></HTML>");
}

/*******************************************************************************/
