# -*- coding: UTF-8 -*-

from Screens.Screen import Screen
from Components.ActionMap import ActionMap
from Components.Label import Label
from Components.Language import language
from Components.MenuList import MenuList
from Components.MultiContent import MultiContentEntryText, MultiContentEntryPixmapAlphaTest
from Components.Sources.StaticText import StaticText
from Tools.Directories import resolveFilename, SCOPE_CURRENT_SKIN
from Tools.LoadPixmap import LoadPixmap
from enigma import eListboxPythonMultiContent, gFont, RT_HALIGN_CENTER, RT_VALIGN_CENTER
from skin import TemplatedListFonts, componentSizes

try:
	from Screens.VirtualKeyBoard import VirtualKeyBoard
	print "[PowerMetrixFHD] 027 -OK-- VirtualKeyBoard"
except:
	print "[PowerMetrixFHD] 027 -NOK- VirtualKeyBoard"

#######################################################################################################################################################

class VirtualKeyBoardList(MenuList):
	COMPONENT_VIRTUAL_KEYBOARD_LIST = "VirtualKeyBoardList"
	def __init__(self, list, enableWrapAround=False):
		MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
		tlf = TemplatedListFonts()
		fontSize = componentSizes[VirtualKeyBoardList.COMPONENT_VIRTUAL_KEYBOARD_LIST].get("fontSize", tlf.size(tlf.BIG))
		#self.l.setFont(0, gFont(tlf.face(tlf.BIG), fontSize))
		self.l.setFont(0, gFont("Ubuntu", fontSize))
		self.l.setItemHeight(VirtualKeyBoardList.itemHeight())

	@staticmethod
	def itemHeight():
		return componentSizes.itemHeight(VirtualKeyBoardList.COMPONENT_VIRTUAL_KEYBOARD_LIST, 45)

#######################################################################################################################################################

def VirtualKeyBoardEntryComponent(keys, selectedKey, shiftMode=False):
	key_backspace = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/vkey_backspace.png"))
	key_bg = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/vkey_bg.png"))
	key_clr = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/vkey_clr.png"))
	key_esc = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/vkey_esc.png"))
	key_ok = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/vkey_ok.png"))
	key_sel = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/vkey_sel.png"))
	key_shift = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/vkey_shift.png"))
	key_shift_sel = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/vkey_shift_sel.png"))
	key_space = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/vkey_space.png"))
	key_space_sel = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/vkey_space_sel.png"))
	res = [ (keys) ]

	x = 0
	count = 0
	height = VirtualKeyBoardList.itemHeight()
	if shiftMode:
		shiftkey_png = key_shift_sel
	else:
		shiftkey_png = key_shift
	for key in keys:
		width = None
		key_sel_png = key_sel
		if key == "EXIT":
			width = key_esc.size().width()
			res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(width, height), png=key_esc))
		elif key == "BACKSPACE":
			width = key_backspace.size().width()
			res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(width, height), png=key_backspace))
		elif key == "CLEAR":
			width = key_clr.size().width()
			res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(width, height), png=key_clr))
		elif key == "SHIFT":
			width = shiftkey_png.size().width()
			res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(width, height), png=shiftkey_png))
		elif key == "SPACE":
			width = key_space.size().width()
			key_sel_png = key_space_sel
			res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(width, height), png=key_space))
		elif key == "OK":
			width = key_ok.size().width()
			res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(width, height), png=key_ok))
		else:
			width = key_bg.size().width()
			res.extend((
				MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(width, height), png=key_bg),
				MultiContentEntryText(pos=(x, 0), size=(width, height), font=0, text=key.encode("utf-8"), flags=RT_HALIGN_CENTER | RT_VALIGN_CENTER)
			))

		if selectedKey == count:
			width = key_sel_png.size().width()
			res.append(MultiContentEntryPixmapAlphaTest(pos=(x, 0), size=(width, height), png=key_sel_png))

		if width is not None:
			x += width
		else:
			x += height
		count += 1

	return res

#######################################################################################################################################################

