#include "i18n.h"
#include "setup.h"
#include <vdr/themes.h>
#include <vdr/device.h>

extern const char *OSDSKIN;

cSkinElchiSetup SkinElchiSetup;

cTheme Theme;

static const char hex[] = { "0123456789abcdef" };

const char * ShowAsItems[] = {NULL, NULL};
#ifdef HAVE_IMAGEMAGICK
const char * EpgImageItems[] = {NULL, NULL, NULL, NULL};
const char * ResizeItems[] = {NULL, NULL, NULL};
const char * AreaItems[] = {NULL, NULL, NULL};
#endif
#ifdef VIDEOSTATUS
const char * VideoStatusItems[] = {NULL, NULL, NULL, NULL};
#endif
const char * VideoSystemItems[] = {NULL, NULL};
#if VDRVERSNUM >= 10330 || defined(PATCH_CUSTOMSERVICES)
const char * TimerItems[] = {NULL, NULL, NULL, NULL};
#else
const char * TimerItems[] = {NULL, NULL};
#endif
const char * dirfilenamechars = NULL;

static const char * ColorNames [] = {
			    "clrBackground",
			    "clrButtonRedFg","clrButtonRedBg","clrButtonGreenFg","clrButtonGreenBg","clrButtonYellowFg","clrButtonYellowBg","clrButtonBlueFg","clrButtonBlueBg",
			    "clrMessageStatusFg","clrMessageStatusBg","clrMessageInfoFg","clrMessageInfoBg","clrMessageWarningFg","clrMessageWarningBg","clrMessageErrorFg","clrMessageErrorBg",
			    "clrVolumePrompt","clrVolumeBarUpper","clrVolumeBarLower","clrVolumeSymbolMuteBg","clrVolumeSymbolMuteFg","clrVolumeSymbolVolumeBg","clrVolumeSymbolVolumeFg",
			    "clrChannelNameFg","clrChannelNameBg","clrChannelNumberFg","clrChannelNumberBg","clrChannelDateFg","clrChannelDateBg","clrChannelEpgTimeFg","clrChannelEpgTimeBg","clrChannelEpgTitle","clrChannelEpgTitleBg","clrChannelEpgShortText","clrChannelSymbolOn","clrChannelSymbolOff","clrChannelSymbolRecFg","clrChannelSymbolRecBg","clrChannelTimebarSeen","clrChannelTimebarRest",
			    "clrMenuTitleFg","clrMenuTitleBg","clrMenuDate","clrMenuItemCurrentFg","clrMenuItemCurrentBg","clrMenuItemSelectable","clrMenuItemNonSelectable","clrMenuEventTime","clrMenuEventVpsFg","clrMenuEventVpsBg","clrMenuEventTitle","clrMenuEventShortText","clrMenuEventDescription","clrMenuScrollbarTotal","clrMenuScrollbarShow","clrMenuText",
			    "clrReplayTitleFg","clrReplayTitleBg","clrReplayCurrent","clrReplayTotal","clrReplayModeJump","clrReplayProgressSeen","clrReplayProgressRest","clrReplayProgressSelected","clrReplayProgressMark","clrReplayProgressCurrent","clrReplaySymbolOn","clrReplaySymbolOff",
			    "Transparent",
			    "changing",
};

/*static const char * DisplayNames [] = {NULL, NULL, NULL, NULL, NULL, NULL, };*/

int lastcurrent;
int coloritemoffset;

// --- cSkinElchiSetup ----------------------------------------------------------

