from Screens.Screen import Screen
from Components.ActionMap import NumberActionMap
from Components.Label import Label
from Components.ChoiceList import ChoiceEntryComponent, ChoiceList
from Components.Sources.StaticText import StaticText

try:
	from Screens.ChoiceBox import ChoiceBox
	print "[PowerMetrixFHD] 014 -OK-- ChoiceBox"
except:
	print "[PowerMetrixFHD] 014 -NOK- ChoiceBox"

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

def ChoiceBox__init__(self, session, title = "", list = [], keys = None, selection = 0, skin_name = [], windowTitle = None, allow_cancel = True, titlebartext = _("Input")):
	if not windowTitle: #for compatibility
		windowTitle = titlebartext
	Screen.__init__(self, session, windowTitle=windowTitle)

	self.allow_cancel = allow_cancel

	if isinstance(skin_name, str):
		skin_name = [skin_name]
	if not title:
		skin_name = skin_name + ["ChoiceBoxNoTitle"]
	self.skinName = skin_name + ["ChoiceBox"]

	self["text"] = Label(title)
	self.list = []
	self.summarylist = []
	if keys is None:
		self.__keys = [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "red", "green", "yellow", "blue", "text" ] + (len(list) - 10) * [""]
	else:
		self.__keys = keys + (len(list) - len(keys)) * [""]

	self.keymap = {}
	pos = 0
	for x in list:
		strpos = str(self.__keys[pos])
		self.list.append(ChoiceEntryComponent(key = strpos, text = x))
		if self.__keys[pos] != "":
			self.keymap[self.__keys[pos]] = list[pos]
		self.summarylist.append((self.__keys[pos],x[0]))
		pos += 1
	self["list"] = ChoiceList(list = self.list, selection = selection)
	self["summary_list"] = StaticText()
	self.updateSummary()

	self["actions"] = NumberActionMap(["WizardActions", "InputActions", "ColorActions", "DirectionActions"], 
	{
		"ok": self.go,
		"back": self.cancel,
		"1": self.keyNumberGlobal,
		"2": self.keyNumberGlobal,
		"3": self.keyNumberGlobal,
		"4": self.keyNumberGlobal,
		"5": self.keyNumberGlobal,
		"6": self.keyNumberGlobal,
		"7": self.keyNumberGlobal,
		"8": self.keyNumberGlobal,
		"9": self.keyNumberGlobal,
		"0": self.keyNumberGlobal,
		"red": self.keyRed,
		"green": self.keyGreen,
		"yellow": self.keyYellow,
		"blue": self.keyBlue,
		"text": self.keyText,
		"up": self.up,
		"down": self.down
	}, -1)

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

def doChoiceBoxChange():
	try:
		ChoiceBox.__init__ = ChoiceBox__init__
		print "[PowerMetrixFHD] 014 -OK-- ChoiceBox"
	except:
		print "[PowerMetrixFHD] 014 -NOK- ChoiceBox"
