from Components.GUIComponent import GUIComponent
from Tools.KeyBindings import queryKeyBinding, getKeyDescription
from enigma import eListboxPythonMultiContent, eListbox, gFont, getDesktop, RT_HALIGN_LEFT, RT_HALIGN_RIGHT, RT_VALIGN_CENTER
from skin import componentSizes, TemplatedListFonts

try:
	from Components.HelpMenuList import HelpMenuList
	print "[PowerMetrixFHD] 017 -OK-- HelpMenuList"
except:
	print "[PowerMetrixFHD] 017 -NOK- HelpMenuList"

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

def HelpMenuList__init__(self, helplist, callback):
	GUIComponent.__init__(self)
	self.onSelChanged = [ ]
	self.l = eListboxPythonMultiContent()
	self.callback = callback
	self.extendedHelp = False

	l = [ ]
	sizes = componentSizes[componentSizes.HELP_MENU_LIST]
	textX = sizes.get("textX", 5)
	textY = sizes.get("textY", 0)
	textWidth = sizes.get("textWidth", 1000)
	textHeight = sizes.get("textHeight", 35)

	for (actionmap, context, actions) in helplist:
		if not actionmap.enabled:
			continue
		for (action, help) in actions:
			buttons = queryKeyBinding(context, action)

			# do not display entries which are not accessible from keys
			if not len(buttons):
				continue

			name = None
			flags = 0

			for n in buttons:
				(name, flags) = (getKeyDescription(n[0]), n[1])
				if name is not None:
					break

			if flags & 8: # for long keypresses, prepend l_ into the key name.
				name = (name[0], "long")

			entry = [(actionmap, context, action, name )]

			if isinstance(help, list):
				self.extendedHelp = True
				print "extendedHelpEntry found"
				txt = help[0] + " - " + help[1]
			else:
				txt = help

			entry.append((eListboxPythonMultiContent.TYPE_TEXT, textX, textY, textWidth, textHeight, 0, RT_HALIGN_LEFT|RT_VALIGN_CENTER, txt))
			l.append(entry)

	self.l.setList(l)

	tlf = TemplatedListFonts()
	fontSize0 = sizes.get("fontSize0", tlf.size(tlf.BIG))
	fontSize1 = sizes.get("fontSize1", tlf.size(tlf.MEDIUM))
	self.l.setFont(0, gFont(tlf.face(tlf.BIG), fontSize0))
	self.l.setFont(1, gFont(tlf.face(tlf.MEDIUM), fontSize1))
	self.l.setItemHeight(sizes.get(componentSizes.ITEM_HEIGHT, 30))

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

def doHelpMenuListChange():
	try:
		HelpMenuList.__init__ = HelpMenuList__init__
		print "[PowerMetrixFHD] 017 -OK-- HelpMenuList"
	except:
		print "[PowerMetrixFHD] 017 -NOK- HelpMenuList"
