from Screens.Screen import Screen
from Components.ActionMap import ActionMap, NumberActionMap
from Components.Label import Label
from Components.MenuList import MenuList
from Components.Pixmap import Pixmap
from enigma import getDesktop

try:
	from Plugins.Extensions.TeleText.__init__ import _debug, _log
	from Plugins.Extensions.TeleText.plugin import TeleTextTransponderMenu, TeleTextFavoritesMenu, log
	print "[PowerMetrixFHD] 021 -OK-- TeleText"
except:
	print "[PowerMetrixFHD] 021 -NOK- TeleText"

dsk_size = getDesktop(0).size()
dsk_width = dsk_size.width()
dsk_height = dsk_size.height()

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

def TeleTextTransponderMenu__init__(self, session, list, index):
	log("[transponder] __init__")

	self.ch_list = list
	self.ch_index = index
	self.cur_service = self.ch_list[self.ch_index][1]

	width = 620
	height = 90
	left = (dsk_width - width) >> 1
	top = (dsk_height - height) >> 1
	log("[transponder] screen rect %s %s %s %s" % (left, top, width, height))
	TeleTextTransponderMenu.skin = """<screen position="%d,%d" size="%d,%d" title="%s">
		<ePixmap pixmap="skin_default/buttons/red.png" position="10,5" size="200,40" alphatest="on" />
		<ePixmap pixmap="skin_default/buttons/green.png" position="410,5" size="200,40" alphatest="on" />
		<widget name="key_r" position="10,5" size="200,40" zPosition="1" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" shadowColor="black" shadowOffset="-2,-2" />
		<widget name="key_g" position="410,5" size="200,40" zPosition="1" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" shadowColor="black" shadowOffset="-2,-2" />
		<eLabel position="10,50" size="600,1" backgroundColor="grey"/>
		<widget name="prev" position="10,60" size="50,25" pixmap="skin_default/icons/left.png" alphatest="on"/>
		<widget name="channel" position="70,62" size="450,20" valign="center" halign="left" font="Regular;21"/>
		<widget name="zapped" position="530,62" size="20,20" valign="center" halign="right" font="Regular;21"/>
		<widget name="next" position="560,60" size="50,25" pixmap="skin_default/icons/right.png" alphatest="on"/>
	</screen>""" % (left, top, width, height, _("Select teletext"))
	Screen.__init__(self, session)

	self["actions"] = ActionMap(["OkCancelActions", "TeleTextActions"],
	{
	 "ok"     : self.okPressed,
	 "cancel" : self.cancelPressed,
	 "red"    : self.cancelPressed,
	 "green"  : self.okPressed,
	 "left"   : self.prevPressed,
	 "right"  : self.nextPressed,
	 "prev"   : self.prevPressed,
	 "next"   : self.nextPressed,
	 "prev_long": self.prevPressed,
	 "next_long": self.nextPressed
	}, -2)
	self["actions"].setEnabled(True)

	self["key_r"] = Label(_("Cancel"))
	self["key_g"] = Label(_("OK"))
	self["green"] = Pixmap()

	self["channel"] = Label("")
	self["prev"] = Pixmap()
	self["next"] = Pixmap()
	self["zapped"] = Label("*")
	self["zapped"].hide()
	if len(self.ch_list) == 1:
		self["prev"].hide()
		self["next"].hide()
	else:
		self["prev"].show()
		self["next"].show()
	self.updateLayout()

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

def TeleTextTransponderMenuupdateLayout(self):
	if self.ch_list[self.ch_index][2] == -1:
		self["zapped"].show()
		self["green"].hide()
		self["key_g"].setText("")
	else:
		self["zapped"].hide()
		self["green"].show()
		self["key_g"].setText(_("OK"))
	self.new_service = self.ch_list[self.ch_index][1]
	self["channel"].setText(self.new_service)
	for i in self.onChangedEntry:
		i()

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

