from Components.config import config
from Components.InputDevice import iInputDevices
from Tools.Profile import profile
from Screens.ChoiceBox import ChoiceBox
from Screens.MessageBox import MessageBox
from GlobalActions import globalActionMap
from enigma import eActionMap, eInputDeviceManager

profile("KeySimulator")

class KeySimulator:
	""" KeySimulator handles Audio long press to access a choice box screen to select action for buttons missing on bluetooth remote control """
	def __init__(self, session):
		globalActionMap.actions["keySelection"]=self.keySelection
		
		self.session = session
		
		self.actionMapInstance = eActionMap.getInstance()	
		
		self.matchList = ["showTv", "keyTv", "keyRadio"]
		
		# this is just a fallback in case someone uses an old rc
		# rcused 0: remote control w/o audio key (aka advanced)
		# rcused 1: remote control with audio key and w/o movie keys (aka non-advanced)
		# rcused 2: remote control with audio key and movie keys (aka advanced)
		rcDict = { 0: "dreambox advanced remote control (native)", 1: "dreambox remote control (native)", 2: "dreambox advanced remote control (native)"}
		
		self.advancedRc = config.misc.rcused.value in (0, 2)
		
		self.rcType = rcDict.get(config.misc.rcused.value)
		
	def keySelection(self):
		choiceList = []
		
		showTvKey = False
		showRadioKey = False
		showTvKeyLong = False
		showRadioKeyLong = False
		keySent = False
		
		activeComps = self.session.current_dialog.__dict__.get('active_components')
		if activeComps is not None:
			activeCompsList = [ x.__dict__ for x in activeComps ]
			for activeComp in activeCompsList:
				if activeComp.get('actions') is not None:
					dictKeysList = activeComp.get('actions').keys()
					if "showTv" in dictKeysList or "keyTV" in dictKeysList:
						showTvKey = True
					if "showRadio" in dictKeysList or "keyRadio" in dictKeysList:
						showRadioKey = True
					for context in activeComp.get('contexts', []):
						if context == "InfobarSubserviceQuickzapActions":
							if "exit" in dictKeysList:
								showTvKey = True
						elif context == "InfobarTimeshiftActions":
							if "timeshiftStop" in dictKeysList:
								showTvKey = True
						elif context == "MoviePlayerActions":
							if "leavePlayer" in dictKeysList:
								showTvKey = True
						elif context == "MediaPlayerActions" and not self.advancedRc:
							if "stop" in dictKeysList:
								showTvKey = True
							if "shift_stop" in dictKeysList:
								showTvKeyLong = True
							if "shift_record" in dictKeysList:
								showRadioKeyLong = True

		# there's help defined
		if len(self.session.current_dialog.__dict__.get('helpList', [])):
			choiceList.append((_("Help"), "138", False))
		if showTvKey:
			choiceList.append(((_("TV"), "377", False)))
		if showTvKeyLong:
			choiceList.append(((_("TV (long)"), "377", True)))
		if showRadioKey:
			choiceList.append(((_("Radio"), "385", False)))
		if showRadioKeyLong:
			choiceList.append(((_("Radio (long)"), "385", True)))
		
		if len(choiceList) > 1:
			self.session.openWithCallback(self.keySelected, ChoiceBox, list = choiceList, windowTitle=_("Please select key to simulate"))
		# there's only one entry in choiceList - call it directly
		elif len(choiceList) == 1:
			self.keySelected(choiceList[0])
		else:
			if not keySent:
				self.session.open(MessageBox, _("No help available"), MessageBox.TYPE_INFO, timeout=5)
		
	def keySelected(self, ret=None):
		if ret:
			rcCode = int(ret[1])
			self.actionMapInstance.keyPressed(self.rcType, rcCode, 0) #press the button
			if ret[2]:
				self.actionMapInstance.keyPressed(self.rcType, rcCode, 3) #long press the button
			self.actionMapInstance.keyPressed(self.rcType, rcCode, 1) #release the button