#include <stdlib.h>
#include <string.h>

#include "common.h"
#include "device.h"
#include "setup.h"

cSetupEm84xx SetupEm84xx;

const char* output_aspect_ratio[]		= {"TvAspectRatio","TV is","4:3","16:9"};
const char* output_handle_43[]		= {"Tv43Handle169","Handle 16:9 on 4:3 TV","Letterbox","Pan & scan"};
const char* output_handle_169[]         = {"Tv169Handle43","Handle 4:3 on 16:9 TV","Zoom on","Zoom of","Nothing","Zoom on 16:9","Zoom on 16:10"};
const char* contrast[]			= {"TvContrast","Contrast (0-1000)"};
const char* brightness[]			= {"TvBrightness","Brightness (0-1000)"};
const char* saturation[]			= {"TvSaturation","Saturation (0-1000)"};
const char* spdif_forward[]		= {"AudioOut","Audio output","Stereo downmix","Passthrough"};
//const char* doaudiolater[]		= {"AudioPtsDelay","Delay audio (*11.1usec)}";
const char* dvd_speedcontrol[] 		= {"DvdSpeedControl","DVD speed control","Off","On"};
const char* dvd_menuonstart[]		= {"DvdMenuOnStart","DVD menu on disc start","Off","On"};
const char* dvd_audio_language[]		= {"DvdAudioLanguage","DVD audio language","Original","English","French","Spanish","Italian","German","Dutch","Portuguese","Chinese","Japanese","Others"};
const char* dvd_subpicture_language[]	= {"DvdSubpictureLanguage","DVD subpicture language","Follow audio","English","French","Spanish","Italian","German","Dutch","Portuguese","Chinese","Japanese","Others"};
const char* dvd_menu_language[]		= {"DvdMenuLanguage","DVD menu language","English","French","Spanish","Italian","German","Dutch","Portuguese","Chinese","Japanese","Others"};
const char* dvd_subpicture[]		= {"DvdSubpicture","DVD subpicture","Off","On"};
const char* dvd_actions[]			= {"None", "Exit", "Time jump","Jump -1 min","Jump +1 min","Prev title","Next title",
							"Prev chapter","Next chapter","Play","Pause","Forward","Rewind","Next audiostream",
							"Next subpicture","Next angle","DVD root menu","DVD title menu","DVD subpicture menu",
							"DVD audio menu","DVD angle menu","DVD chapter menue","Resume DVD"};

tDiscPlayerKey discPlayerKeyTable[] =
{
	{ kRed,           "Red",		"DvdKeyRed"		},
	{ kGreen,         "Green",		"DvdKeyGreen"		},
	{ kYellow,        "Yellow",	"DvdKeyYellow"	},
	{ kBlue,          "Blue",		"DvdKeyBlue"		},
	{ k0,             "0",		"DvdKey0"		},
	{ k1,             "1",		"DvdKey1"		},
	{ k2,             "2",		"DvdKey2"		},
	{ k3,             "3",		"DvdKey3"		},
	{ k4,             "4",		"DvdKey4"		},
	{ k5,             "5",		"DvdKey5"		},
	{ k6,             "6",		"DvdKey6"		},
	{ k7,             "7",		"DvdKey7"		},
	{ k8,             "8",		"DvdKey8"		},
	{ k9,             "9",		"DvdKey9"		},
	{ kPlay,          "Play",		"DvdKeyPlay"		},
	{ kPause,         "Pause",		"DvdKeyPause"		},
	{ kStop,          "Stop",		"DvdKeyStop"		},
	{ kFastFwd,       "FastFwd",	"DvdKeyFastFow"	},
	{ kFastRew,       "FastRew",	"DvdKeyFastRew"	},
	{ kAudio,         "Audio",		"DvdKeyAudio"		},
	{ kSchedule,      "Schedule",	"DvdKeySchedule"	},
	{ kChannels,      "Channels",	"DvdKeyChannels"	},
	{ kTimers,        "Timers"	,	"DvdKeyTimers"	},
	{ kRecordings,    "Recordings",	"DvdKeyRecordings"	},
	{ kSetup,         "Setup",		"DvdKeySetup"		},
	{ kCommands,      "Commands",	"DvdKeyCommands"	},
	{ kUser1,         "User1",		"DvdKeyUser1"		},
	{ kUser2,         "User2",		"DvdKeyUser2"		},
	{ kUser3,         "User3",		"DvdKeyUser3"		},
	{ kUser4,         "User4",		"DvdKeyUser4"		},
	{ kUser5,         "User5",		"DvdKeyUser5"		},
	{ kUser6,         "User6",		"DvdKeyUser6"		},
	{ kUser7,         "User7",		"DvdKeyUser7"		},
	{ kUser8,         "User8",		"DvdKeyUser8"		},
	{ kUser9,         "User9",		"DvdKeyUser9"		},
};