def VirtualKeyBoard__init__(self, session, title="", text=""):
	Screen.__init__(self, session)
	self.keys_list = []
	self.shiftkeys_list = []
	self.lang = language.getLanguage()
	self.nextLang = None
	self.shiftMode = False
	self.text = text
	self.selectedKey = 0

	self["key_red"] = Label(_("Backspace"))
	self["key_green"] = Label(_("OK"))
	self["key_yellow"] = Label(_("Switch keyboard"))
	self["key_blue"] = Label(_("Shift"))

	self["country"] = StaticText("")
	self["header"] = Label(title)
	self["text"] = Label(self.text)
	self["list"] = VirtualKeyBoardList([])

	self["actions"] = ActionMap(["OkCancelActions", "WizardActions", "ColorActions", "KeyboardInputActions", "InputBoxActions", "InputAsciiActions"],
		{
			"gotAsciiCode": self.keyGotAscii,
			"ok": self.okClicked,
			"cancel": self.exit,
			"left": self.left,
			"right": self.right,
			"up": self.up,
			"down": self.down,
			"red": self.backClicked,
			"green": self.ok,
			"yellow": self.switchLang,
			"blue": self.shiftClicked,
			"deleteBackward": self.backClicked,
			"back": self.exit
		}, -2)
	self.setLang()
	self.onExecBegin.append(self.setKeyboardModeAscii)
	self.onLayoutFinish.append(self.buildVirtualKeyBoard)

#######################################################################################################################################################

def VirtualKeyBoardshiftClicked(self):
	if self.shiftMode:
		self.shiftMode = False
	else:
		self.shiftMode = True
	self.buildVirtualKeyBoard(self.selectedKey)

#######################################################################################################################################################

def VirtualKeyBoardswitchLang(self):
	self.lang = self.nextLang
	self.setLang()
	self.selectedKey = 0
	self.buildVirtualKeyBoard()

#######################################################################################################################################################