cSkinElchiSetup::cSkinElchiSetup(void)
{
	logo = 0;
	strcpy(logodir, "");
	commandlinelogodir = false;
//for epgimages
#ifdef HAVE_IMAGEMAGICK
	logousemagick = 0;
	logox = 64;
	logoy = 48;
	epgimages = 0;
	epgshowtime = 5;
	EpgImageItems[0] = tr("no");
	EpgImageItems[1] = "120x90 (epg4vdr)";
	EpgImageItems[2] = "max. 180x144";
	EpgImageItems[3] = "120x120 (tvmovie2vdr)";
	resize = 0;
	ResizeItems[0] = tr("pixel algo");
	ResizeItems[1] = tr("ratio algo");
	ResizeItems[2] = tr("zoom image");
	strcpy(epgimagedir, "/video/epgimages");
	commandlinedir = false;
	AreaItems[0] = tr("no");
	AreaItems[1] = tr("yes (4-bit)");
	AreaItems[2] = tr("yes (8-bit in menu for epgsearch direct switching)");
#endif
//
#ifdef VIDEOSTATUS
	VideoStatusItems[0] = tr("no");
	VideoStatusItems[1] = tr("symbol");
	VideoStatusItems[2] = tr("text");
	VideoStatusItems[3] = tr("show");
#endif
	videosystem = vsPAL;
	VideoSystemItems[0] = tr("vdr-OSD=PAL - following is for NTSC");
	VideoSystemItems[1] = tr("vdr-OSD=NTSC - following is for PAL");
	if ((Setup.OSDTop + Setup.OSDHeight) > 480) {
	    osdleft = 48;
	    osdtop = 20;
	    osdwidth = 628;
	    osdheight = 440;
	    }	
	else {
	    osdleft = Setup.OSDLeft;
	    osdtop = Setup.OSDTop;
	    osdwidth = Setup.OSDWidth;
	    osdheight = Setup.OSDHeight;
	    }
	timer = 0;
	TimerItems[0] = tr("no");
	TimerItems[1] = tr("Next recording");
#if VDRVERSNUM >= 10330 || defined(PATCH_CUSTOMSERVICES)
	TimerItems[2] = tr("Timer-Conflict");
	TimerItems[3] = tr("all");
#endif
	char *s;
    	asprintf(&s, "%s/_", tr(FileNameChars));
	dirfilenamechars = strdup(s);
	free(s);
	singlearea = 0;
#ifdef VIDEOSTATUS
	videostatus = 0;
#if VDRVERSNUM >= 10322
	switchvideodisplayformat = 0;
#endif
#endif
	hidemenu = 0;
	strncpy(menuentry,tr("Elchi VDR Skin-Plugin"), sizeof(menuentry));
	usenofixedfont = 0;
	scrolling = 0;
	color = 0;
	ShowAsItems[0] = tr("no");
	ShowAsItems[1] = tr("as skin setup");
}

cSkinElchiSetup::~cSkinElchiSetup()
{
}

bool cSkinElchiSetup::SetupParse(const char *Name, const char *Value)
{
	if (strcmp(Name, "logo") == 0) logo = atoi(Value);
	else if (!strcasecmp(Name, "logodir") && !commandlinelogodir) strn0cpy(logodir, Value , sizeof(logodir));
//for epgimages
#ifdef HAVE_IMAGEMAGICK
	else if (strcmp(Name, "logousemagick") == 0) logousemagick = atoi(Value);
	else if (strcmp(Name, "logox") == 0) logox = atoi(Value);
	else if (strcmp(Name, "logoy") == 0) logoy = atoi(Value);
	else if (strcmp(Name, "epgimages") == 0) epgimages = atoi(Value);
	else if (strcmp(Name, "resize")	== 0) resize  = atoi(Value);
	else if (!strcasecmp(Name, "epgimagedir") && !commandlinedir) strn0cpy(epgimagedir, Value , sizeof(epgimagedir));
	else if (strcmp(Name, "epgshowtime") == 0) epgshowtime = atoi(Value);
#endif
//
	else if (strcmp(Name, "singlearea") == 0) singlearea = atoi(Value);
	else if (strcmp(Name, "videosystem") == 0) videosystem = atoi(Value);
	else if (strcmp(Name, "osdleft") == 0) osdleft = atoi(Value);
	else if (strcmp(Name, "osdtop") == 0) osdtop = atoi(Value);
	else if (strcmp(Name, "osdwidth") == 0) osdwidth = atoi(Value);
	else if (strcmp(Name, "osdheight") == 0) osdheight = atoi(Value);
	else if (strcmp(Name, "timer") == 0) timer = atoi(Value);
#ifdef VIDEOSTATUS
	else if (strcmp(Name, "videostatus") == 0) videostatus = atoi(Value);
#if VDRVERSNUM >= 10322
	else if (strcmp(Name, "switchvideodisplayformat") == 0) switchvideodisplayformat = atoi(Value);
#endif
#endif
	else if	(strcmp(Name, "usenofixedfont") == 0) usenofixedfont = atoi(Value);
	else if	(strcmp(Name, "scrolling") == 0) scrolling = atoi(Value);
	else if	(strcmp(Name, "hidemenu") == 0) hidemenu = atoi(Value);
	else if (!strcasecmp(Name, "menuentry")) snprintf(menuentry, sizeof(menuentry), Value);
	else return false;
	return true;
}


// --- cSkinElchiSetupPage ------------------------------------------------------

