#include <tcl.h>
#include <tk.h>

#include "tobject.h"
#include "ptr.h"

#include "tk/tcl_interp.h"
#include "tk/gui/tk_button.h"
#include "tk/gui/tk_toplevel.h"
#include "tk/gui/tk_menu.h"
#include "tk/gui/tk_entry.h"
#include "tk/gui/tk_place_layout.h"
#include "tk/gui/options/enums/tk_enumoption.h"

#include "tk/gui/image/tk_photo.h"

#include "designer/designer.h"

#include "exceptions/exceptionbase.h"

#include "modulemanager/help.h"
#include "modulemanager/modulemanager.h"

#include "boom/boom.h"

#include "testsuite/testadd.h"
#include "testsuite/testcompare.h"

#include <iostream>

using namespace std;

ModuleType* types[]=
{
	new BoomLoader(),
	new TestAddLoader(),
	new TestCompareLoader(),
	
	0
};

Ptr<ModuleManager> manager;
Ptr<Designer> designer;
Ptr<TkGui> gui;


int Tcl_AppInit(Tcl_Interp *interp);

int main(int argc, char * argv[])
{
	try
	{
		Tk_Main(argc, argv, Tcl_AppInit);
	}
	catch(ExceptionBase* e)
	{
		cout<<e->stackTrace();
		return -1;
	}
	return 0;
}

int Tcl_AppInit(Tcl_Interp *interp) 
{
	// inicializuju shell
	if (Tcl_Init(interp) == TCL_ERROR) 
	{
		return TCL_ERROR;
	}
	// inicializuju grafiku
	if (Tk_Init(interp) == TCL_ERROR) 
	{
		return TCL_ERROR;
	}
	
	try
	{
		Ptr<TclInterp> minterp=new TclInterp(interp,"log.txt");
		TclInterp::defaultInterp=minterp;

		gui=new TkGui(minterp);
		gui->mainWindow->setWindowState(TkWinStateOption::withdrawn);

		manager=new ModuleManager(types,gui);
		ModuleManager::manager=manager;

		TXmlObject::registerAll();

		gui->load();

		designer=new Designer(minterp,gui,manager);
		
	}
	catch(ExceptionBase* e)
	{
		cout<<e->stackTrace()<<endl;
	}

	return TCL_OK;
}