def VirtualKeyBoardsetLang(self):
	if self.lang == "it_IT":
		self.keys_list = [
			[u"EXIT", u"1", u"2", u"3", u"4", u"5", u"6", u"7", u"8", u"9", u"0", u"BACKSPACE"],
			[u"q", u"w", u"e", u"r", u"t", u"y", u"u", u"i", u"o", u"p", u"è", u"+"],
			[u"a", u"s", u"d", u"f", u"g", u"h", u"j", u"k", u"l", u"ò", u"à", u"ù"],
			[u"<", u"z", u"x", u"c", u"v", u"b", u"n", u"m", u",", ".", u"-", u"CLEAR"],
			[u"SHIFT", u"\\", u"SPACE", u"'", u"ì", u"@", u"#", u"OK"]]
		self.shiftkeys_list = [
			[u"EXIT", u"!", u'"', u"£", u"$", u"%", u"&", u"/", u"(", u")", u"=", u"BACKSPACE"],
			[u"Q", u"W", u"E", u"R", u"T", u"Y", u"U", u"I", u"O", u"P", u"é", u"*"],
			[u"A", u"S", u"D", u"F", u"G", u"H", u"J", u"K", u"L", u"ç", u"°", u"§"],
			[u">", u"Z", u"X", u"C", u"V", u"B", u"N", u"M", u";", u":", u"_", u"CLEAR"],
			[u"SHIFT", u"|", u"SPACE", u"?", u"^", u"[", u"]", u"OK"]]
		self.nextLang = "de_DE"

	elif self.lang == "de_DE":
		self.keys_list = [
			[u"EXIT", u"1", u"2", u"3", u"4", u"5", u"6", u"7", u"8", u"9", u"0", u"BACKSPACE"],
			[u"q", u"w", u"e", u"r", u"t", u"z", u"u", u"i", u"o", u"p", u"ü", u"+"],
			[u"a", u"s", u"d", u"f", u"g", u"h", u"j", u"k", u"l", u"ö", u"ä", u"#"],
			[u"<", u"y", u"x", u"c", u"v", u"b", u"n", u"m", u",", u".", u"-", u"CLEAR"],
			[u"SHIFT", u"^", u"SPACE", u"ß", u"´", u"[", u"]", u"@", u"\\", u"~", u"OK"]]
		self.shiftkeys_list = [
			[u"EXIT", u"!", u'"', u"§", u"$", u"%", u"&", u"/", u"(", u")", u"=", u"BACKSPACE"],
			[u"Q", u"W", u"E", u"R", u"T", u"Z", u"U", u"I", u"O", u"P", u"Ü", u"*"],
			[u"A", u"S", u"D", u"F", u"G", u"H", u"J", u"K", u"L", u"Ö", u"Ä", u"'"],
			[u">", u"Y", u"X", u"C", u"V", u"B", u"N", u"M", u";", u":", u"_", u"CLEAR"],
			[u"SHIFT", u"°", u"SPACE", u"?", u"`", u"{", u"}", u"µ", u"|", u"", u"OK"]]
		self.nextLang = "en_EN"

	else:
		self.keys_list = [
			[u"EXIT", u"1", u"2", u"3", u"4", u"5", u"6", u"7", u"8", u"9", u"0", u"BACKSPACE"],
			[u"q", u"w", u"e", u"r", u"t", u"y", u"u", u"i", u"o", u"p", u"[", u"]"],
			[u"a", u"s", u"d", u"f", u"g", u"h", u"j", u"k", u"l", u";", u"'", u"#"],
			[u"\\", u"z", u"x", u"c", u"v", u"b", u"n", u"m", u",", ".", u"/", u"CLEAR"],
			[u"SHIFT", u"`", u"SPACE", u"-", u"=", u"OK"]]
		self.shiftkeys_list = [
			[u"EXIT", u"!", u'"', u"£", u"$", u"%", u"^", u"&", u"*", u"(", u")", u"BACKSPACE"],
			[u"Q", u"W", u"E", u"R", u"T", u"Y", u"U", u"I", u"O", u"P", u"{", u"}"],
			[u"A", u"S", u"D", u"F", u"G", u"H", u"J", u"K", u"L", u":", u"@", u"~"],
			[u"|", u"Z", u"X", u"C", u"V", u"B", u"N", u"M", u"<", u">", u"?", u"CLEAR"],
			[u"SHIFT", u"¬", u"SPACE", u"_", u"+", u"OK"]]

		self.lang = "en_EN"
		self.nextLang = "it_IT"
	self["country"].setText(self.lang)
	self.max_key = 47 + len(self.keys_list[4])

#######################################################################################################################################################

def VirtualKeyBoardbuildVirtualKeyBoard(self, selectedKey=0):
	list = []

	if self.shiftMode:
		self.k_list = self.shiftkeys_list
		for keys in self.k_list:
			if selectedKey < 12 and selectedKey > -1:
				list.append(VirtualKeyBoardEntryComponent(keys, selectedKey, True))
			else:
				list.append(VirtualKeyBoardEntryComponent(keys, -1, True))
			selectedKey -= 12
	else:
		self.k_list = self.keys_list
		for keys in self.k_list:
			if selectedKey < 12 and selectedKey > -1:
				list.append(VirtualKeyBoardEntryComponent(keys, selectedKey))
			else:
				list.append(VirtualKeyBoardEntryComponent(keys, -1))
			selectedKey -= 12

	self["list"].setList(list)

#######################################################################################################################################################

def doVirtualKeyBoardChange():
	try:
		VirtualKeyBoard.setLang = VirtualKeyBoardsetLang
		VirtualKeyBoard.buildVirtualKeyBoard = VirtualKeyBoardbuildVirtualKeyBoard
		VirtualKeyBoard.shiftClicked = VirtualKeyBoardshiftClicked
		VirtualKeyBoard.switchLang = VirtualKeyBoardswitchLang
		VirtualKeyBoard.__init__ = VirtualKeyBoard__init__
		print "[PowerMetrixFHD] 027 -OK-- VirtualKeyBoard"
	except:
		print "[PowerMetrixFHD] 027 -NOK- VirtualKeyBoard"