cSetupEm84xx::cSetupEm84xx()
{
	TvIs169 = false;
	Tv43HandleOther = 0;
	Tv169HandleOther = 0;
	Contrast = 500;
	Brightness = 500;
	Saturation = 500;
	SpdifForward = false;
//	DoAudioLater = 0;
	ControlDvdSpeed = 0;
	DvdMenuOnStart = 0;
	DvdAudioLanguage = 0;
	DvdSubpictureLanguage = 0;
	DvdMenuLanguage = 0;
	DvdSubpicture = 0;

	PlayerKeys[0]		= discTimeJump;
	PlayerKeys[1]		= discPrevMinute;
	PlayerKeys[2]		= discNextMinute;
	PlayerKeys[3]		= discDvdRootMenu;
	PlayerKeys[4]		= discDvdResumeBookmark;
	PlayerKeys[5]		= discNextAudiostream;
	PlayerKeys[6]		= discNextSubpicture;
	PlayerKeys[7]		= discNextAngle;
	PlayerKeys[8]		= discPrevChapter;
	PlayerKeys[9]		= discActionNone;
	PlayerKeys[10]	= discNextChapter;
	PlayerKeys[11]	= discPrevTrack;
	PlayerKeys[12]	= discActionNone;
	PlayerKeys[13]	= discNextTrack;
	PlayerKeys[14]	= discPlay;
	PlayerKeys[15]	= discPause;
	PlayerKeys[16]	= discExit;
	PlayerKeys[17]	= discForward;
	PlayerKeys[18]	= discRewind;
	PlayerKeys[19]	= discDvdAudioMenu;
	PlayerKeys[20]	= discActionNone;
	PlayerKeys[21]	= discActionNone;
	PlayerKeys[22]	= discActionNone;
	PlayerKeys[23]	= discActionNone;
	PlayerKeys[24]	= discActionNone;
	PlayerKeys[25]	= discActionNone;
	PlayerKeys[26]	= discActionNone;
	PlayerKeys[27]	= discActionNone;
	PlayerKeys[28]	= discActionNone;
	PlayerKeys[29]	= discActionNone;
	PlayerKeys[30]	= discActionNone;
	PlayerKeys[31]	= discActionNone;
	PlayerKeys[32]	= discActionNone;
	PlayerKeys[33]	= discActionNone;
	PlayerKeys[34]	= discActionNone;
}

bool cSetupEm84xx::SetupParse(const char *Name, const char *Value)
{
	if (!strcasecmp(Name, output_aspect_ratio[0]))
		TvIs169  = atoi(Value);
	else if (!strcasecmp(Name, output_handle_43[0]))
		Tv43HandleOther = atoi(Value);
	else if (!strcasecmp(Name, output_handle_169[0]))
		Tv169HandleOther = atoi(Value);
	else if (!strcasecmp(Name, contrast[0]))
		Contrast = atoi(Value);
	else if (!strcasecmp(Name, brightness[0]))
		Brightness = atoi(Value);
	else if (!strcasecmp(Name, saturation[0]))
		Saturation = atoi(Value);
	else if (!strcasecmp(Name, spdif_forward[0]))
		SpdifForward = atoi(Value);
//	else if (!strcasecmp(Name, doaudiolater[0]))
//		DoAudioLater = atoi(Value);
	else if (!strcasecmp(Name, dvd_speedcontrol[0]))
		ControlDvdSpeed = atoi(Value);
	else if (!strcasecmp(Name, dvd_menuonstart[0]))
		DvdMenuOnStart = atoi(Value);
	else if (!strcasecmp(Name, dvd_audio_language[0]))
		DvdAudioLanguage = atoi(Value);
	else if (!strcasecmp(Name, dvd_subpicture_language[0]))
		DvdSubpictureLanguage = atoi(Value);
	else if (!strcasecmp(Name, dvd_menu_language[0]))
		DvdMenuLanguage = atoi(Value);
	else if (!strcasecmp(Name, dvd_subpicture[0]))
		DvdSubpicture = atoi(Value);
	else
	{
		for (int i=0; i < NOOFPLAYERKEYS; i++)
		{	
			if (!strcasecmp(Name, discPlayerKeyTable[i].storename))
			{
				PlayerKeys[i] = atoi(Value);
				return true;
			}
		}

		return false;
	}
		

	return true;
}

cMenuSetupEm84::cMenuSetupEm84(void)
{
	mSetup = SetupEm84xx;

	Add(new cOsdItem(hk("Video settings"),           osUser1));
	Add(new cOsdItem(hk("Audio settings"),           osUser2));

	if (!sDvdConnected)
		return;

	Add(new cOsdItem(hk("DVD settings"),           osUser3));
	Add(new cOsdItem(hk("Disc player keys"),           osUser4));
}

eOSState cMenuSetupEm84::ProcessKey(eKeys Key)
{
	eOSState state = cMenuSetupPage::ProcessKey(Key);

	switch (state)
	{
		case osUser1:
			return AddSubMenu(new cSubMenuSetupEm84_Video(this,&mSetup));
		case osUser2:
			return AddSubMenu(new cSubMenuSetupEm84_Audio(this,&mSetup));
		case osUser3:
			return AddSubMenu(new cSubMenuSetupEm84_DVD(this,&mSetup));
		case osUser4:
			return AddSubMenu(new cSubMenuSetupEm84_DVDPlayer(this,&mSetup));

		default:
		break;
	}

	return state;
}

