#include <stream.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>

#include <clib/utility_protos.h>

#include "GUIC_Application.hpp"
#include "GUIC_Checkbox.hpp"
#include "GUIC_Cycle.hpp"
#include "GUIC_Error.hpp"
#include "GUIC_Event.hpp"
#include "GUIC_Exceptions.hpp"
#include "GUIC_File.hpp"
#include "GUIC_FileString.hpp"
#include "GUIC_ImageButton.hpp"
#include "GUIC_Message.hpp"
#include "GUIC_Panel.hpp"
#include "GUIC_PathString.hpp"
#include "GUIC_Screen.hpp"
#include "GUIC_SlidingInteger.hpp"
#include "GUIC_String.hpp"
#include "GUIC_System.hpp"

#include "MainWindow.hpp"

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

MainWindowC::MainWindowC 			(GUIC_ApplicationC &a, GUIC_ScreenC &s, PrefsWindowC &p, HTMLWindowC &h, ManagerWindowC &m, CompareWindowC &c) : GUIC_WindowC(5,2)
{
	app 				= &a;
	screen 			= &s;
	pWindow			= &p;
	hWindow			= &h;
	mWindow		= &m;
	cWindow			= &c;
	
	fileRequester.setPathPart("projects");
	
	panel				= new GUIC_PanelC			(GUIC_Horizontal, TRUE); /* TRUE means: no space between the objects */
	
	loadButton		= new GUIC_ImageButtonC ("gfx/load.iff");
	saveButton		= new GUIC_ImageButtonC ("gfx/save.iff");
	button6			= new GUIC_ImageButtonC ("gfx/compare.iff");
	button1			= new GUIC_ImageButtonC ("gfx/HTML.iff");
	button2			= new GUIC_ImageButtonC ("gfx/Manager.iff");
	button3			= new GUIC_ImageButtonC ("gfx/Prefs.iff");
	button4			= new GUIC_ImageButtonC ("gfx/Help.iff");
	button5			= new GUIC_ImageButtonC ("gfx/Shell.iff");
	
	panel->add (loadButton);
	panel->add (saveButton);
	panel->add (button6);
	panel->add (button1);
	panel->add (button2);
	panel->add (button3);
	panel->add (button4);
	panel->add (button5);

	add(panel);
	
	activate();
	setTitle("Gallery");
	setGuideContext("Usage");

	app->addPrefs("Usage", this);
}
MainWindowC::~MainWindowC 		(VOID)
{
	cleanUp();
}

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

STRPTR	MainWindowC::getClass	(VOID)
{
	return "MainWindowC";
}

BOOL 	MainWindowC::action 		(GUIC_EventC &event)
{
	switch (event.id)
		{
		case GUIC_GadgetEvent:
			return TRUE;
			break;
		case GUIC_ObjectEvent:
			if (event.object == (GUIC_GUIObjectC *) loadButton)
				{
				app->setBusy(TRUE);
				if (fileRequester.request(this, GUIC_Load))
					{
					if (! load(fileRequester.getFileName()))
						{
						GUIC_ErrorC err ("Error", "Could not load data file.");
						err.request(this);
						}
					}
				app->setBusy(FALSE);
				}
			else if (event.object == (GUIC_GUIObjectC *) saveButton)
				{
				app->setBusy(TRUE);
				if (fileRequester.request(this, GUIC_Save))
					{
					if (! save(fileRequester.getFileName()))
						{
						GUIC_ErrorC err ("Error", "Could not save data file.");
						err.request(this);
						}
					}
				app->setBusy(FALSE);
				}
			else if (event.object == (GUIC_GUIObjectC *) button1) // HTML
				{
				screen->remove(pWindow);
				screen->remove(mWindow);
				screen->remove(cWindow);
				screen->add(hWindow);
				}
			else if (event.object == (GUIC_GUIObjectC *) button2) // Manager
				{
				screen->remove(pWindow);
				screen->remove(hWindow);
				screen->remove(cWindow);
//				screen->add(mWindow);
				GUIC_MessageC message ("Gallery", "Sorry, but this part of Gallery\nis not yet implemented.", "Ok");
				message.request(this);
				}
			else if (event.object == (GUIC_GUIObjectC *) button3) // Prefs
				{
				screen->remove(hWindow);
				screen->remove(mWindow);
				screen->remove(cWindow);
				screen->add(pWindow);
				}
			else if (event.object == (GUIC_GUIObjectC *) button4) // Guide
				{
				app->showAmigaGuide(screen, "MAIN");
				}
			else if (event.object == (GUIC_GUIObjectC *) button5) // Shell
				{
				GUIC_SystemC::runProgram(pWindow->st_shellCommand->get(), 50000);
				}
			else if (event.object == (GUIC_GUIObjectC *) button6) // Compare
				{
				screen->remove(hWindow);
				screen->remove(mWindow);
				screen->remove(pWindow);
				screen->add(cWindow);
				}
			return TRUE;
			break;
		case GUIC_OpenWindow:
			return TRUE;
			break;
		case GUIC_CloseWindow:
			return FALSE;
			break;
		}
	return FALSE;
}