void cSkinElchiSetupPage::ShowItems(void)
{
	coloritemoffset = 0;
	Add(new cMenuEditBoolItem(tr("Channel logo"), &mySkinElchiSetup.logo, tr("no"), tr("show") ) );
	coloritemoffset +=1;
	if (mySkinElchiSetup.logo) {
	    if (mySkinElchiSetup.commandlinelogodir) {
		char *s;
    		asprintf(&s, "  '%s'", mySkinElchiSetup.logodir);
		cOsdItem *item = new cOsdItem(s, osUser9);
		free(s);
		item->SetSelectable(false);
		Add(item);
		}
	    else
		Add(new cMenuEditStrItem (tr("  Logos"), mySkinElchiSetup.logodir, sizeof(mySkinElchiSetup.logodir), dirfilenamechars));
	    coloritemoffset +=1;
#ifdef HAVE_IMAGEMAGICK
	    Add(new cMenuEditBoolItem(tr("  Logos loading with"), &mySkinElchiSetup.logousemagick, "vdr", "ImageMagick") );
	    coloritemoffset +=1;
	    if (mySkinElchiSetup.logousemagick) {
		Add(new cMenuEditIntItem(tr("    x"), &mySkinElchiSetup.logox, 64, 116));
		coloritemoffset +=1;
		Add(new cMenuEditIntItem(tr("    y"), &mySkinElchiSetup.logoy, 48, 87));
		coloritemoffset +=1;
		}
#endif
	    }
//for epgimages
#ifdef HAVE_IMAGEMAGICK
	Add(new cMenuEditStraItem(tr("Show pictures of epg"), &mySkinElchiSetup.epgimages, 4, EpgImageItems ) );
	coloritemoffset +=1;
	if (mySkinElchiSetup.epgimages) {
	    Add(new cMenuEditIntItem(tr("  Showtime for image [s]"), &mySkinElchiSetup.epgshowtime, 2, 15) );
	    coloritemoffset +=1;
	    if (mySkinElchiSetup.epgimages == 2) {
		Add(new cMenuEditStraItem(tr("  Resize use"), &mySkinElchiSetup.resize, 3, ResizeItems));
		coloritemoffset +=1;
		}
	    if (mySkinElchiSetup.commandlinedir) {
		char *s;
    		asprintf(&s, "%s: '%s'",tr("  Image directory"), mySkinElchiSetup.epgimagedir);
		cOsdItem *item = new cOsdItem(s, osUser9);
		free(s);
		item->SetSelectable(false);
		Add(item);
		}
	    else
		Add(new cMenuEditStrItem(tr("  Image directory"), mySkinElchiSetup.epgimagedir, sizeof(mySkinElchiSetup.epgimagedir), dirfilenamechars));
	    coloritemoffset +=1;
	    }	    
	Add(new cMenuEditStraItem(tr("One area (if possible)"), &mySkinElchiSetup.singlearea, 3, AreaItems));
//
#else
	Add(new cMenuEditBoolItem(tr("One area (if possible)"), &mySkinElchiSetup.singlearea, tr("no"), tr("yes")));
#endif
	coloritemoffset +=1;
	Add(new cMenuEditStraItem(tr("Video system"), &mySkinElchiSetup.videosystem, 2, VideoSystemItems));
	coloritemoffset +=1;
	char *s;
    	asprintf(&s, "  %s", tr("Setup.OSD$Left"));
	Add(new cMenuEditIntItem( s, &mySkinElchiSetup.osdleft, 0, MAXOSDWIDTH));
	coloritemoffset +=1;
    	asprintf(&s, "  %s", tr("Setup.OSD$Top"));
	Add(new cMenuEditIntItem( s, &mySkinElchiSetup.osdtop, 0, MAXOSDHEIGHT));
	coloritemoffset +=1;
    	asprintf(&s, "  %s", tr("Setup.OSD$Width"));
	Add(new cMenuEditIntItem( s, &mySkinElchiSetup.osdwidth, MINOSDWIDTH, MAXOSDWIDTH));
	coloritemoffset +=1;
    	asprintf(&s, "  %s", tr("Setup.OSD$Height"));
	Add(new cMenuEditIntItem( s, &mySkinElchiSetup.osdheight, MINOSDHEIGHT, MAXOSDHEIGHT));
	free(s);
	coloritemoffset +=1;
	Add(new cMenuEditStraItem(tr("Timer in menu"), &mySkinElchiSetup.timer,
#if VDRVERSNUM >= 10330 || defined(PATCH_CUSTOMSERVICES)
 4
#else
 2
#endif
, TimerItems));
	coloritemoffset +=1;
#ifdef VIDEOSTATUS
	Add(new cMenuEditStraItem(tr("Video status (if possible)"), &mySkinElchiSetup.videostatus, 4, VideoStatusItems));
	coloritemoffset +=1;
#if VDRVERSNUM >= 10322
	if (mySkinElchiSetup.videostatus) {
	    Add(new cMenuEditBoolItem(tr("  Switch VDF on replay"), &mySkinElchiSetup.switchvideodisplayformat, tr("no"), tr("yes")));
	    coloritemoffset +=1;
	    }
#endif
#endif
	Add(new cMenuEditBoolItem(tr("Don't use 'FixedFont'"), &mySkinElchiSetup.usenofixedfont, tr("no"), tr("yes") ) );
	coloritemoffset +=1;
	Add(new cMenuEditBoolItem(tr("Scroll text"), &mySkinElchiSetup.scrolling, tr("no"), tr("yes") ) );
	coloritemoffset +=1;
	Add(new cMenuEditBoolItem(tr("Colors"), &mySkinElchiSetup.color, tr("hide"), tr("show") ) );
	coloritemoffset +=1;
	if (mySkinElchiSetup.color) {
	    for (int i = 0; i < (MAX_ELCHI_THEME_COLORS - 2); i++)
	    {
		mySkinElchiSetup.ThemeColors[i] = Theme.Color(i);
		char *s;
    		asprintf(&s, "%08x", (unsigned int)mySkinElchiSetup.ThemeColors[i]);
		strncpy(mySkinElchiSetup.tempcolor, s,sizeof(mySkinElchiSetup.tempcolor));
		free(s);
		Add(new cMenuEditStrItem(ColorNames[i], mySkinElchiSetup.tempcolor, sizeof(mySkinElchiSetup.tempcolor) + 1, hex));
	    }
	}
	Add(new cMenuEditStraItem(tr("Show in main menu"), &mySkinElchiSetup.hidemenu, 2, ShowAsItems));
	if (mySkinElchiSetup.hidemenu)
	    Add(new cMenuEditStrItem ( tr("Main menu entry"), mySkinElchiSetup.menuentry, sizeof(mySkinElchiSetup.menuentry), dirfilenamechars));
}