void cMenuSetupEm84::Store(void)
{
	SetupStore(output_aspect_ratio[0], mSetup.TvIs169);
	SetupStore(output_handle_43[0], mSetup.Tv43HandleOther);
	SetupStore(output_handle_169[0], mSetup.Tv169HandleOther);
	SetupStore(contrast[0], mSetup.Contrast);
	SetupStore(brightness[0], mSetup.Brightness);
	SetupStore(saturation[0], mSetup.Saturation);
	SetupStore(spdif_forward[0], mSetup.SpdifForward);
//	SetupStore(doaudiolater[0], mSetup.DoAudioLater);
	SetupStore(dvd_speedcontrol[0], mSetup.ControlDvdSpeed);
	SetupStore(dvd_menuonstart[0], mSetup.DvdMenuOnStart);
	SetupStore(dvd_audio_language[0], mSetup.DvdAudioLanguage);
	SetupStore(dvd_subpicture_language[0], mSetup.DvdSubpictureLanguage);
	SetupStore(dvd_menu_language[0], mSetup.DvdMenuLanguage);
	SetupStore(dvd_subpicture[0], mSetup.DvdSubpicture);
	for (int i=0; i < NOOFPLAYERKEYS; i++)
	{
		SetupStore(discPlayerKeyTable[i].storename, mSetup.PlayerKeys[i]);
	}


	SetupEm84xx = mSetup;

	cDeviceEm84xx::Configure();
}


cSubMenuSetupEm84Base::cSubMenuSetupEm84Base(cMenuSetupEm84 *iSetupMenu, cSetupEm84xx *iSetup)
{
	mSetup = iSetup;
	mSetupMenu = iSetupMenu;
}

void cSubMenuSetupEm84Base::Store(void)
{
	mSetupMenu->Store();
}

cSubMenuSetupEm84_Video::cSubMenuSetupEm84_Video(cMenuSetupEm84 *iSetupMenu, cSetupEm84xx *iSetup) : cSubMenuSetupEm84Base(iSetupMenu, iSetup)
{
	Add(new cMenuEditStraItem(output_aspect_ratio[1],&mSetup->TvIs169, 2, &output_aspect_ratio[2]));
	Add(new cMenuEditStraItem(output_handle_43[1],&mSetup->Tv43HandleOther, 2, &output_handle_43[2]));
	Add(new cMenuEditStraItem(output_handle_169[1],&mSetup->Tv169HandleOther, 5, &output_handle_169[2]));
	Add(new cMenuEditIntItem(contrast[1], &mSetup->Contrast, 0, 1000));
	Add(new cMenuEditIntItem(brightness[1], &mSetup->Brightness, 0, 1000));
	Add(new cMenuEditIntItem(saturation[1], &mSetup->Saturation, 0, 1000));
}

cSubMenuSetupEm84_Audio::cSubMenuSetupEm84_Audio(cMenuSetupEm84 *iSetupMenu, cSetupEm84xx *iSetup) : cSubMenuSetupEm84Base(iSetupMenu, iSetup)
{
	Add(new cMenuEditStraItem(spdif_forward[1],&mSetup->SpdifForward, 2, &spdif_forward[2]));
//	Add(new cMenuEditIntItem(doaudiolater[1], &mSetup->DoAudioLater, INT_MIN, INT_MAX));
}

cSubMenuSetupEm84_DVD::cSubMenuSetupEm84_DVD(cMenuSetupEm84 *iSetupMenu, cSetupEm84xx *iSetup) : cSubMenuSetupEm84Base(iSetupMenu, iSetup)
{
	Add(new cMenuEditStraItem(dvd_speedcontrol[1],&mSetup->ControlDvdSpeed, 2, &dvd_speedcontrol[2]));
	Add(new cMenuEditStraItem(dvd_menuonstart[1],&mSetup->DvdMenuOnStart, 2, &dvd_menuonstart[2]));
	Add(new cMenuEditStraItem(dvd_audio_language[1],&mSetup->DvdAudioLanguage, 11, &dvd_audio_language[2]));
	Add(new cMenuEditStraItem(dvd_subpicture_language[1],&mSetup->DvdSubpictureLanguage, 11, &dvd_subpicture_language[2]));
	Add(new cMenuEditStraItem(dvd_menu_language[1],&mSetup->DvdMenuLanguage, 10, &dvd_menu_language[2]));
	Add(new cMenuEditStraItem(dvd_subpicture[1],&mSetup->DvdSubpicture, 2, &dvd_subpicture[2]));
}

cSubMenuSetupEm84_DVDPlayer::cSubMenuSetupEm84_DVDPlayer(cMenuSetupEm84 *iSetupMenu, cSetupEm84xx *iSetup) : cSubMenuSetupEm84Base(iSetupMenu, iSetup)
{
	for (int i=0; i < NOOFPLAYERKEYS; i++)
	{
		Add(new cMenuEditStraItem(discPlayerKeyTable[i].name,&mSetup->PlayerKeys[i], NOOFPLAYERACTIONS, dvd_actions));
	}
};
