// LSettingsWindow.cpp
// ```````````````````

#include "stdafx.h"
#include "LSettingsWindow.h"
#include "res/resource.h"

LSettingsWindow::LSettingsWindow() :
MGWindow((void*)IDI_LILLYMAIN, _T("Settings"))
{
	this->setResizable(false);
	this->resize(400, 400);

	lbOptions = new MGListBox(this, false, false);
	lbOptions->resize(7, 7, 100, this->getInnerHeight() - 14);
	lbOptions->addItem(_T("Setup"));
	lbOptions->addItem(_T("Media"));
	lbOptions->addItem(_T("Hotkeys"));
	lbOptions->addItem(_T("Device"));
	lbOptions->onChange.assign(this, MGCB(LSettingsWindow::onListChanged));
	lbOptions->setSelected(0);

	grSettings = new MGGroup(this);
	grSettings->resize(114, 7, this->getInnerWidth() - 114-7, this->getInnerHeight() -14);
	
	// Hotkeys tab

	lbHotkeys = new MGListBox(this, false, false);
	lbHotkeys->resize(125, 28, 250, 100);
	lbHotkeys->addItem(_T("Next Song \tAlt + Shift + S"));
	lbHotkeys->addItem(_T("Play/Pause \tAlt + Shift + D"));
	lbHotkeys->onChange.assign(this, MGCB(LSettingsWindow::onHotkeySelected));
	hcKeyinput = new MGHotkeyControl(this);
	hcKeyinput->resize(125, 140, 250, 20);
	btSetHotkey = new MGButton(this, _T("Assign"));
	btSetHotkey->resize(125, 165, 80, 22); 

	// Device tab

	lDevices = new MGLabel(this, true);
	lDevices->resize(125, 28, 250, 20);
	lDevices->setText(_T("Output device:"));
	cbDevices = new MGComboBox(this, false);
	cbDevices->resize(125, 46, 250, 100);
	cbDevices->allowTextEntry(false);
	repopulateDevices();
	cbDevices->onChange.assign(this, MGCB(LSettingsWindow::onDeviceChanged));
	lSoundInfo = new MGLabel(this, true);
	lSoundInfo->resize(125, 80, 250, 100);
	repopulateSoundInfo();

	// Initial state
	onListChanged();

	// Showtime
	this->setVisible(true);
}

LSettingsWindow::~LSettingsWindow() {
	delete lbOptions;
	delete grSettings;
	delete lDevices;
	delete cbDevices;
	// TODO
}

void LSettingsWindow::onHotkeySelected() {
	MGKeyCombination tmp;
	tmp.virtkey = MGVK_S;
	tmp.alt = true;
	tmp.ctrl = true;
	tmp.shift = true;
	hcKeyinput->setValue(tmp);
}

void LSettingsWindow::repopulateDevices() {
	// Load all devices
	BASS_DEVICEINFO info;
	int device_idx = 0;
	for(;;) {
		if (! BASS_GetDeviceInfo(device_idx, &info)) break;
		TSTRING devName = TSTRINGify(info.name);
		cbDevices->addItem((TCHAR*) devName.c_str());
		device_idx++;
	}
	// Select the current device
	cbDevices->setSelected( BASS_GetDevice()  );
}

void LSettingsWindow::onDeviceChanged() {
	int curIdx = cbDevices->getSelected();
	//FIXME: SetDevice requires BASS_Init called on the old device first / invalidate all
	// current streams - LOW PRIORITY FEATURE
	//BASS_SetDevice( curIdx );
	repopulateSoundInfo();
}

void LSettingsWindow::repopulateSoundInfo() {
	BASS_INFO binf;
	BASS_GetInfo(&binf);

	TSTRING sinf = format(
		_T("Output properties:\n")
		_T("• Master Volume:\t%.1f %%\n")
		_T("• Frequency:\t%d Hz\n")
		_T("• Channels:\t%d\n")
		_T("• Latency:\t%d ms")
		, (BASS_GetVolume()*100), binf.freq, binf.speakers, binf.latency );
	lSoundInfo->setText((TCHAR*) sinf.c_str());
}

void LSettingsWindow::onListChanged() {
	// Hide all control elements
	lbHotkeys->setVisible(false);
	hcKeyinput->setVisible(false);
	btSetHotkey->setVisible(false);

	lDevices->setVisible(false);
	cbDevices->setVisible(false);
	lSoundInfo->setVisible(false);

	// Show relevant control elements
	switch(lbOptions->getSelected()) {
		case 0: { // Setup
			grSettings->setText(_T("Setup"));
		} break;
		case 1: { // Media
			grSettings->setText(_T("Media"));
		} break;
		case 2: { // Hotkeys
			grSettings->setText(_T("Hotkeys"));
			lbHotkeys->setVisible(true);
			hcKeyinput->setVisible(true);
			btSetHotkey->setVisible(true);
		} break;
		case 3: { // Device
			grSettings->setText(_T("Device"));
			lDevices->setVisible(true);
			cbDevices->setVisible(true);
			lSoundInfo->setVisible(true);
		} break;
	}
}
