#include "defines.h"
#include "i18n.h"
#include "setup.h"
#include <st.h>

cSoundtestSetup SoundtestSetup;

int freqitemoffset;
int lastcurrent;
const char * dirfilenamechars = NULL;
int filetypes;
const char *BarItems[3];

// --- cSoundtestSetup ----------------------------------------------------------

cSoundtestSetup::cSoundtestSetup(void) {
	hidemenu = 0;
	alpha = 0x7F;
	linear = 0;
	screen = 0;
	samplerate = 44100;
	size = 1;
	channels = 2;
	byteswap = 0;
	char *s;
	commandlinedevice = false;
    	asprintf(&s, "%s/:_", tr(FileNameChars));
	dirfilenamechars = strdup(s);
	free(s);
	filetype = 0;
	filetypes = 0;
	strncpy( filetypename, "ossdsp", sizeof(filetypename));
        for (int i = 0; st_formats[i].names != NULL; i++) {
    	    for (int j = 0; st_formats[i].names[j] != NULL; j++) {
        	filetypeItems[filetypes] = st_formats[i].names[j];
		if (strcmp(st_formats[i].names[j], filetypename) == 0) {
		    filetype = filetypes;
		    }
		filetypes++;
		}
    	    }
	BarItems[0] = tr("Bars");
	BarItems[1] = tr("Lines");
	BarItems[2] = tr("Points");
	BarItems[3] = tr("Bars delayed");
	BarItems[4] = tr("Lines delayed");
	BarItems[5] = tr("Points delayed");
	strncpy( device, "/dev/dsp", sizeof(device));
}

cSoundtestSetup::~cSoundtestSetup() {
}

bool cSoundtestSetup::SetupParse(const char *Name, const char *Value) {
    if (strcmp(Name, "hidemenu") == 0) hidemenu = atoi(Value);
    else if (strcmp(Name, "alpha") == 0) alpha = atoi(Value);
    else if (strcmp(Name, "linear") == 0) linear = atoi(Value);
    else if (strcmp(Name, "screen") == 0) screen = atoi(Value);
    else if (strcmp(Name, "bars") == 0) bars = atoi(Value);
    else if (strcmp(Name, "samplerate") == 0) samplerate = atoi(Value);
    else if (strcmp(Name, "returnsamplerate") == 0) returnsamplerate = atoi(Value);
    else if (strcmp(Name, "size") == 0) size = atoi(Value);
    else if (strcmp(Name, "returnsize") == 0) returnsize = atoi(Value);
    else if (strcmp(Name, "channels") == 0) channels = atoi(Value);
    else if (strcmp(Name, "returnchannels") == 0) returnchannels = atoi(Value);
    else if (strcmp(Name, "byteswap") == 0) byteswap = atoi(Value);
    else if (!strcasecmp(Name, "device") && !commandlinedevice) strn0cpy(device, Value , sizeof(device));
    else if (strcmp(Name, "filetypename") == 0) {
    	strn0cpy(filetypename, Value , sizeof(filetypename));
	if (strcmp(filetypeItems[filetype], filetypename) != 0) {
    	    for (int i = 0; i < filetypes; i++) {
		if (strcmp(filetypeItems[i], filetypename) == 0) {
		    filetype = i;
		    }
    		}
	    }
	}
    else return false;
    return true;
}

// --- cSoundtestSetupPage ------------------------------------------------------

