from __future__ import absolute_import
from Screens.Screen import Screen
from Components.config import config, getConfigListEntry
from Components.ConfigList import ConfigListScreen
from Components.ActionMap import ActionMap
from Components.Sources.StaticText import StaticText
from Tools.BoundFunction import boundFunction
from Components.MerlinPictureViewerWidget import MerlinPictureViewer
from enigma import getDesktop

class MerlinPictureViewerConfigScreen(ConfigListScreen, Screen):

	IS_DIALOG = True	

	def __init__(self, session):	
		self.session = session
		Screen.__init__(self, session)
		self.title = _("Merlin PictureView Config")
		self["actions"] = ActionMap(["SetupActions", "ColorActions"],
		{
			"green": self.keySave,
			"red": self.keyCancel,
			"cancel": self.keyCancel,
		}, -2)
		self["key_red"] = StaticText(_("Cancel"))
		self["key_green"] = StaticText(_("OK"))
		self.list = [ ]
		self.list.append(getConfigListEntry(_("Enable Ken Burns effect"), config.merlin2.merlinpictureviewer_kenburns))
		self.list.append(getConfigListEntry(_("Display time in seconds"), config.merlin2.merlinpictureviewer_picture_duration))
		self.list.append(getConfigListEntry(_("Transition mode"), config.merlin2.merlinpictureviewer_transition_mode))
		self.list.append(getConfigListEntry(_("use default transition time for modes"), config.merlin2.merlinpictureviewer_transition_default_duration))
		self.list.append(getConfigListEntry(_("Transition time in milliseconds (if default is disabled)"), config.merlin2.merlinpictureviewer_transition_duration))
		self.list.append(getConfigListEntry(_("Scale to screen"), config.merlin2.merlinpictureviewer_picture_scale_to_screen))
		ConfigListScreen.__init__(self, self.list, session)

	def keySave(self):
		for x in self["config"].list:
			x[1].save()
		self.close(True)

	def keyCancel(self):
		ConfigListScreen.cancelConfirm(self, True)

class MerlinPictureViewerScreen(Screen):
	
	def __init__(self, session, list, index):
		Screen.__init__(self, session)
		self.session = session
		self.list = list
		self.index = index
		self.lastservice = self.session.nav.getCurrentlyPlayingServiceReference()
		self.session.nav.stopService()
		config.merlin2.merlinpictureviewer_transition_mode.addNotifier(self.transitionModeConfigOnChange, initial_call = False)
		config.merlin2.merlinpictureviewer_transition_duration.addNotifier(self.transitionDurationConfigOnChange, initial_call = False)
		config.merlin2.merlinpictureviewer_picture_scale_to_screen.addNotifier(self.scaleToScreenConfigOnChange, initial_call = False)
		self["actions"] = ActionMap(["OkCancelActions", "InfobarActions", "DirectionActions", "MediaPlayerActions", "MediaPlayerSeekActions", "EPGSelectActions"], 
		{
			"cancel": self.closed_pressed,
			"right": boundFunction(self.skipImage,1),
			"left": boundFunction(self.skipImage,-1),
			"pause": self.pause,
			"seekBack": boundFunction(self.skipImage,-1),
			"seekFwd": boundFunction(self.skipImage,1),
			"input_date_time": self.menu_pressed,			

		}, -1)
		self["image"] = MerlinPictureViewer()
		self.onLayoutFinish.append(self.startSlideShow)
		self.onClose.append(self.onCloseMerlinPictureViewerScreen)

	def onCloseMerlinPictureViewerScreen(self):
		self.session.nav.playService(self.lastservice)
		config.merlin2.merlinpictureviewer_transition_mode.removeNotifier(self.transitionModeConfigOnChange)
		config.merlin2.merlinpictureviewer_transition_duration.removeNotifier(self.transitionDurationConfigOnChange)
		config.merlin2.merlinpictureviewer_picture_scale_to_screen.removeNotifier(self.scaleToScreenConfigOnChange)

	def startSlideShow(self):
		self["image"].setTransitionMode(int(config.merlin2.merlinpictureviewer_transition_mode.value))
		self["image"].scaleToScreen(1 if config.merlin2.merlinpictureviewer_picture_scale_to_screen.value else 0)
		self["image"].setTransitionDuration(-1 if config.merlin2.merlinpictureviewer_transition_default_duration.value else config.merlin2.merlinpictureviewer_transition_duration.value)
		self["image"].startSlideShow(self.list, self.index, config.merlin2.merlinpictureviewer_picture_duration.value * 1000, config.merlin2.merlinpictureviewer_kenburns.value)

	def pause(self):
		self["image"].setState()

	def skipImage(self, direction):
		self["image"].skipImage(direction)

	def closed_pressed(self):
		self.close(self["image"].getCurrentFilename())

	def transitionModeConfigOnChange(self, configElement = None):
		self["image"].setTransitionMode(int(config.merlin2.merlinpictureviewer_transition_mode.value))

	def transitionDurationConfigOnChange(self, configElement = None):
		self["image"].setTransitionDuration(-1 if config.merlin2.merlinpictureviewer_transition_default_duration.value else config.merlin2.merlinpictureviewer_transition_duration.value)

	def scaleToScreenConfigOnChange(self, configElement = None):
		self["image"].scaleToScreen(1 if config.merlin2.merlinpictureviewer_picture_scale_to_screen.value else 0)

	def menu_pressed(self):
		self.session.openWithCallback(self.configCallback, MerlinPictureViewerConfigScreen)

	def configCallback(self, result = False):
		if result:
			self.index = self["image"].getCurrentIndex()
			self.startSlideShow()

	@staticmethod
	def isValidPicture(filename):
		return filename.lower().endswith((".jpg",".jpeg", ".png",".bmp", ".gif"))
