from Screens.Screen import Screen
from Screens.ChoiceBox import ChoiceBox
from Screens.MessageBox import MessageBox
from Screens.TimerEntry import TimerEntry, TimerLog
from Components.ActionMap import ActionMap
from Components.Button import Button
from Components.config import config
from Components.MenuList import MenuList
from Components.Sources.StaticText import StaticText
from Components.TimerList import TimerList
from Components.TimerSanityCheck import TimerSanityCheck
from Components.UsageConfig import preferredTimerPath
from RecordTimer import RecordTimerEntry, parseEvent, AFTEREVENT
from Tools.BoundFunction import boundFunction
from ServiceReference import ServiceReference
from time import time
from timer import TimerEntry as RealTimerEntry

try:
	from Screens.TimerEdit import TimerEditList
	from Screens.TimerEdit import TimerSanityConflict
	print "[PowerMetrixFHD] 016 -OK-- TimerEdit"
except:
	print "[PowerMetrixFHD] 016 -NOK- TimerEdit"

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

def TimerEditList__init__(self, session):
	Screen.__init__(self, session)

	list = [ ]
	self.list = list
	self.fillTimerList()

	print "EMPTY:",self.EMPTY
	print "ENABLE:",self.ENABLE
	print "DISABLE:",self.DISABLE
	print "CLEANUP:",self.CLEANUP
	print "DELETE:",self.DELETE

	self["timerlist"] = TimerList(list)

	self.key_red_choice = self.EMPTY
	self.key_yellow_choice = self.EMPTY
	self.key_blue_choice = self.EMPTY

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

	print "key_red_choice:",self.key_red_choice

	self["actions"] = ActionMap(["OkCancelActions", "DirectionActions", "ShortcutActions", "TimerEditActions"], 
		{
			"ok": self.openEdit,
			"cancel": self.leave,
			"green": self.addCurrentTimer,
			"log": self.showLog,
			"left": self.left,
			"right": self.right,
			"up": self.up,
			"down": self.down
		}, -1)
	self.session.nav.RecordTimer.on_state_change.append(self.onStateChange)
	self.onShown.append(self.updateState)

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

def TimerSanityConflict__init__(self, session, timer):
	Screen.__init__(self, session)
	self.timer = timer
	print "TimerSanityConflict"

	self["timer1"] = TimerList(self.getTimerList(timer[0]))
	self.list = []
	self.list2 = []
	count = 0
	for x in timer:
		if count != 0:
			self.list.append((_("Conflicting timer") + " " + str(count), x))
			self.list2.append((timer[count], False))
		count += 1
	if count == 1:
		self.list.append((_("Channel not in services list")))

	self["list"] = MenuList(self.list)
	self["timer2"] = TimerList(self.list2)

	self["key_red"] = StaticText("Edit")
	self["key_green"] = StaticText(" ")
	self["key_yellow"] = StaticText(" ")
	self["key_blue"] = StaticText(" ")

	self.key_green_choice = self.EMPTY
	self.key_yellow_choice = self.EMPTY
	self.key_blue_choice = self.EMPTY

	self["actions"] = ActionMap(["OkCancelActions", "DirectionActions", "ShortcutActions", "TimerEditActions"], 
		{
			"ok": self.leave_ok,
			"cancel": self.leave_cancel,
			"red": self.editTimer1,
			"up": self.up,
			"down": self.down
		}, -1)
	self.onShown.append(self.updateState) 

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

def doTimerEditChange():
	try:
		TimerEditList.__init__ = TimerEditList__init__
		TimerSanityConflict.__init__ = TimerSanityConflict__init__
		print "[PowerMetrixFHD] 016 -OK-- TimerEdit"
	except:
		print "[PowerMetrixFHD] 016 -NOK- TimerEdit"