def TeleTextFavoritesMenu__init__(self, session, parent, service, page, favorites):
	width = 820
	height = 480
	left = (dsk_width - width) >> 1
	top = (dsk_height - height) >> 1
	log("[fav-menu] screen rect %s %s %s %s" % (left, top, width, height))
	TeleTextFavoritesMenu.skin = """<screen position="%d,%d" size="%d,%d" title="%s [%s]">
		<ePixmap pixmap="skin_default/buttons/red.png" position="10,5" size="200,40" alphatest="on" />
		<ePixmap pixmap="skin_default/buttons/green.png" position="210,5" size="200,40" alphatest="on" />
		<ePixmap pixmap="skin_default/buttons/yellow.png" position="410,5" size="200,40" alphatest="on" />
		<ePixmap pixmap="skin_default/buttons/blue.png" position="610,5" size="200,40" alphatest="on" />
		<widget name="key_r" position="10,5" size="200,40" zPosition="1" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" shadowColor="black" shadowOffset="-2,-2" />
		<widget name="key_g" position="210,5" size="200,40" zPosition="1" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" shadowColor="black" shadowOffset="-2,-2" />
		<widget name="key_y" position="410,5" size="200,40" zPosition="1" font="Regular;20" halign="center" valign="center" backgroundColor="#a08500" transparent="1" shadowColor="black" shadowOffset="-2,-2" />
		<widget name="key_b" position="610,5" size="200,40" zPosition="1" font="Regular;20" halign="center" valign="center" backgroundColor="#18188b" transparent="1" shadowColor="black" shadowOffset="-2,-2" />
		<eLabel position="10,50" size="800,1" backgroundColor="grey" />
		<widget name="fav_list" position="63,60" size="754,330" enableWrapAround="1" scrollbarMode="showOnDemand" />
		<ePixmap pixmap="skin_default/buttons/key_1.png" position="10,63" size="50,25" alphatest="on"/>
		<ePixmap pixmap="skin_default/buttons/key_2.png" position="10,93" size="50,25" alphatest="on"/>
		<ePixmap pixmap="skin_default/buttons/key_3.png" position="10,123" size="50,25" alphatest="on"/>
		<ePixmap pixmap="skin_default/buttons/key_4.png" position="10,153" size="50,25" alphatest="on"/>
		<ePixmap pixmap="skin_default/buttons/key_5.png" position="10,183" size="50,25" alphatest="on"/>
		<ePixmap pixmap="skin_default/buttons/key_6.png" position="10,213" size="50,25" alphatest="on"/>
		<ePixmap pixmap="skin_default/buttons/key_7.png" position="10,243" size="50,25" alphatest="on"/>
		<ePixmap pixmap="skin_default/buttons/key_8.png" position="10,273" size="50,25" alphatest="on"/>
		<ePixmap pixmap="skin_default/buttons/key_9.png" position="10,303" size="50,25" alphatest="on"/>
		<ePixmap pixmap="skin_default/buttons/key_0.png" position="10,333" size="50,25" alphatest="on"/>
	</screen>""" % (left, top, width, height, _("TeleText favorites"), service[1])

	self.service = service[0]
	self.page = page
	self.favorites = favorites

	Screen.__init__(self, session, parent)

	self["fav_list"] = MenuList([], enableWrapAround = True)
	self["fav_list"].onSelectionChanged.append(self.updateLayout)

	self.title = "%s [%s]" %(_("TeleText favorites"), service[1])

	self["actions"] = NumberActionMap(["OkCancelActions", "TeleTextActions"],
	{
	 "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,
	 "ok"     : self.okPressed,
	 "cancel" : self.cancelPressed,
	 "red"    : self.redPressed,
	 "green"  : self.okPressed,
	 "yellow" : self.yellowPressed,
	 "blue"   : self.bluePressed
	}, -2)
	self["actions"].setEnabled(True)

	self["key_r"] = Label("")
	self["red"] = Pixmap()
	self["red"].hide()
	self["key_g"] = Label("")
	self["green"] = Pixmap()
	self["green"].hide()
	self["key_y"] = Label("")
	self["key_b"] = Label(_("Clear all"))

	self.onLayoutFinish.append(self.layoutFinished)

def TeleTextFavoritesMenulayoutFinished(self):
	self.setTitle(self.title)
	self.updateList()
	self.updateLayout()

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

def TeleTextFavoritesMenuupdateLayout(self):
	value = self["fav_list"].getCurrent()
	self.sel_page = self.pages[self["fav_list"].getSelectedIndex()]
	if value == _("<empty>"):
		self["key_r"].setText("")
		self["red"].hide()
		self["key_g"].setText("")
		self["green"].hide()
		self["key_y"].setText(_("Add"))
	else:
		self["key_r"].setText(_("Delete"))
		self["red"].show()
		self["key_g"].setText(_("Select"))
		self["green"].show()
		self["key_y"].setText(_("Edit"))

	for i in self.onChangedEntry:
		i()

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

def doTeleTextChange():
	try:
		TeleTextTransponderMenu.__init__ = TeleTextTransponderMenu__init__
		TeleTextTransponderMenu.updateLayout = TeleTextTransponderMenuupdateLayout
		TeleTextFavoritesMenu.layoutFinished = TeleTextFavoritesMenulayoutFinished
		TeleTextFavoritesMenu.__init__ = TeleTextFavoritesMenu__init__
		TeleTextFavoritesMenu.updateLayout = TeleTextFavoritesMenuupdateLayout
		print "[PowerMetrixFHD] 021 -OK-- TeleText"
	except:
		print "[PowerMetrixFHD] 021 -NOK- TeleText"
