/***************************************************************************
 * settings.cpp
 *
 * This file is part of KTabEdit, a KDE tabulature editor
 *
 * copyright (C) 2007- the KTabEdit development team <mrgwen@gmail.com>
 ***************************************************************************/

/***************************************************************************
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * See the file COPYING for more information.
 ***************************************************************************/

#include "settings.h"

#include "songview.h"

#include "util/assert.h"

const QString Settings::noteNames[9][12] = {
	{"C",  "C#", "D",  "D#", "E",  "F", "F#", "G",  "G#", "A",  "A#", "B"},
	{"C",  "Db", "D",  "Eb", "E",  "F", "Gb", "G",  "Ab", "A",  "Bb", "B"},
	{"C",  "Db", "D",  "Eb", "E",  "F", "F#", "G",  "G#", "A",  "Bb", "B"},
	{"C",  "C#", "D",  "D#", "E",  "F", "F#", "G",  "G#", "A",  "A#", "H"},
	{"C",  "Db", "D",  "Eb", "E",  "F", "Gb", "G",  "Ab", "A",  "Hb", "H"},
	{"C",  "Db", "D",  "Eb", "E",  "F", "F#", "G",  "G#", "A",  "Hb", "H"},
	{"C",  "C#", "D",  "D#", "E",  "F", "F#", "G",  "G#", "A",  "B" , "H"},
	{"C",  "Db", "D",  "Eb", "E",  "F", "Gb", "G",  "Ab", "A",  "B" , "H"},
	{"C",  "Db", "D",  "Eb", "E",  "F", "F#", "G",  "G#", "A",  "B" , "H"}
};

Settings* Settings::SThis = 0;

Settings* Settings::getInstance()
{
	if (SThis == 0) {
		SThis = new Settings();
		SThis->mSettings = new QSettings;
	}
	
	return SThis;
}
/*
void Settings::initConfig()
{
	PRECONDITION(SThis != 0);
	
	setCurrentGroup("General");
	mNoteNameConfig = addItemUInt("NoteNames", noteNamesArrayPos, 2);
	mMaj7Config = addItemUInt("Maj7", maj7, 0);
	mFlatPlusConfig = addItemUInt("FlatPlus", flatPlus, 0);
	mSharpPlusConfig = addItemUInt("SharpPlus", sharpPlus, 0);
	
	setCurrentGroup("MIDI");
	mMidiNameConfig = addItemString("Name", sMidiName, "");
	mMidiPortConfig = addItemString("Port", sMidiPort, "");
	mMidiPortNumberConfig = addItemUInt("PortNumber", sMidiPortNumber, 0);
	mMidiReadableConfig = addItemBool("Readable", sMidiReadable, false);
	mMidiWriteableConfig = addItemBool("Writeable", sMidiWriteable, false);
	mMidiTimidityConfig = addItemBool("Timidity", sMidiTimidity, false);
	
	setCurrentGroup("MelodyEditor");
	mWoodConfig = addItemUInt("Wood", sWood, 2);
	mInLayConfig = addItemUInt("InLay", sInLay, 1);
	for (int i = 0; i < 3; i++) {
		mEditorActionConfig[i] = addItemUInt(QString("Action%0").arg(i), sEditorAction[i], 0);
		mEditorAdvanceConfig[i] = addItemBool(QString("Advance%0").arg(i), sEditorAdvance[i], false);
	}
	
	setCurrentGroup("MusiXTeX");
	mTexTabSizeConfig = addItemUInt("TabSize", sTexTabSize, 2);
	mTexShowBarNumberConfig = addItemBool("ShowBarNumber", sTextShowBarNumber, true);
	mTexShowStrConfig = addItemBool("ShowStr", sTexShowStr, true);
	mTexShowPageNumberConfig = addItemBool("ShowPageNumber", sTexShowPageNumber, true);
	mTexExportModeConfig = addItemBool("ExportMode", sTexExportMode, 0);
	
	setCurrentGroup("Printing");
	mPrintingStyleConfig = addItemUInt("Style", sPrintingStyle, 0);
	
	setCurrentGroup("Timidity");
	mTimidityPathConfig = addItemString("Path", sTimidityPath, "/usr/bin/timidity");
	mTimidityBufferConfig = addItemUInt("Buffer", sTimidityBuffer, 2);
	mTimidityFragmentConfig = addItemUInt("Fragment", sTimidityFragment, 8);
	mTimidityOutputConfig = addItemString("Output", sTimidityOutput, "s");
	mTimidityChorusConfig = addItemString("Chorus", sTimidityChorus, "d");
	mTimidityDelayConfig = addItemString("Delay", sTimidityDelay, "d");
	mTimidityReverbConfig = addItemString("Reverb", sTimidityReverb, "d");
	mTimidityVlpfConfig = addItemString("Vlpf", sTimidityVlpf, "d");
	mTimidityNoiseConfig = addItemUInt("Noise", sTimidityNoise, 0);
	mTimidityResampConfig = addItemString("Resample", sTimidityResamp, "l");
	mTimidityFrequencyConfig = addItemUInt("Frequency", sTimidityFrequency, 44100);
	mTimidityRealTimeEnableConfig = addItemBool("RealTimeEnable", sTimidityRealTimeEnable, false);
	mTimidityRealTimeConfig = addItemUInt("RealTime", sTimidityRealTime, 50);
	
	setCurrentGroup("Assistant");
	mAssistantConfig = addItemBool("FirstTime", sAssistant, false);
	
	setCurrentGroup("Font");
	mTitleFontConfig = addItemFont("titleFont", sTitleFont, QFont("Bitstream Vera Sans", 50));
	mSubTitleFontConfig = addItemFont("subTitleFont", sSubTitleFont, QFont("Bitstream Vera Sans", 24));
	mTextAndTabFontConfig = addItemFont("textAndTabFont", sTextAndTabFont, QFont("Bitstream Vera Sans", 9));
	mTimeSignatureConfig = addItemFont("timeSignatureFont", sTimeSignatureFont, QFont("Bitstream Vera Sans", 14));
}
*/
QString Settings::noteName(int num)
{
	if (num < 0 || num > 11)
		return SongView::tr("Unknown");
	
	int option = mSettings->value("General/NoteNames", 2).toInt();
	
	if (option < 0 || option > 8)
		option = 2;

	return noteNames[option][num];
}

