/*
 * Campaign creation stuff here
 */
#include "main.h"

#ifdef LOG
#include <xlog.h>
#endif

CAMP *cc;

BOOL _stdcall AboutDlg(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
    {
		case WM_CLOSE:
			EndDialog(hwnd,0);
			return 1;

		case WM_COMMAND:
			switch (LOWORD(wParam))
            {
				case IDOK:
					EndDialog(hwnd,1);
					return 1;
			}
			break;
	}
	return 0;
}

BOOL _stdcall DiceDlg(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    BOOL o, t;
    char output[MSL*3];
    char temp[MIL];
    char dice[MSL*3];
    unsigned int size = 20, rolls = 1, result = 0;
    unsigned int i = 0;

	switch(msg)
    {
		case WM_CLOSE:
			EndDialog(hwnd,0);
			return 1;

		case WM_COMMAND:
			switch (LOWORD(wParam))
            {
                case 105: /* roll! */
                    rolls = GetDlgItemInt(hwnd, 102, &o, 0);
                    size =  GetDlgItemInt(hwnd, 104, &t, 0);

                    if ( o == FALSE )
                     rolls = 1;

                    if ( t == FALSE )
                     size = 20;

                    if ( rolls > 1000 )
                    {
                      MessageBox(hwnd, "You can only roll 1000 dice!", "Too Many Dice!", MB_OK );
                      break;
                    }

                    if ( size > 1000 )
                    {
                       MessageBox(hwnd, "A 1000 sided die is the largest!", "Too Big of Dice!", MB_OK );
                       break;
                    }

                    sprintf( output, "Rolling %dd%d\r\n---------------------------\r\n", rolls, size );

                    for ( i = 1; i <= rolls; i++ )
                    {
                        int b = 0;
                        b = roll_dice( 1, size );

                        sprintf( temp, "\r\nRolled: %d", b );
                        strcat( dice, temp );

                        result += b;
                    }

                    sprintf( temp, "\r\nTotal: %d\r\nAverage: %d", result, result/rolls );
                    strcat( output, temp );
                    strcat( output, "\r\n" );
                    strcat( output, dice );

#ifdef LOG_DICE
                    L( "DiceDlg", "Rolls = %d, Size = %d, Output = %d, Dice = %d, Total = %d", rolls, size, strlen(output)+1, strlen(dice)+1, result );
#endif

                    SetDlgItemText( hwnd, 107, output );

                    temp[0] = '\0';
                    output[0] = '\0';
                    dice[0] = '\0'; /* reset strings */

                    break;

				case IDOK:
					EndDialog(hwnd,1);
					return 1;
			}
			break;
	}
	return 0;
}

/*
 * Dlg window that handles creating a new campaign
 */
BOOL _stdcall NewDlg(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    char filename[MAX_PATH];
    char f[MAX_PATH];
    char author[MIL];
    char name[MIL];
    char world[MIL];

    short low, high;
    BOOL o, t;

    low = high = 1;

	switch(msg)
    {
		case WM_CLOSE:
			EndDialog(hwnd,0);
			return 1;

		case WM_COMMAND:
			switch (LOWORD(wParam))
            {
              case 300: /* cancel, eg close window */
					EndDialog(hwnd,1);
					return TRUE;
					
              case 301: /* reset form */
                   SetDlgItemText( hwnd, 150, "" );
                   SetDlgItemText( hwnd, 151, "" );
                   SetDlgItemText( hwnd, 152, "" );
                   SetDlgItemText( hwnd, 153, "" );

                   SetDlgItemInt ( hwnd, 200, 0, FALSE );
                   SetDlgItemInt ( hwnd, 201, 0, FALSE );
                   break;

              case IDOK: /* CREATE! */
                    if ( cc != NULL ) /* we already have a campaign?! */
                    {
                        char buf[64];
                        sprintf( buf, "There is already another open and active campaign! Would you like to close and save it to start another?" );
                        
                        if ( MessageBox(hwnd, buf, "Close and Save?", MB_YESNO ) == IDYES )
                        {
                          save_campaign(cc);
                          close_campaign(cc);
                          cc = NULL;
                        }
                        else /* we obviously want to keep the current one going, so end the dialogue */
                        {
                           EndDialog(hwnd,1);
                           return TRUE;
                        }
                    }
                    
                    /* strings */
                    if ( (GetDlgItemText( hwnd, 151, author, MIL ) == 0 ) )
                    {
                      MessageBox(hwnd, "Who is the author?", "No Data!", MB_OK);
                      break;
                    }

                    if ( (GetDlgItemText( hwnd, 152, name, MIL ) == 0 ) )
                    {
                      MessageBox(hwnd, "What is the name of the campaign?", "No Data!", MB_OK);
                      break;
                    }

                    if ( (GetDlgItemText( hwnd, 153, world, MIL ) == 0 ) )
                    {
                      MessageBox(hwnd, "In what world does this take place?", "No Data!", MB_OK);
                      break;
                    }

                    /* do file name last so we can base it off the name if not provided */
                    if ( (GetDlgItemText( hwnd, 150, filename, MIL ) == 0 ) )
                      sprintf( f, "save/%s.camp", name );
                    else
                      sprintf( f, "save/%s.camp", filename );


                    CREATE( cc, CAMP, 1 ); /* alloc mem for new campaign */

                    cc->filename  = strdup(f);
                    cc->author    = strdup(author);
                    cc->name      = strdup(name);
                    cc->world_name = strdup(world);

                    /* level range */
                    low =   GetDlgItemInt(hwnd, 200, &o, 0);
                    high =  GetDlgItemInt(hwnd, 201, &t, 0);

                    if ( o == FALSE )
                     low = 1;

                    if ( t == FALSE )
                     high = 2;

                    if ( low > 40 )
                     low = 40;
                     
                    if ( high > 40 )
                     high = 40;

                     
                    cc->level_range[0] = low;
                    cc->level_range[1] = high;

                    cc->modified = TRUE;

                    save_campaign(cc);
					EndDialog(hwnd,1);
					return TRUE;
			}
			break;
	}
	return FALSE;
}