from Screens.Screen import Screen
from Components.ActionMap import ActionMap
from Components.Label import Label
from Components.ScrollLabel import ScrollLabel
from Components.Sources.ServiceEvent import ServiceEvent
from Components.Sources.StaticText import StaticText
from enigma import eEPGCache, eTimer, eServiceReference

try:
	from Screens.EventView import EventViewBase
	print "[PowerMetrixFHD] 011 -OK-- EventView"
except:
	print "[PowerMetrixFHD] 011 -NOK- EventView"

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

def EventViewBase__init__(self, Event, Ref, callback=None, similarEPGCB=None):
	self.similarEPGCB = similarEPGCB
	self.cbFunc = callback
	self.currentService=Ref
	self.isRecording = (not Ref.ref.flags & eServiceReference.isGroup) and Ref.ref.getPath() and Ref.ref.getPath()[0] == '/'
	self.event = Event
	self["Service"] = ServiceEvent()
	self["epg_description"] = ScrollLabel()
	self["datetime"] = Label()
	self["channel"] = Label()
	self["duration"] = Label()
	self["key_red"] = StaticText("")
	if similarEPGCB is not None:
		self.SimilarBroadcastTimer = eTimer()
		self.SimilarBroadcastTimer_conn = self.SimilarBroadcastTimer.timeout.connect(self.getSimilarEvents)
	else:
		self.SimilarBroadcastTimer = None
	self.key_green_choice = self.ADD_TIMER
	if self.isRecording:
		self["key_green"] = StaticText("")
	else:
		self["key_green"] = StaticText(_("Add timer"))
	self["key_yellow"] = StaticText("")
	self["key_blue"] = StaticText("")
	self["actions"] = ActionMap(["OkCancelActions", "EventViewActions"],
		{
			"cancel": self.close,
			"ok": self.close,
			"pageUp": self.pageUp,
			"pageDown": self.pageDown,
			"prevEvent": self.prevEvent,
			"nextEvent": self.nextEvent,
			"timerAdd": self.timerAdd,
			"openSimilarList": self.openSimilarList
		})
	self.onShown.append(self.onCreate)

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

def EventViewBaseonCreate(self):
	self.setTitle(_("Event view"))
	self.setService(self.currentService)
	self.setEvent(self.event)

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

def EventViewBasesetEvent(self, event):
	self.event = event
	if event is None:
		return
	text = event.getEventName()
	short = event.getShortDescription()
	ext = event.getExtendedDescription()
	if short and short != text:
		text += "\n\n" + short
	if ext:
		if text:
			text += "\n\n"
		text += ext

	#self.setTitle(event.getEventName())
	self["epg_description"].setText(text)
	self["datetime"].setText(event.getBeginTimeString())
	self["duration"].setText(_("%d min")%(event.getDuration()/60))
	self["key_red"].setText("")
	if self.SimilarBroadcastTimer is not None:
		self.SimilarBroadcastTimer.start(400,True)

	serviceref = self.currentService
	eventid = self.event.getEventId()
	refstr = serviceref.ref.toString()
	isRecordEvent = False
	for timer in self.session.nav.RecordTimer.timer_list:
		if timer.eit == eventid and timer.service_ref.ref.toString() == refstr:
			isRecordEvent = True
			break
	if isRecordEvent and self.key_green_choice != self.REMOVE_TIMER:
		self["key_green"].setText(_("Remove timer"))
		self.key_green_choice = self.REMOVE_TIMER
	elif not isRecordEvent and self.key_green_choice != self.ADD_TIMER:
		self["key_green"].setText(_("Add timer"))
		self.key_green_choice = self.ADD_TIMER

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

def doEventViewChange():
	try:
		EventViewBase.__init__ = EventViewBase__init__
		EventViewBase.onCreate = EventViewBaseonCreate
		EventViewBase.setEvent = EventViewBasesetEvent
		print "[PowerMetrixFHD] 011 -OK-- EventView"
	except:
		print "[PowerMetrixFHD] 011 -NOK- EventView"