QString Settings::maj7Name()
{
	switch (mSettings->value("General/Maj7", 0).toInt()) {
	case 1: return "maj7";
	case 2: return "dom7";
	default: return "7M";
	}
}

QString Settings::flatName()
{
	if (mSettings->value("General/FlatPlus", 0).toInt() == 1)
		return "b";
	else
		return "-";
}

QString Settings::sharpName()
{
	if (mSettings->value("General/SharpPlus", 0).toInt() == 1)
		return "#";
	else
		return "+";
}

QString Settings::midiName()
{
	return mSettings->value("Midi/Name", "").toString();
}

QString Settings::midiPort()
{
	return mSettings->value("Midi/Port", "").toString();
}

int Settings::midiPortNumber()
{
	return mSettings->value("Midi/PortNumber", 0).toInt();
}

bool Settings::midiReadable()
{
	return mSettings->value("Midi/Readable", false).toBool();
}

bool Settings::midiWriteable()
{
	return mSettings->value("Midi/Writeable", false).toBool();
}

bool Settings::midiTimidity()
{
	return mSettings->value("Midi/Timidity", false).toBool();
}

int Settings::melodyEditorWood()
{
	return mSettings->value("MelodyEditor/Wood", 2).toInt();
}

int Settings::melodyEditorInlay()
{
	return mSettings->value("MelodyEditor/Inlay", 1).toInt();
}

int Settings::melodyEditorAction(int num)
{
	PRECONDITION(num < 3);
	
	return mSettings->value(QString("MelodyEditor/Action%0").arg(num), 0).toInt();
}

bool Settings::melodyEditorAdvance(int num)
{
	PRECONDITION(num < 3);
	
	return mSettings->value(QString("MelodyEditor/Advance%0").arg(num), false).toBool();
}

int Settings::texTabSize()
{
	return mSettings->value("MusiXTeX/TabSize", 2).toInt();
}

bool Settings::texShowBarNumber()
{
	return mSettings->value("MusixTex/Showbarnumber", true).toBool();
}

bool Settings::texShowStr()
{
	return mSettings->value("MusixTex/Showstr", true).toBool();
}

bool Settings::texShowPageNumber()
{
	return mSettings->value("MusixTex/Showpagenumber", true).toBool();
}

bool Settings::texExportMode()
{
	return mSettings->value("MusixTex/Texexportmode", false).toBool();
}

int Settings::printingStyle()
{
	return mSettings->value("Printing/Style", 0).toInt();
}

QString Settings::timidityPath()
{
	return mSettings->value("Timidiy/Path", "/usr/bin/timidity").toString();
}

int Settings::timidityBuffer()
{
	return mSettings->value("Timidiy/Buffer", 2).toInt();
}

int Settings::timidityFragment()
{
	return mSettings->value("Timidiy/Fragment", 8).toInt();
}

QString Settings::timidityOutput()
{
	return mSettings->value("Timidiy/Output", "s").toString();
}

QString Settings::timidityChorus()
{
	return mSettings->value("Timidiy/Chorus", "d").toString();
}

QString Settings::timidityDelay()
{
	return mSettings->value("Timidiy/Delay", "d").toString();
}

QString Settings::timidityReverb()
{
	return mSettings->value("Timidiy/Reverb", "d").toString();
}

QString Settings::timidityVlpf()
{
	return mSettings->value("Timidiy/Vlpf", "d").toString();
}

int Settings::timidityNoise()
{
	return mSettings->value("Timidiy/Noise", 0).toInt();
}

QString Settings::timidityResamp()
{
	return mSettings->value("Timidiy/Resamp", "l").toString();
}

int Settings::timidityFrequency()
{
	return mSettings->value("Timidiy/Frequency", 44100).toInt();
}

bool Settings::timidityRealTimeEnable()
{
	return mSettings->value("Timidiy/Realtimeenable", false).toBool();
}

int Settings::timidityRealTime()
{
	return mSettings->value("Timidiy/Realtime", 50).toInt();
}

bool Settings::assistant()
{
	return mSettings->value("Assistant/Firsttime", true).toBool();
}

QFont Settings::titleFont()
{
	return mSettings->value("Font/Title", QFont("Bitstream Vera Sans", 50)).value<QFont>();
}

QFont Settings::subTitleFont()
{
	return mSettings->value("Font/Subtitle", QFont("Bitstream Vera Sans", 24)).value<QFont>();
}

QFont Settings::textAndTabFont()
{
	return mSettings->value("Font/Text", QFont("Bitstream Vera Sans", 9)).value<QFont>();
}

QFont Settings::timeSignatureFont()
{
	return mSettings->value("Font/Timesignature", QFont("Bitstream Vera Sans", 14)).value<QFont>();
}

Settings::Settings()
{
}
