# -*- coding: utf-8 -*-
from Components.Converter.Converter import Converter
from enigma import iServiceInformation, iPlayableService, iPlayableServicePtr
from Components.Element import cached
from Components.config import config

#Dr.Best --> meta with Non-TS-Files
from enigma import eServiceReference
from Components.Merlin import MerlinImage
from merlin.emerlin import eMerlin

class ServiceName(Converter, object):
	NAME = 0
	PROVIDER = 1
	REFERENCE = 2
	NAMEORBITAL = 3
	ORBITAL = 4

	def __init__(self, type):
		if config.merlin2.skin_shownameorbital.value == "Orbital" and type not in ["Provider","Reference"]:
			type = "Orbital"
		elif config.merlin2.skin_shownameorbital.value == "NameOrbital" and type not in ["Provider","Reference"]:
			type = "NameOrbital"
		elif config.merlin2.skin_shownameorbital.value == "Name" and type not in ["Provider","Reference"]:
			type = "Name"
		Converter.__init__(self, type)
		if type == "Provider":
			self.type = self.PROVIDER
		elif type == "Reference":
			self.type = self.REFERENCE
		elif type == "NameOrbital":
			self.type = self.NAMEORBITAL
		elif type == "Orbital":
			self.type = self.ORBITAL
		else:
			self.type = self.NAME

	def getServiceInfoValue(self, info, what, ref=None):
		v = ref and info.getInfo(ref, what) or info.getInfo(what)
		if v != iServiceInformation.resIsString:
			ret = "N/A" if not ref or self.type != self.REFERENCE else ref.toString()
		elif ref:
			ret = info.getInfoString(ref, what) or self.type == self.REFERENCE and ref.toString()
		else:
			ret = info.getInfoString(what)
		return ret

	@cached
	def getText(self):
		service = self.source.service
		if isinstance(service, iPlayableServicePtr):
			info = service and service.info()
			ref = None
		else: # reference
			info = service and self.source.info
			ref = service
		if info is None:
			return ""
		if (self.type == self.NAME) or (self.type == self.NAMEORBITAL) or (self.type == self.ORBITAL):
			orb = ""
			if ref:
				transponder_info = info.getInfoObject(ref, iServiceInformation.sTransponderData)
			else:
				transponder_info = info.getInfoObject(iServiceInformation.sTransponderData)
			if transponder_info and "orbital_position" in transponder_info.keys():
				pos = int(transponder_info["orbital_position"])
				direction = 'E'
				if pos > 1800:
					pos = 3600 - pos
					direction = 'W'
					orb = "(%d.%d%s)" % (pos/10, pos%10, direction)
				elif pos > 0:				
					orb = "(%d.%d%s)" % (pos/10, pos%10, direction)
			name = ref and info.getName(ref)
			if name is None:
				name = info.getName()
			name = name.replace('\xc2\x86', '').replace('\xc2\x87', '')
			if self.type == self.NAMEORBITAL:
				return "%s %s" % (name, orb)
			elif (self.type == self.ORBITAL) and (transponder_info) and ("orbital_position" in transponder_info.keys()):
				orb = "%d.%d%s" % (pos/10, pos%10, direction)
				return "%s" % (orb)
			else: ##Dr.Best --> meta with Non-TS-Files
				if ref is None:
					ref = eServiceReference(info.getInfoString(iServiceInformation.sServiceref)) # for VideoDB --> even ts files with meta or eit was set with setName, use this
					_n = ref.getName()
					if _n:
						name = _n
					else:
						_name = MerlinImage.getMetaData(ref, MerlinImage.getName, False)
						if _name is not None:
							name = _name
						else:
							event = eMerlin.getInstance().getEvent(ref)
							if event and len(event.getEventName()):
								name = event.getEventName()
						
				return name
		elif self.type == self.PROVIDER:
			return self.getServiceInfoValue(info, iServiceInformation.sProvider, ref)
		elif self.type == self.REFERENCE:
			return self.getServiceInfoValue(info, iServiceInformation.sServiceref, ref)

	text = property(getText)

	def changed(self, what):
		if what[0] != self.CHANGED_SPECIFIC or what[1] in (iPlayableService.evStart,):
			Converter.changed(self, what)
