#include "microdot.h"

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

static char *doeb_options[] = 
{
   "Immer",
   "Nachfragen",
   "Nie",
   NULL
};

int donetcfg( void )
{
	struct IntuiMessage *im;
	struct ogwin *ogw;
	struct Window *w;
	int Done = FALSE;
	char wz[32], sz[32];
	static char *ctrlfilteropts[] = { "Nein", "Alle", "Nur Cancel", NULL };

	sprintf( wz, "%ld.%ld", prefs.wz_fd, prefs.wz_fm );
	sprintf( sz, "%ld.%ld", prefs.sz_fd, prefs.sz_fm );

	ogw = ogreInitWindow( scr, WFLG_RMBTRAP, NULL, "Netz-Konfiguration" );
	ogreAddGroup( ogw, 0, OGFRAME_OUTLINE, " Zusätzliche Header " );
	ogreAddString( ogw, 0, 'r', "_Realname:", 55, prefs.userrealname, 42, 40 );
	ogreAddString( ogw, 1, 0, "Organisation:", 0, prefs.organisation, 42, 80 );
	ogreAddString( ogw, 2, 0, "Postanschrift:", 1, prefs.postanschrift, 42, 128 );
	ogreAddString( ogw, 3, 0, "Telefon:", 2, prefs.telefon, 42, 128 );

	ogreAddGroup( ogw, 1, OGFRAME_OUTLINE, " Zeitzonen " );
	ogreAddString( ogw, 0, 'f', "Winterzeit An_fang", 5, wz, 6, 6 );
	ogreAddInteger( ogw, 0, 0, "Abweichung zur GMT in Stunden", 6, prefs.wz_offs, -24, +24, 4, 3 );
	ogreAddString( ogw, 1, 0, "Sommerzeit Anfang", 7, sz, 6, 6 );
	ogreAddInteger( ogw, 1, 0, "Abweichung zur GMT in Stunden", 8, prefs.sz_offs, -24, +24, 4, 3 );

	ogreAddGroup( ogw, 2, OGFRAME_OUTLINE, " Empfangsbestätigungen " );
	ogreAddCycle( ogw, 0, 'v', "_Verschicken:", 4, doeb_options, 12, prefs.ebmode & 3 );
	ogreAddCheckbox( ogw, 0, '0', "Uhrzeit immer _00:00:00", 3, prefs.ebmode & EBMODE_00 );

	ogreAddGroup( ogw, 3, OGFRAME_OUTLINE, " Sonstiges " );
	ogreAddCycle( ogw, 0, 'c', "RFC-_Control-Nachrichten filtern:", 13, ctrlfilteropts, 10, prefs.rfc_control_filter );

	ogreAddGroup( ogw, 4, OGFRAME_NONE, NULL );
	ogreAddButton( ogw, 0 | OGB_ONENTER, 'o', "_OK", 10 );
	ogreAddHelp( ogw, 0 );
	ogreAddButton( ogw, 0 | OGB_ONESC, 'a', "_Abbruch", 11 );

	w = ogreOpenWindow( ogw );
	if( !w )
		return( 0 );

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

		if( im->Class == IDCMP_GADGETHELP )
			showguidenum( "netcfg_gads", im->Code );
		else if( im->Class == IDCMP_GADGETUP )
		{
			if( im->Code == 10 )
				Done = 1;
			else if( im->Code == 11 )
				Done = 2;
		}

		ogreIMReply( ogw, im );
	}

	/* Speichern */
	if( Done == 1 )
	{
		char *p;

		ogreCopyStringValue( ogw, 0, prefs.organisation );
		ogreCopyStringValue( ogw, 1, prefs.postanschrift );
		ogreCopyStringValue( ogw, 2, prefs.telefon );
		ogreCopyStringValue( ogw, 55, prefs.userrealname );

		prefs.rfc_control_filter = ogreValue( ogw, 13 );

		/* EB's verschicken */
		prefs.ebmode = ogreValue( ogw, 4 );
		if( ogreValue( ogw, 3 ) )
			prefs.ebmode |= EBMODE_00;

		prefs.sz_offs = ogreValue( ogw, 8 );
		prefs.wz_offs = ogreValue( ogw, 6 );

		p = ogreStringValue( ogw, 7 );
		p = stpblk( p );
		prefs.sz_fd = atoi( p );
		if( prefs.sz_fd < 1 || prefs.sz_fd > 31)
			prefs.sz_fd = 1;
		while( isdigit( *p++ ) );
		prefs.sz_fm = atoi( p );
		if( prefs.sz_fm < 1 || prefs.sz_fm > 12)
			prefs.sz_fm = 1;

		p = ogreStringValue( ogw, 5 );
		p = stpblk( p );
		prefs.wz_fd = atoi( p );
		if( prefs.wz_fd < 1 || prefs.wz_fd > 31)
			prefs.wz_fd = 1;
		while( isdigit( *p++ ) );
		prefs.wz_fm = atoi( p );
		if( prefs.wz_fm < 1 || prefs.wz_fm > 12)
			prefs.wz_fm = 1;

		mytzset();

	}

	ogreExitWindow( ogw );
}