cSkinElchiSetupPage::cSkinElchiSetupPage(void)
{
	mySkinElchiSetup = SkinElchiSetup;
	SkinElchiSetup.changing = false;
	lastcurrent = -1;
	ShowItems();
}


eOSState cSkinElchiSetupPage::ProcessKey(eKeys Key)
{
	bool oldcolor = mySkinElchiSetup.color;
	bool oldlogo = mySkinElchiSetup.logo;
#ifdef HAVE_IMAGEMAGICK
	bool oldlogousemagick = mySkinElchiSetup.logousemagick;
	int oldsinglearea = mySkinElchiSetup.singlearea;
	int oldepgimages = mySkinElchiSetup.epgimages;
#else
	bool oldsinglearea = mySkinElchiSetup.singlearea;
#endif
#ifdef VIDEOSTATUS
#if VDRVERSNUM >= 10322
	int oldvideostatus = mySkinElchiSetup.videostatus;
#endif
#endif
	int oldhidemenu = mySkinElchiSetup.hidemenu;

	char *oldtempcolor;
	asprintf(&oldtempcolor, "%s", mySkinElchiSetup.tempcolor);
	if (mySkinElchiSetup.color) {
	    int ccurrent = Current();
	    if (ccurrent > (coloritemoffset - 1) && ccurrent < (coloritemoffset + MAX_ELCHI_THEME_COLORS - 2)) { 
		SkinElchiSetup.changing = true;
		if (lastcurrent != ccurrent) {
	    	    char *s;
    	    	    asprintf(&s, "%08x", (int)mySkinElchiSetup.ThemeColors[ccurrent - coloritemoffset]);
	    	    strncpy(mySkinElchiSetup.tempcolor, s,sizeof(mySkinElchiSetup.tempcolor));
	    	    lastcurrent = ccurrent;
	    	    free(s);
		    }
		}
	    else
		SkinElchiSetup.changing = false;
	    }
	else
	    SkinElchiSetup.changing = false;
	
	eOSState state = cMenuSetupPage::ProcessKey(Key);

	if (mySkinElchiSetup.color) {
	    int ccurrent = Current();
	    if (ccurrent > (coloritemoffset - 1) && ccurrent < (coloritemoffset + MAX_ELCHI_THEME_COLORS - 2)) { 
		if (lastcurrent == ccurrent) {
		    if (strcasecmp(oldtempcolor, mySkinElchiSetup.tempcolor)) {
			unsigned int temp = 0;
			for (int i = 0; i < 8; i++) {
			    temp = temp * 16;
			    unsigned int temp1 = (unsigned int)(mySkinElchiSetup.tempcolor[i]);
			    unsigned int temp2 = (temp1 <= 0x39) ? 0x30 : 0x57;
			    temp = temp + (temp1 - temp2);
			    }
			mySkinElchiSetup.ThemeColors[ccurrent - coloritemoffset] = tColor (temp);
			SkinElchiSetup.ThemeColors[ccurrent - coloritemoffset] = mySkinElchiSetup.ThemeColors[ccurrent - coloritemoffset];
			}
		    }
		SkinElchiSetup.ThemeColors[MAX_ELCHI_THEME_COLORS - 1] = mySkinElchiSetup.ThemeColors[ccurrent - coloritemoffset];
    		Theme.AddColor("changing", SkinElchiSetup.ThemeColors[ccurrent - coloritemoffset]);
		Theme.AddColor(ColorNames[ccurrent - coloritemoffset], SkinElchiSetup.ThemeColors[ccurrent - coloritemoffset]);
		SkinElchiSetup.changing = true;
		Display();
		}
	    }		

	if (oldcolor != mySkinElchiSetup.color
	    || oldlogo != mySkinElchiSetup.logo
	    || oldsinglearea != mySkinElchiSetup.singlearea
#ifdef HAVE_IMAGEMAGICK
	    || oldlogousemagick != mySkinElchiSetup.logousemagick
	    || oldepgimages != mySkinElchiSetup.epgimages
#endif
#ifdef VIDEOSTATUS
#if VDRVERSNUM >= 10322
	    || (oldvideostatus && !mySkinElchiSetup.videostatus)
	    || (!oldvideostatus && mySkinElchiSetup.videostatus)
#endif
#endif
	    || oldhidemenu != mySkinElchiSetup.hidemenu)
	{
	    int oldcurrent = Current();
	    lastcurrent = -1;
	    Clear();
	    ShowItems();
	    SetCurrent(Get(oldcurrent));
	    Display();
	    }
	return state;
}

