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

from Screens.Screen import Screen
from Screens.ChannelSelection import SimpleChannelSelection
from Screens.EpgSelection import EPGSelection
from Components.ActionMap import ActionMap
from Components.MenuList import MenuList
from Components.Sources.StaticText import StaticText
from Components.TimerList import TimerList
from enigma import eListboxPythonMultiContent, RT_HALIGN_LEFT, RT_VALIGN_CENTER

try:
	from Plugins.Extensions.AutoTimer.AutoTimerEditor import AutoTimerChannelSelection, AutoTimerEPGSelection
	from Plugins.Extensions.AutoTimer.AutoTimerImporter import AutoTimerImportSelector
	from Plugins.Extensions.AutoTimer.AutoTimerList import AutoTimerList
	print "[PowerMetrixFHD] 030 -OK-- AutoTimer"
except:
	print "[PowerMetrixFHD] 030 -NOK- AutoTimer"

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

def AutoTimerChannelSelection__init__(self, session, autotimer):
	SimpleChannelSelection.__init__(self, session, _("Channel Selection"))
	self.skinName = ["AutoTimerChannelSelection", "SimpleChannelSelection"]
	self.autotimer = autotimer

	self["ChannelSelectEPGActions"] = ActionMap(["ChannelSelectEPGActions"],
		{
			"showEPGList": self.channelSelected
		}
	)

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

def AutoTimerEPGSelection__init__(self, *args):
	EPGSelection.__init__(self, *args)
	self.skinName = ["AutoTimerEPGSelection", "EPGSelection"]

def AutoTimerEPGSelectionenterDateTime(self):
	pass

def AutoTimerEPGSelectioninfoKeyPressed(self):
	pass

def AutoTimerEPGSelectiononSelectionChanged(self):
	cur = self["list"].getCurrent()
	if cur is None:
		if self.key_green_choice != self.EMPTY:
			self["key_green"].setText("")
			self.key_green_choice = self.EMPTY
		return

	event = cur[0]
	self["Event"].newEvent(event)

	if cur[1] is None or cur[1].getServiceName() == "":
		if self.key_green_choice != self.EMPTY:
			self["key_green"].setText("")
			self.key_green_choice = self.EMPTY
		return

	if event is None:
		if self.key_green_choice != self.EMPTY:
			self["key_green"].setText("")
			self.key_green_choice = self.EMPTY
		return

	self["key_green"].setText(_("Add timer"))
	self.key_green_choice = self.ADD_TIMER

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

def AutoTimerImportSelector__init__(self, session, autotimer):
	Screen.__init__(self, session)
	self.skinName = ["AutoTimerImportSelector", "TimerEditList"]

	self.autotimer = autotimer

	self.list = []
	self.fillTimerList()

	self["timerlist"] = TimerList(self.list)

	self["key_red"] = StaticText(_("Cancel"))
	self["key_green"] = StaticText(_("OK"))
	self["key_yellow"] = StaticText("")
	self["key_blue"] = StaticText("")

	self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
	{
		"ok": self.openImporter,
		"cancel": self.cancel,
		"green": self.openImporter,
		"red": self.cancel
	}, -1)
	self.onLayoutFinish.append(self.setCustomTitle)

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

def AutoTimerListbuildListboxEntry(self, timer):
	size = self.l.getItemSize()

	color = None
	if not timer.enabled:
		color = self.colorDisabled

	return [
		None,
		(eListboxPythonMultiContent.TYPE_TEXT, 15, 0, size.width() - 35, size.height(), 0, RT_HALIGN_LEFT|RT_VALIGN_CENTER, timer.name, color, color)
		]

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

def doAutoTimerChange():
	try:
		AutoTimerList.buildListboxEntry = AutoTimerListbuildListboxEntry
		AutoTimerChannelSelection.__init__ = AutoTimerChannelSelection__init__
		AutoTimerEPGSelection.enterDateTime = AutoTimerEPGSelectionenterDateTime
		AutoTimerEPGSelection.infoKeyPressed = AutoTimerEPGSelectioninfoKeyPressed
		AutoTimerEPGSelection.onSelectionChanged = AutoTimerEPGSelectiononSelectionChanged
		AutoTimerEPGSelection.__init__ = AutoTimerEPGSelection__init__
		AutoTimerImportSelector.__init__ = AutoTimerImportSelector__init__

		print "[PowerMetrixFHD] 030 -OK-- AutoTimer"
	except:
		print "[PowerMetrixFHD] 030 -NOK- AutoTimer"
