#include "microdot.h"

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

#define BE_OK 1
#define BE_CANCEL 2

#define BE_NAME 3
#define BE_BET 4
#define BE_HOLD 5
#define BE_NOCRUNCH 6
#define BE_DISKIN 7
#define BE_DISKINSEL 8
#define BE_ORIGIN 9
#define BE_EDITORIGIN 10
#define BE_PFAD 11
#define BE_PFADSEL 12

#define BE_LANG 13

#define BE_PM_HEAD 15
#define BE_PM_MASK 16
#define BE_ANTWORTAN 17

static char *origin_options[] = 
   {
      "Normal",
      "Keiner",
      "Spezial",
      NULL
   };

static char *langopts[ 8 ];

static char **initlangopts( void )
{
	char *p;
	int c;

	for( c = 0; c < 7; c++ )
	{
		p = &prefs.langnames[ c * 30 ];

		if( !*p )
			sprintf( p, "Sprache %ld", c + 1 );

		langopts[ c ] = p;
	}
	return( langopts );
}

extern void makebrettnamenoup( char * );
int dobrettprefs( struct mbrett *mb, int new, char *title )
{
	struct ogwin *ogw;
	struct Window *iw;
	struct IntuiMessage *im;
	struct brettinfo *bi = getbrettinfo( mb );
	char *p;
	int Done = FALSE;
	int ispm = mb->b.flags & BFLAG_EXTEND_PM;

	ogw = ogreInitWindow( scr, 0, 0, title );

	ogreAddGroup( ogw, 0, OGFRAME_OUTLINE, " Allgemein " );

	ogreAddString( ogw, 0, 'n', "Brett_name:", BE_NAME, mb->b.name, 40, 80 );
	ogreAddString( ogw, 1, 'b',   " _Betreff:", BE_BET, mb->b.betreff, 40, 32 );

	ogreAddGroup( ogw, 1, OGFRAME_OUTLINE, " Halten " );
	ogreAddInteger( ogw, 0, 'h', "_Haltezeit in Tagen:", BE_HOLD, mb->b.days, 0, MAXINT, 8, 8 );
	ogreAddCheckbox( ogw, 0, 'l', "Nie _löschen?", BE_NOCRUNCH, mb->b.flags & BFLAG_NOCRUNCH );

	ogreAddGroup( ogw, 2, OGFRAME_OUTLINE, " Sonstiges " );
	ogreAddString( ogw, 0, 'p', " Auslager_pfad:", BE_PFAD, bi->autoexpfad, 40, 128 );
	ogreAddButton( ogw, 0 | OGGP_KEEPSIZE, 'P', "?", BE_PFADSEL );
	ogreAddCycle( ogw, 1, 'r',  "       O_rigin:", BE_ORIGIN, origin_options, 8, ( mb->b.flags & BFLAG_SPECIALORIGIN ) ? 2 : ( ( mb->b.flags & BFLAG_NOORIGIN ) ? 1 : 0 ) );
	ogreAddButton( ogw, 1, 'e', "Origin _Editieren", BE_EDITORIGIN );
	ogreAddCycle( ogw, 2, 's',  "      _Sprache:", BE_LANG, initlangopts(), 8, mb->b.language );
	ogreAddString( ogw, 3, 'd', "_Diskussion-In:", BE_DISKIN, bi->diskussionin, 40, 40 );
	ogreAddButton( ogw, 3 | OGGP_KEEPSIZE, 'D', "?", BE_DISKINSEL );

	ogreAddGroup( ogw, 3, OGFRAME_OUTLINE, " PM-Brett-Parameter " );
	ogreAddString( ogw, 0, 'v', "_Vergleichs-Header:", BE_PM_HEAD, bi->expm_header, 40, 32 );
	ogreAddString( ogw, 1, 'i', "Vergleichs-_Inhalt:", BE_PM_MASK, bi->expm_data, 40, 128 );
	ogreAddString( ogw, 2, 'f', "Emp_fänger:", BE_ANTWORTAN, bi->antwortan, 40, 128 );

	ogreAddGroup( ogw, 4, OGFRAME_NONE, NULL );
	ogreAddButton( ogw, 0 | OGB_ONENTER, 'o', "_OK", BE_OK );
	ogreAddButton( ogw, 0, 0x5f, "Hilfe", OGID_HELP );
	ogreAddButton( ogw, 0 | OGB_ONESC, 'a', "_Abbruch", BE_CANCEL );

	if( !new )
		ogreEnable( ogw, FALSE, BE_NAME, 0 );

	if( !ispm )
		ogreEnable( ogw, FALSE, BE_PM_HEAD, BE_PM_MASK, BE_ANTWORTAN, 0 );

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

	while( !Done )
	{

		ogreEnable( ogw, ogreValue( ogw, BE_ORIGIN ) == 2, BE_EDITORIGIN, 0 );
		ogreEnable( ogw, !ogreValue( ogw, BE_NOCRUNCH ), BE_HOLD, 0 );

		im = ogreWaitIM( ogw );

		if( im->Class == IDCMP_GADGETHELP)
			showguidenum( "brett_gads", im->Code );
		else if( im->Class == IDCMP_GADGETUP )
		{
			switch( im->Code )
			{
				case BE_OK:
					p = ogreStringValue( ogw, BE_NAME );
					if( p && *p )
						Done = 2;
					else
						askreq( "Ungültiger Brettname.", "Weiter" );
					break;
				case BE_CANCEL:
					Done = 1;
					break;

				case BE_PFADSEL:
					newdofrseldir( ogw, BE_PFAD, "Pfad für Auslagerung..." );
					break;

				case BE_DISKINSEL:
					p = dobrettreq();
					if( p )
						ogreSetStringValue( ogw, BE_DISKIN, p );
					break;

				case BE_EDITORIGIN:
					calleditor( "T:mdtemp.origin" );
					break;
			}
		}
		ogreIMReply( ogw, im );
	}

	if( Done == 2 )
	{
		struct brettinfo binfo;
		int c;

		ogreCopyStringValue( ogw, BE_DISKIN, binfo.diskussionin );
		if( !strchr( binfo.diskussionin, '@' ) )
			makebrettname( binfo.diskussionin );
		ogreCopyStringValue( ogw, BE_PFAD, binfo.autoexpfad );
		makedospath( binfo.autoexpfad );

		ogreCopyStringValue( ogw, BE_NAME, mb->b.name );
		if( new )
		{
			if( ispm )
				makebrettnamenoup( mb->b.name );
			else
				makebrettname( mb->b.name );
		}

		ogreCopyStringValue( ogw, BE_BET, mb->b.betreff );

		ogreCopyStringValue( ogw, BE_PM_HEAD, binfo.expm_header );
		ogreCopyStringValue( ogw, BE_PM_MASK, binfo.expm_data );
		ogreCopyStringValue( ogw, BE_ANTWORTAN, binfo.antwortan );

		mb->b.days = ogreValue( ogw, BE_HOLD );

		/* Flags initialisieren */
		mb->b.flags = ispm ? BFLAG_EXTEND_PM : 0;

		if( ogreValue( ogw, BE_NOCRUNCH ) )
			mb->b.flags |= BFLAG_NOCRUNCH;

		c = ogreValue( ogw, BE_ORIGIN );
		if( c == 1 )
			mb->b.flags |= BFLAG_NOORIGIN;
		else if( c == 2 )
			mb->b.flags |= BFLAG_SPECIALORIGIN;

		mb->b.language = ogreValue( ogw, BE_LANG );

		setbrettinfo( mb, &binfo );

		makembrettlastwrite( mb );

	}

	ogreExitWindow( ogw );

	return( Done == 2 );

}
