# -*- coding: utf-8 -*-
#
# ServiceReference to MovieName  - Converter
#
# Coded by dre (c) 2014 - 2018
# Support: www.dreambox.tools
# E-Mail: dre@dreambox.tools
#
# This Converter is open source but it is NOT free software.
#
# This Converter may only be distributed to and executed on hardware which
# is licensed by Dream Multimedia GmbH.
# In other words:
# It's NOT allowed to distribute any parts of this Converter or its source code in ANY way
# to hardware which is NOT licensed by Dream Multimedia GmbH.
# It's NOT allowed to execute this Converter and its source code or even parts of it in ANY way
# on hardware which is NOT licensed by Dream Multimedia GmbH.
#
# If you want to use or modify the code or parts of it,
# you have to keep MY license and inform me about the modifications by mail.
#

from Components.Converter.Converter import Converter
from Components.Element import cached
from enigma import eServiceReference, iPlayableServicePtr, iServiceInformation
from Components.Merlin import MerlinImage
from Components.ExternalMovieInfo import ExternalMovieInfo
from os import path as os_path

class RefToMovieName(Converter, object):
	def __init__(self, type):
		Converter.__init__(self, type)
		self.moviepath = ""

	@cached
	def getText(self):
		service = self.source.service
		ref = None
		name = ""
		if isinstance(service, iPlayableServicePtr):
			info = service and service.info()
			if info:
				ref = eServiceReference(info.getInfoString(iServiceInformation.sServiceref))
		else:
			ref = service
			if ref.flags & (eServiceReference.flagDirectory):
				self.moviepath = os_path.dirname(ref.getPath())
				return "folder"
		if ref:
			self.moviepath = os_path.dirname(ref.getPath())
			extension = ref.getPath().split('.')[-1].lower()
			if extension in MerlinImage.getAllExtensions():
				return ExternalMovieInfo.generateFilename(ref, "", "")
		return ""
		
	text = property(getText)

	@cached
	def getMoviePath(self):
		return self.moviepath

	def changed(self, what):
		Converter.changed(self, what)