BOOL		MainWindowC::load			(STRPTR file)
{
	STRPTR s = 0;
	
	try
		{
		GUIC_FileC f(file);
		
		s = f.readLine();
		if (!s) return FALSE;
		
		if (! Stricmp(s, ("Gallery #1")) )
			{
			s = f.readLine();
			if (!s) return FALSE;
			pWindow->ps_thumbnailPath->set(s);
			
			s = f.readLine();
			if (!s) return FALSE;
			pWindow->ps_temporaryPath->set(s);

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->si_thumbnailWidth->set(atol(s));

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->si_thumbnailHeight->set(atol(s));

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->cb_thumbnailHide->set( Stricmp(s, "TRUE") ? FALSE : TRUE);

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->fs_pattern1->set(s);

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->fs_pattern2->set(s);

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->fs_sound1->set(s);

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->fs_sound2->set(s);

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->st_filename->set(s);

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->cy_suffix->setActive(atol(s));

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->cb_frames->set( Stricmp(s, "TRUE") ? FALSE : TRUE);

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->si_frameWidth->set(atol(s));

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->si_tableLines->set(atol(s));

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->si_tableColumns->set(atol(s));

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->si_tableBorderSize->set(atol(s));

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->cb_showSize->set( Stricmp(s, "TRUE") ? FALSE : TRUE);

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->cb_showDate->set( Stricmp(s, "TRUE") ? FALSE : TRUE);

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->cb_showDimensions->set( Stricmp(s, "TRUE") ? FALSE : TRUE);

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->st_tableColor1->set(s);

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->st_tableColor2->set(s);
			
			s = f.readLine();
			if (!s) return FALSE;
			hWindow->ps_path->set(s);
			}
		else if (! Stricmp(s, ("Gallery #2")) )
			{
			s = f.readLine();
			if (!s) return FALSE;
			pWindow->ps_thumbnailPath->set(s);
			
			s = f.readLine();
			if (!s) return FALSE;
			pWindow->ps_temporaryPath->set(s);

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->si_thumbnailWidth->set(atol(s));

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->si_thumbnailHeight->set(atol(s));

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->cb_thumbnailHide->set( Stricmp(s, "TRUE") ? FALSE : TRUE);

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->fs_pattern1->set(s);

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->fs_pattern2->set(s);

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->fs_sound1->set(s);

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->fs_sound2->set(s);

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->st_filename->set(s);

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->cy_suffix->setActive(atol(s));

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->cb_frames->set( Stricmp(s, "TRUE") ? FALSE : TRUE);

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->si_frameWidth->set(atol(s));

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->si_tableLines->set(atol(s));

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->si_tableColumns->set(atol(s));

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->si_tableBorderSize->set(atol(s));

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->cb_showSize->set( Stricmp(s, "TRUE") ? FALSE : TRUE);

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->cb_showDate->set( Stricmp(s, "TRUE") ? FALSE : TRUE);

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->cb_showDimensions->set( Stricmp(s, "TRUE") ? FALSE : TRUE);

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->st_tableColor1->set(s);

			s = f.readLine();
			if (!s) return FALSE;
			pWindow->st_tableColor2->set(s);
			
			s = f.readLine();
			if (!s) return FALSE;
			pWindow->fs_up->set(s);
			
			s = f.readLine();
			if (!s) return FALSE;
			pWindow->fs_left->set(s);
			
			s = f.readLine();
			if (!s) return FALSE;
			pWindow->fs_right->set(s);

			s = f.readLine();
			if (!s) return FALSE;
			hWindow->ps_path->set(s);
			}
		else return FALSE;
		}
	catch (GUIC_Exception &ex) { return FALSE; }

	return TRUE;
}
BOOL		MainWindowC::save		(STRPTR file)
{
	try
		{
		GUIC_FileC f(file, GUIC_Write);
		
		f.writeLn("Gallery #2");
		f.writeLn(pWindow->ps_thumbnailPath->get());
		f.writeLn(pWindow->ps_temporaryPath->get());
		f.writeLn(pWindow->si_thumbnailWidth->get());
		f.writeLn(pWindow->si_thumbnailHeight->get());
		f.writeLn(pWindow->cb_thumbnailHide->get());
		f.writeLn(pWindow->fs_pattern1->get());
		f.writeLn(pWindow->fs_pattern2->get());
		f.writeLn(pWindow->fs_sound1->get());
		f.writeLn(pWindow->fs_sound2->get());
		f.writeLn(pWindow->st_filename->get());
		f.writeLn(pWindow->cy_suffix->getActive());
		f.writeLn(pWindow->cb_frames->get());
		f.writeLn(pWindow->si_frameWidth->get());
		f.writeLn(pWindow->si_tableLines->get());
		f.writeLn(pWindow->si_tableColumns->get());
		f.writeLn(pWindow->si_tableBorderSize->get());
		f.writeLn(pWindow->cb_showSize->get());
		f.writeLn(pWindow->cb_showDate->get());
		f.writeLn(pWindow->cb_showDimensions->get());
		f.writeLn(pWindow->st_tableColor1->get());
		f.writeLn(pWindow->st_tableColor2->get());
		f.writeLn(pWindow->fs_up->get());
		f.writeLn(pWindow->fs_left->get());
		f.writeLn(pWindow->fs_right->get());
		
		f.writeLn(hWindow->ps_path->get());
		}
	catch (GUIC_Exception &ex) { return FALSE; }

	return TRUE;
}

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

VOID 		MainWindowC::cleanUp	(VOID)
{
	delete loadButton;
	delete saveButton;
	delete button1;
	delete button2;
	delete button3;
	delete button4;
	delete button5;
	delete button6;
	
	delete panel;
}

