#include "sysconfig.h"
#include "sysdeps.h"
#include "config.h"
#include "options.h"
#include "cfgfile.h"

int config_sound_style=0;
int config_sound_tweak=0;
int config_socket_emu=0;
int config_middle_mouse=0;
int config_map_drives=1;
int config_logfile=0;

char config_prtname[ MAX_PATH ] = "";
char config_sername[ MAX_PATH ] = "";
char config_file_path[ MAX_PATH ] = "";
struct cfg_lines extra_config_stuff[ extralastentry ] = {
    { "win32.file_path", "Base path which can be used as a variable in this file, like: $(FILE_PATH)\\disk_images", config_file_path },
    { "win32.parallel_port", "Parallel port name - typically LPT1", config_prtname },
    { "win32.serial_port", "Serial port name - typically COM1", config_sername },
    { "win32.sound_style", "Can be waveout_looping, waveout_dblbuff, dsound_looping, dsound_dblbuff", &config_sound_style },
    { "win32.sound_tweak", "For advanced users", &config_sound_tweak },
    { "win32.networking", "BSD Socket Library emulation", &config_socket_emu },
    { "win32.middle_mouse", "If yes, sends middle mouse-button events to Amiga.  If no, treats middle mouse-button presses as ALT-TAB\n", &config_middle_mouse },
    { "win32.map_drives", "If yes, maps all PC drive-letters to Amiga volumes at startup.", &config_map_drives },
    { "win32.logfile", "If yes, winuaelog.txt is generated.", &config_logfile }
};

int CFGFILE2_convert_option( extra_config_entries entry, char *param, int direction )
{
    int result = 1;
    int *iptr = (int *)extra_config_stuff[entry].config_var;
    char *strptr = (char *)iptr;

    switch( entry )
    {
        case sound_style:
            if( direction == 0 )
            {
                if( strcasecmp( param, "waveout_looping" ) == 0 )
                {
                    *iptr = 0;
                }
                else if( strcasecmp( param, "waveout_dblbuff" ) == 0 )
                {
                    *iptr = 1;
                }
                else if( strcasecmp( param, "dxsound_looping" ) == 0 )
                {
                    *iptr = 2;
                }
                else if( strcasecmp( param, "dxsound_dblbuff" ) == 0 )
                {
                    *iptr = 3;
                }
                else
                    result = 0;
            }
            else
            {
                switch( *iptr )
                {
                case 1:
                    strcpy( param, "waveout_dblbuff" );
                    break;
                case 2:
                    strcpy( param, "dxsound_looping" );
                    break;
                case 3:
                    strcpy( param, "dxsound_dblbuff" );
                    break;
                case 0:
                default:
                    strcpy( param, "waveout_looping" );
                    break;
                }
            }
        break;

        case sound_tweak:
            if( direction == 0 )
                sscanf( param, "%d", iptr );
            else
                sprintf( param, "%d", *iptr );
        break;

        case parallel_port:
        case serial_port:
        case file_path:
            if( direction == 0 )
            {
                if( strcasecmp( param, "none" ) == 0 )
                {
                    *strptr = '\0';
                    if( entry == serial_port )
                        config_prefs.use_serial = 0;
                }
                else
                {
                    strcpy( strptr, param );
                    if( entry == serial_port )
                        config_prefs.use_serial = 1;
                }
            }
            else
            {
                if( *strptr == '\0' )
                {
                    strcpy( param, "none" );
                }
                else
                {
                    strcpy( param, strptr );
                }
            }
        break;

        case map_drives:
        case socket_emu:
        case middle_mouse:
        case create_logfile:
            if( direction == 0 )
            {
                if( ( param[0] == 'y' ) ||
                    ( param[0] == 'Y' ) )
                {
                    *iptr = 1;
                }
                else
                {
                    *iptr = 0;
                }
            }
            else
            {
                if( *iptr == 0 )
                {
                    strcpy( param, "no" );
                }
                else
                {
                    strcpy( param, "yes" );
                }
            }
        break;

        default:
            result = 0;
        break;
    }

    return result;
}

int parse_config_line_machdep( char *part1, char *part2 )
{
    int result = 1;
    int i;
    char line[256] = "";

    for( i = 0; i < extralastentry; i++ )
    {
        if( strcasecmp( part1, extra_config_stuff[i].config_label ) == 0 )
        {
            if( !CFGFILE2_convert_option( i, part2, 0 ) )
            {
                write_log( "Failure converting option %s with param %s, using default setting for that option instead.\n", part1, part2 );
            }
            else
            {
                break;
            }
        }
    }
    if( i == extralastentry )
    {
        result = 0;
    }
    return result;
}

void save_config_lines_machdep( FILE *fh )
{
    int i;
    char buffer[32];

    for( i = 0; i < extralastentry; i++ )
    {
        fprintf( fh, "%s=", extra_config_stuff[i].config_label );
        CFGFILE2_convert_option( i, buffer, 1 );
        fprintf( fh, "%s\n", buffer );
    }
}

// If direction is zero, then we've got a set of config_prefs and want them to be our currprefs
// If direction is one, then we've got some currprefs, and we want them to be our config_prefs
void CFGFILE2_transfer_params( struct uae_prefs *to, struct uae_prefs *from, int direction )
{
    extern int win32_sound_style;
    extern int win32_sound_tweak;
    extern int win32_socket_emu;
    extern int win32_middle_mouse;

    if( direction == 0 )
    {
        win32_sound_style  = config_sound_style;
        win32_sound_tweak  = config_sound_tweak;
        win32_socket_emu   = config_socket_emu;
        win32_middle_mouse = config_middle_mouse;
    }
    else
    {
        config_sound_style  = win32_sound_style;
        config_sound_tweak  = win32_sound_tweak;
        config_socket_emu   = win32_socket_emu;
        config_middle_mouse = win32_middle_mouse;
    }
}
