/*
 * powermate plugin for VDR
 *
 * setup.c - settings and setup menu
 *
 * Copyright (C) 2003 Andreas Regel <andreas.regel@powarman.de>
 *
 * This code is distributed under the terms and conditions of the
 * GNU GENERAL PUBLIC LICENSE. See the file COPYING for details.
 */

#include "powermate.h"
#include "setup.h"

const char * strContexts[kContexts] =
{
	"Normal",
	"Menu",
	"Replay"
};

const char * strEvents[kEvents] =
{
	"TurnLeft",
	"TurnRight",
	"PushedTurnLeft",
	"PushedTurnRight",
	"Click",
	"DoubleClick"
};

cPowerMateSetup::cPowerMateSetup()
:	brightness(0),
	sensitivity(1),
	doubleClickTime(300)
{
	for (int c = 0; c < kContexts; c++)
		for (int e = 0; e < kEvents; e++)
			keys[c][e] = kNone;
}

bool cPowerMateSetup::Parse(const char *Name, const char *Value)
{
	// Parse your own setup parameters and store their values.
	if      (!strcasecmp(Name, "Brightness")) brightness = atoi(Value);
	else if (!strcasecmp(Name, "Sensitivity")) sensitivity = atoi(Value);
	else if (!strcasecmp(Name, "DoubleClickTime")) doubleClickTime = atoi(Value);
	else if (strstr(Name, "Key") == Name)
	{
		for (int c = 0; c < kContexts; c++)
		{
			for (int e = 0; e < kEvents; e++)
			{
				char str[256];
				sprintf(str, "Key%s%s", strContexts[c], strEvents[e]);
				if (!strcasecmp(Name, str))
				{
					keys[c][e] = eKeys(atoi(Value));
					return true;
				}
			}
		}
		return false;
	}
	else
		return false;
	return true;
}

static tKey keyTable[] = {
	{ kNone,          "None"       },
	{ kUp,            "Up"         },
	{ kDown,          "Down"       },
	{ kMenu,          "Menu"       },
	{ kOk,            "Ok"         },
	{ kBack,          "Back"       },
	{ kLeft,          "Left"       },
	{ kRight,         "Right"      },
	{ kRed,           "Red"        },
	{ kGreen,         "Green"      },
	{ kYellow,        "Yellow"     },
	{ kBlue,          "Blue"       },
	{ k0,             "0"          },
	{ k1,             "1"          },
	{ k2,             "2"          },
	{ k3,             "3"          },
	{ k4,             "4"          },
	{ k5,             "5"          },
	{ k6,             "6"          },
	{ k7,             "7"          },
	{ k8,             "8"          },
	{ k9,             "9"          },
	{ kPlay,          "Play"       },
	{ kPause,         "Pause"      },
	{ kStop,          "Stop"       },
	{ kRecord,        "Record"     },
	{ kFastFwd,       "FastFwd"    },
	{ kFastRew,       "FastRew"    },
	{ kPower,         "Power"      },
	{ kChanUp,        "Channel+"   },
	{ kChanDn,        "Channel-"   },
	{ kVolUp,         "Volume+"    },
	{ kVolDn,         "Volume-"    },
	{ kMute,          "Mute"       },
	{ kSchedule,      "Schedule"   },
	{ kChannels,      "Channels"   },
	{ kTimers,        "Timers"     },
	{ kRecordings,    "Recordings" },
	{ kSetup,         "Setup"      },
	{ kCommands,      "Commands"   },
	{ kUser1,         "User1"      },
	{ kUser2,         "User2"      },
	{ kUser3,         "User3"      },
	{ kUser4,         "User4"      },
	{ kUser5,         "User5"      },
	{ kUser6,         "User6"      },
	{ kUser7,         "User7"      },
	{ kUser8,         "User8"      },
	{ kUser9,         "User9"      },
	{ kNone,          NULL         }
};

cPowerMateSetupPage::cPowerMateSetupPage()
{
	context = kContextNormal;
	contextOld = kContextNormal;
	brightness = PowerMateSetup.brightness;
	sensitivity = PowerMateSetup.sensitivity;
	doubleClickTime = PowerMateSetup.doubleClickTime;
	for (numKeys = 0; keyTable[numKeys].name; numKeys++)
	{
		for (int c = 0; c < kContexts; c++)
			for (int e = 0; e < kEvents; e++)
				if (keyTable[numKeys].type == PowerMateSetup.keys[c][e])
					keys[c][e] = numKeys;
		strKeys[numKeys] = new char[strlen(keyTable[numKeys].name) + 1];
		strcpy(strKeys[numKeys], keyTable[numKeys].name);
	}
	for (int e = 0; e < kEvents; e++)
		setKeys[e] = keys[context][e];
	Add(itemBrightness = new cMenuEditIntItem("Brightness", &brightness, 0, 255));
	Add(new cMenuEditIntItem("Sensitivity", &sensitivity, 1, 10));
	Add(new cMenuEditIntItem("Double click time (ms)", &doubleClickTime, 100, 1000));
	Add(itemContext = new cMenuEditStraItem("Set keys for context", &context, kContexts, strContexts));
	Add(new cMenuEditStraItem("Turn Left", &setKeys[kEventTurnLeft], numKeys, strKeys));
	Add(new cMenuEditStraItem("Turn Right", &setKeys[kEventTurnRight], numKeys, strKeys));
	Add(new cMenuEditStraItem("Pushed Turn Left", &setKeys[kEventPushedTurnLeft], numKeys, strKeys));
	Add(new cMenuEditStraItem("Pushed Turn Right", &setKeys[kEventPushedTurnRight], numKeys, strKeys));
	Add(new cMenuEditStraItem("Button Click", &setKeys[kEventClick], numKeys, strKeys));
	Add(new cMenuEditStraItem("Button Double Click", &setKeys[kEventDoubleClick], numKeys, strKeys));
}

cPowerMateSetupPage::~cPowerMateSetupPage()
{
	for (int i = 0; i < numKeys; i++)
	{
		delete[] strKeys[i];
	}
}

void cPowerMateSetupPage::Store()
{
	SetupStore("Brightness", PowerMateSetup.brightness = brightness);
	SetupStore("Sensitivity", PowerMateSetup.sensitivity = sensitivity);
	SetupStore("DoubleClickTime", PowerMateSetup.doubleClickTime = doubleClickTime);
	
	for (int e = 0; e < kEvents; e++)
		keys[contextOld][e] = setKeys[e];
	for (int c = 0; c < kContexts; c++)
	{
		for (int e = 0; e < kEvents; e++)
		{
			char str[256];
			PowerMateSetup.keys[c][e] = keyTable[keys[c][e]].type;
			sprintf(str, "Key%s%s", strContexts[c], strEvents[e]);
			SetupStore(str, PowerMateSetup.keys[c][e]);
		}
	}

	PowerMate->SetBrightness(brightness);
}

eOSState cPowerMateSetupPage::ProcessKey(eKeys key)
{
	eOSState state = cMenuSetupPage::ProcessKey(key);

	if (state == osContinue)
	{
		if (key == kLeft || key == kRight)
		{
			cOsdItem * item = Get(Current());
			if (item == itemBrightness)
			{
				PowerMate->SetBrightness(brightness);
			}
			if (item == itemContext)
			{
				for (int e = 0; e < kEvents; e++)
				{
					keys[contextOld][e] = setKeys[e];
					setKeys[e] = keys[context][e];
					item = Get(Current() + e + 1);
					item->Set();
				}
				contextOld = context;
				Display();
			}
		}
	}
	return state;
}

cPowerMateSetup PowerMateSetup;