void cSoundtestSetupPage::ShowItems(void) {
    freqitemoffset = 0;
    Add(new cMenuEditBoolItem(tr("Hide menu entry"), &mySoundtestSetup.hidemenu, tr("no"), tr("yes")));
    Add(new cMenuEditIntItem(tr("Backgrund alpha"), &mySoundtestSetup.alpha, 0, 255));
    Add(new cMenuEditBoolItem(tr("Screen"), &mySoundtestSetup.screen, tr("Samples and spectrum"), tr("Spectrum only")));
    Add(new cMenuEditStraItem(tr("Spectrum show as"), &mySoundtestSetup.bars, 6, BarItems));
    Add(new cMenuEditBoolItem(tr("Linear"), &mySoundtestSetup.linear, tr("no"), tr("yes")));
    Add(new cMenuEditIntItem(tr("Samplerate"), &mySoundtestSetup.samplerate, 1000, MAXSAMPLERATE));
    freqitemoffset++;
    char *s;
    cOsdItem *item;
    if (mySoundtestSetup.returnsamplerate != mySoundtestSetup.samplerate) {
	asprintf(&s, "  %s %s: %d", tr("Samplerate"), tr("return"), mySoundtestSetup.returnsamplerate);
	item = new cOsdItem(s, osUser9);
	item->SetSelectable(false);
	Add(item);
	freqitemoffset++;
	}
    Add(new cMenuEditIntItem(tr("Data-size [byte]"), &mySoundtestSetup.size, 1, 4));
    freqitemoffset++;
    if (mySoundtestSetup.size != mySoundtestSetup.returnsize) {
	asprintf(&s, "  %s %s: %d", tr("Data-size"), tr("return"), mySoundtestSetup.returnsize);
	item = new cOsdItem(s, osUser9);
	item->SetSelectable(false);
	Add(item);
	freqitemoffset++;
	}
    Add(new cMenuEditIntItem(tr("Channels"), &mySoundtestSetup.channels, 1, 8));
    freqitemoffset++;
    if (mySoundtestSetup.channels != mySoundtestSetup.returnchannels) {
	asprintf(&s, "  %s %s: %d", tr("Channels"), tr("return"), mySoundtestSetup.returnchannels);
	item = new cOsdItem(s, osUser9);
	item->SetSelectable(false);
	Add(item);
	freqitemoffset++;
	}
    Add(new cMenuEditBoolItem(tr("Swap Data"), &mySoundtestSetup.byteswap, tr("Byte"), tr("Word")));
    freqitemoffset++;
    if (mySoundtestSetup.commandlinedevice) {
	asprintf(&s, "  Device: '%s'", mySoundtestSetup.device);
	item = new cOsdItem(s, osUser9);
	item->SetSelectable(false);
	Add(item);
	}
    else
	Add(new cMenuEditStrItem (tr("Device"), mySoundtestSetup.device, sizeof(mySoundtestSetup.device), dirfilenamechars));
    freqitemoffset++;
    if (filetypes > 0) {
	Add(new cMenuEditStraItem(tr("Filetype"), &mySoundtestSetup.filetype, filetypes, mySoundtestSetup.filetypeItems));
	freqitemoffset++;
	}
    asprintf(&s, " %s: %d - %d Hz", tr("Frequencies"), mySoundtestSetup.samplerate / SAMPLES, mySoundtestSetup.samplerate / 2 );
    item = new cOsdItem(s, osUser9);
    item->SetSelectable(false);
    Add(item);
    free(s);
}

cSoundtestSetupPage::cSoundtestSetupPage(void) {
    mySoundtestSetup = SoundtestSetup;
    ShowItems();
}

eOSState cSoundtestSetupPage::ProcessKey(eKeys Key) {
    int oldsamplerate = mySoundtestSetup.samplerate;
    int oldfiletype = mySoundtestSetup.filetype;
    lastcurrent = Current();
    
    eOSState state = cMenuSetupPage::ProcessKey(Key);

    if (oldfiletype != mySoundtestSetup.filetype) {
	strncpy( mySoundtestSetup.filetypename, mySoundtestSetup.filetypeItems[mySoundtestSetup.filetype] , sizeof(mySoundtestSetup.filetypename));
	}
    if (oldsamplerate != mySoundtestSetup.samplerate) {
	cOsdItem *item = Get(lastcurrent + freqitemoffset);
	cList<cOsdItem>::Del(item, false);
	char *s;
	asprintf(&s, " %s: %d - %d Hz", tr("Frequencies"), mySoundtestSetup.samplerate / SAMPLES, mySoundtestSetup.samplerate / 2 );
	item = new cOsdItem(s, osUser9);
	free(s);
	item->SetSelectable(false);
	Add(item);
	Display();
	}
    return state;
}

cSoundtestSetupPage::~cSoundtestSetupPage() {
}

void cSoundtestSetupPage::Store(void) {
    SoundtestSetup = mySoundtestSetup;
    SetupStore("hidemenu", SoundtestSetup.hidemenu);
    SetupStore("alpha", SoundtestSetup.alpha);
    SetupStore("screen", SoundtestSetup.screen);
    SetupStore("bars", SoundtestSetup.bars);
    SetupStore("linear", SoundtestSetup.linear);
    SetupStore("samplerate", SoundtestSetup.samplerate);
    SetupStore("returnsamplerate", SoundtestSetup.returnsamplerate);
    SetupStore("size", SoundtestSetup.size);
    SetupStore("returnsize", SoundtestSetup.returnsize);
    SetupStore("channels", SoundtestSetup.channels);
    SetupStore("returnchannels", SoundtestSetup.returnchannels);
    SetupStore("byteswap", SoundtestSetup.byteswap);
    SetupStore("device", SoundtestSetup.device);
    SetupStore("filetypename", SoundtestSetup.filetypename);
}
