#include "microdot.h"

#include "ogre.h"
#include "ogre_protos.h"

#define SMI_OK 1
#define SMI_CANCEL 2
#define SMI_SMON 3

#define SMI_PREIMPORT 4
#define SMI_IMPORTFILE 5
#define SMI_IMPORTFILESEL 6
#define SMI_POSTIMPORT 7

#define SMI_PREEXPORT 8
#define SMI_EXPORTFILE 9
#define SMI_EXPORTFILESEL 10
#define SMI_POSTEXPORT 11

#define SMI_INIT 13

#define SMI_COPYBUF 12

void dosysopmodereq( void )
{
	struct ogwin *ogw;
	struct Window *iw;
	struct IntuiMessage *im;
	int Done = FALSE;

	ogw = ogreInitWindow( scr, 0, 0, "Sysop-Modus" );

	ogreAddGroup( ogw, 0, OGFRAME_OUTLINE, " Allgemein " );
	ogreAddCheckbox( ogw, 0, 's', "_Sysop-Modus aktiv?", SMI_SMON, prefs.flags & MDF_SYSOPMODE );
	ogreAddInteger( ogw, 0, 'b', "_Buffergröße für Import/Export:", SMI_COPYBUF, prefs.sm_copybuf, 1, MAXINT, 10, 16 );
	ogreAddString( ogw, 1, 0,   "Initialisierung:", SMI_INIT, prefs.sm_init, 50, 128 );

	ogreAddGroup( ogw, 1 | OGGP_EXPANDSTRING, OGFRAME_OUTLINE, " Export " );
	ogreAddString( ogw, 0, 0,   " Vorher aufrufen:", SMI_PREEXPORT, prefs.sm_exportbefore, 20, 128 );
	ogreAddString( ogw, 1, 'e', "     _Exportdatei:", SMI_EXPORTFILE, prefs.sm_exportfile, 20, 128 );
	ogreAddButton( ogw, 1 | OGGP_KEEPSIZE, 'E', "?", SMI_EXPORTFILESEL );
	ogreAddString( ogw, 2, 0,   "Nachher aufrufen:", SMI_POSTEXPORT, prefs.sm_exportafter, 20, 128 );

	ogreAddGroup( ogw, 2 | OGGP_EXPANDSTRING, OGFRAME_OUTLINE, " Import " );
	ogreAddString( ogw, 0, 0,   " Vorher aufrufen:", SMI_PREIMPORT, prefs.sm_importbefore, 20, 128 );
	ogreAddString( ogw, 1, 'i', "     _Importdatei:", SMI_IMPORTFILE, prefs.sm_importfile, 20, 128 );
	ogreAddButton( ogw, 1 | OGGP_KEEPSIZE, 'I', "?", SMI_IMPORTFILESEL );
	ogreAddString( ogw, 2, 0,   "Nachher aufrufen:", SMI_POSTIMPORT, prefs.sm_importafter, 20, 128 );

	ogreAddGroup( ogw, 3, OGFRAME_NONE, NULL );
	ogreAddButton( ogw, 0 | OGB_ONENTER, 'o', "_Ok", SMI_OK );
	ogreAddHelp( ogw, 0 );
	ogreAddButton( ogw, 0 | OGB_ONESC, 'a', "_Abbruch", SMI_CANCEL );

	ogreEnable( ogw, prefs.flags & MDF_SYSOPMODE,
		SMI_INIT,
		SMI_PREEXPORT, SMI_POSTEXPORT, SMI_EXPORTFILE, SMI_EXPORTFILESEL,
		SMI_PREIMPORT, SMI_POSTIMPORT, SMI_IMPORTFILE, SMI_IMPORTFILESEL,
		SMI_COPYBUF, 0
	);

	iw = ogreOpenWindow( ogw );
	if( !iw )
		return;

	while( !Done )
	{
		im = ogreWaitIM( ogw );

		if( im->Class == IDCMP_GADGETHELP)
			showguidenum( "sysopmodewin_gads", im->Code );
		else if( im->Class == IDCMP_GADGETUP )
		{
			switch( im->Code )
			{
				case SMI_OK:
					Done = 2;
					break;

				case SMI_CANCEL:
					Done = 1;
					break;

				case SMI_IMPORTFILESEL:
					newdofrselfile( ogw, SMI_IMPORTFILE, "Importdatei auswählen..." );
					break;

				case SMI_EXPORTFILESEL:
					newdofrselfile( ogw, SMI_EXPORTFILE, "Exportdatei auswählen..." );
					break;

			}
		}
		ogreIMReply( ogw, im );

		ogreEnable( ogw, ogreValue( ogw, SMI_SMON ),
			SMI_INIT,
			SMI_PREEXPORT, SMI_POSTEXPORT, SMI_EXPORTFILE, SMI_EXPORTFILESEL,
			SMI_PREIMPORT, SMI_POSTIMPORT, SMI_IMPORTFILE, SMI_IMPORTFILESEL,
			SMI_COPYBUF, 0
		);
	}

	if( Done == 2 )
	{
		if( ogreValue( ogw, SMI_SMON ) )
		{
			prefs.flags |= MDF_SYSOPMODE;
			prefs.sm_copybuf = ogreValue( ogw, SMI_COPYBUF );
			ogreCopyStringValue( ogw, SMI_PREEXPORT, prefs.sm_exportbefore );
			ogreCopyStringValue( ogw, SMI_POSTEXPORT, prefs.sm_exportafter );
			ogreCopyStringValue( ogw, SMI_EXPORTFILE, prefs.sm_exportfile );
			ogreCopyStringValue( ogw, SMI_PREIMPORT, prefs.sm_importbefore );
			ogreCopyStringValue( ogw, SMI_POSTIMPORT, prefs.sm_importafter );
			ogreCopyStringValue( ogw, SMI_IMPORTFILE, prefs.sm_importfile );
			ogreCopyStringValue( ogw, SMI_INIT, prefs.sm_init );
		}
		else
		{
			prefs.flags &= ~MDF_SYSOPMODE;
		}
	}

	ogreExitWindow( ogw );

}