cSkinElchiSetupPage::~cSkinElchiSetupPage()
{
    SkinElchiSetup.changing = false;
}


void cSkinElchiSetupPage::Store(void)
{
    SkinElchiSetup = mySkinElchiSetup;
    SetupStore("logo", SkinElchiSetup.logo);
    if (!SkinElchiSetup.commandlinelogodir)
	SetupStore("logodir", SkinElchiSetup.logodir);
//for epgimages
#ifdef HAVE_IMAGEMAGICK
    SetupStore("logousemagick", SkinElchiSetup.logousemagick);
    SetupStore("logox", SkinElchiSetup.logox);
    SetupStore("logoy", SkinElchiSetup.logoy);
    SetupStore("epgimages", SkinElchiSetup.epgimages);
    SetupStore("epgshowtime", SkinElchiSetup.epgshowtime);
    SetupStore("resize", SkinElchiSetup.resize);
    if (!SkinElchiSetup.commandlinedir)
	SetupStore("epgimagedir", SkinElchiSetup.epgimagedir);
#endif
//
    SetupStore("singlearea", SkinElchiSetup.singlearea);
    SetupStore("videosystem", SkinElchiSetup.videosystem);
    SetupStore("osdleft", SkinElchiSetup.osdleft);
    SetupStore("osdtop", SkinElchiSetup.osdtop);
    SetupStore("osdwidth", SkinElchiSetup.osdwidth);
    SetupStore("osdheight", SkinElchiSetup.osdheight);
    SetupStore("timer", SkinElchiSetup.timer);
#ifdef VIDEOSTATUS
    SetupStore("videostatus", SkinElchiSetup.videostatus);
#if VDRVERSNUM >= 10322
    SetupStore("switchvideodisplayformat", SkinElchiSetup.switchvideodisplayformat);
#endif
#endif
    SetupStore("usenofixedfont", SkinElchiSetup.usenofixedfont);
    SetupStore("scrolling", SkinElchiSetup.scrolling);
    SetupStore("hidemenu", SkinElchiSetup.hidemenu);
    SetupStore("menuentry", SkinElchiSetup.menuentry);
    char *filename;
    asprintf(&filename, "%s/../../themes/%s%s", cPlugin::ConfigDirectory("skinelchi"), OSDSKIN,"-change.theme");
    Theme.Save(filename);
    free(filename);
}

