#include "tk_dialogs.h"
#include "../tcl_guid.h"

const char* TkDialogs::mbox_types[]=
{
	"abortretrycancel",
	"ok",
	"okcancel",
	"retrycancel",
	"yesno",
	"yesnocancel",

	0
};

const char* TkDialogs::mbox_buttons[]=
{
	"abort",
	"retry",
	"cancel",
	"ok",
	"yes",
	"no",
    
	0
};

const char* TkDialogs::mbox_icons[]=
{
	"error"
	"info",
	"question",
	"warning",
	
	0
};



int TkDialogs::createDialog(Ptr<TclInterp>& interp,const string& title,const string& text,int defaultButton)
{
	list<string> args;
	
	return createDialog(interp,title,text,defaultButton,args);
}

int TkDialogs::createDialog(Ptr<TclInterp>& interp,const string& title,const string& text,int defaultButton,const string& button0)
{
	list<string> args;
	args.push_back(button0);

	return createDialog(interp,title,text,defaultButton,args);
}

int TkDialogs::createDialog(Ptr<TclInterp>& interp,const string& title,const string& text,int defaultButton,const string& button0,const string& button1)
{
	list<string> args;
	args.push_back(button0);
	args.push_back(button1);

	return createDialog(interp,title,text,defaultButton,args);
}

int TkDialogs::createDialog(Ptr<TclInterp>& interp,const string& title,const string& text,int defaultButton,const string& button0,const string& button1,const string& button2)
{
	list<string> args;
	args.push_back(button0);
	args.push_back(button1);
	args.push_back(button2);

	return createDialog(interp,title,text,defaultButton,args);
}

int TkDialogs::createDialog(Ptr<TclInterp>& interp,const string& title,const string& text,int defaultButton,const list<string>& buttons)
{
	TclGuid id;
	string cmd;

	cmd=string("tk_dialog .")+id.getId()+" \""+title+"\" \""+text+"\" {} ";
	
	char buf[128];
	sprintf(buf,"%d ",defaultButton);
	cmd.append(buf);

	for(list<string>::const_iterator it=buttons.begin();it!=buttons.end();it++)
	{
		cmd.append("\"");
		cmd.append(*it);
		cmd.append("\" ");
	}

	interp->eval(cmd);
	return interp->getIntResult();
}

MBoxButton TkDialogs::createMBox(Ptr<TclInterp>& interp,const string& title,const string& message,MBoxType type,MBoxIcon icon,MBoxButton defaultButton)
{
    string cmd;
	cmd=string("tk_messageBox -title \"")+title+"\" -message \""+message+"\" -type ";
	cmd.append(mbox_types[(int)type]);
	cmd.append(" -icon ");
	cmd.append(mbox_icons[(int)icon]);
	cmd.append(" -default ");
	cmd.append(mbox_buttons[(int)defaultButton]);
	
	interp->eval(cmd);

	const char* result=interp->getStrResult();
	if (strcmp(result,"abort")==0) return mbut_abort;
	if (strcmp(result,"retry")==0) return mbut_retry;
	if (strcmp(result,"cancel")==0) return mbut_cancel;
	if (strcmp(result,"ok")==0) return mbut_ok;
	if (strcmp(result,"yes")==0) return mbut_yes;
	if (strcmp(result,"no")==0) return mbut_no;

	return defaultButton;
}

const string& TkDialogs::createOpenDialog(Ptr<TclInterp>& interp)
{
	string cmd;
	cmd=string("tk_getOpenFile");
	interp->eval(cmd);
	
	return *(new string(interp->getStrResult()));
}

const string& TkDialogs::createOpenDialog(Ptr<TclInterp>& interp,const string& defaultExtension,const map<string,string>& fileTypes,const string& initialDir,bool multiple,const string& title)
{
	string cmd;
	cmd=string("tk_getOpenFile");

	cmd=cmd+" -defaultextension "+defaultExtension;
	cmd=cmd+" -filetypes {";
	string types;
	for(map<string,string>::const_iterator it=fileTypes.begin();it!=fileTypes.end();it++)
	{
		types=types+" { {"+it->first+"} {"+it->second+"} } ";
	}
	cmd=cmd+types+"}";
	if (initialDir!="")	cmd=cmd+" -initialdir \""+initialDir+"\"";
	cmd=cmd+" -title \""+title+"\"";

	interp->eval(cmd);

	return *(new string(interp->getStrResult()));
}

const string& TkDialogs::createSaveDialog(Ptr<TclInterp>& interp,const string& defaultExtension,const map<string,string>& fileTypes,const string& initialDir,const string& title)
{
	string cmd;
	cmd=string("tk_getSaveFile");
	cmd=cmd+" -defaultextension "+defaultExtension;
	cmd=cmd+" -filetypes {";
	string types;
	for(map<string,string>::const_iterator it=fileTypes.begin();it!=fileTypes.end();it++)
	{
		types=types+" { {"+it->first+"} {"+it->second+"} } ";
	}
	cmd=cmd+types+"}";
	if (initialDir!="")	cmd=cmd+" -initialdir \""+initialDir+"\"";
	cmd=cmd+" -title \""+title+"\"";

	interp->eval(cmd);

	return *(new string(interp->getStrResult()));
}
