#
# SkinSettings - this is a part of Merlin3-Image
#
# Coded by dre (c) 2013
# Support: www.dreamboxtools.de
# E-Mail: dre@dreamboxtools.de
#
# This plugin is open source but it is NOT free software.
#
# This plugin 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 plugin or its source code in ANY way
# to hardware which is NOT licensed by Dream Multimedia GmbH.
# It's NOT allowed to execute this plugin 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 __future__ import print_function
from Components.Button import Button
from Components.ActionMap import HelpableActionMap, ActionMap
from Components.ConfigList import ConfigListScreen
from Components.config import config, getConfigListEntry
from Components.Sources.StaticText import StaticText
from Screen import Screen
from Screens.HelpMenu import HelpableScreen
from Screens.MessageBox import MessageBox
from Screens.Standby import TryQuitMainloop
from Tools.Directories import fileExists
from Tools.HardwareInfo import HardwareInfo
import xml.etree.cElementTree

CONVERTERDIR = "/usr/lib/enigma2/python/Components/Converter/"
RENDERERDIR  = "/usr/lib/enigma2/python/Components/Renderer/"

class SkinSettingsSetup(Screen, HelpableScreen, ConfigListScreen):
	skin = """
		<screen name="SkinSettingsSetup" position="center,center" size="600,400" title="Skin Settings" >
			<ePixmap pixmap="skin_default/buttons/red.png" position="0,0" zPosition="0" size="140,40" transparent="1" alphatest="on" />
			<ePixmap pixmap="skin_default/buttons/green.png" position="140,0" zPosition="0" size="140,40" transparent="1" alphatest="on" />
			<ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" zPosition="0" size="140,40" transparent="1" alphatest="on" />
			<ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" zPosition="0" size="140,40" transparent="1" alphatest="on" />
			<widget render="Label" source="key_red" position="0,0" size="140,40" zPosition="5" valign="center" halign="center" backgroundColor="red" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
			<widget render="Label" source="key_green" position="140,0" size="140,40" zPosition="5" valign="center" halign="center" backgroundColor="red" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
			<widget name="config" position="20,50" size="560,330" scrollbarMode="showOnDemand" />
		</screen>"""

	def __init__(self, session):
		Screen.__init__(self, session)
		HelpableScreen.__init__(self)
		self.setTitle(_("Skin Settings"))

		self["SetupActions"] = HelpableActionMap(self, "SetupActions",
		{
			"cancel":	(self.keyCancel, _("Discard changes and close")),
		}, -2)
		
		self["ColorActions"] = HelpableActionMap(self, "ColorActions",
		{
			"green":	(self.keySave, _("Save settings")),
			"red":		(self.keyCancel, _("Discard changes and close")),
		}, -2)
		
		self["key_blue"] = StaticText("")
		self["key_yellow"] = StaticText("")
		self["key_red"] = StaticText(_("Cancel"))
		self["key_green"] = StaticText(_("OK"))
		
		self.list = []
		ConfigListScreen.__init__(self, self.list, session = session)
		
		self.createConfig()
	
	def keyLeft(self):
		ConfigListScreen.keyLeft(self)
		cur = self["config"].getCurrent()
		if cur and (cur[0] == _("Show picon") or cur[0] == _("Simple decodeinfo") or cur[0] == _("Show FTA") or cur[0] == _("Show decryption")):
			self.createConfig()
		
	def keyRight(self):
		ConfigListScreen.keyRight(self)
		cur = self["config"].getCurrent()
		if cur and (cur[0] == _("Show picon") or cur[0] == _("Simple decodeinfo") or cur[0] == _("Show FTA") or cur[0] == _("Show decryption")):
			self.createConfig()
	
	def createConfig(self, configElement=""):
		self.list = []

		self.list.append((_("General"),))
		self.list.append(getConfigListEntry(_("Disable alternate row color"), config.merlin2.disable_alternate_row_color))
		self.list.append((_("Infobar"),))
		self.list.append(getConfigListEntry(_("Show SNR"), config.merlin2.skin_showsnrindb))
		self.list.append(getConfigListEntry(_("Name / Orbital display"), config.merlin2.skin_shownameorbital))
		if fileExists(RENDERERDIR + "ChannelNumber.pyo"):
			self.list.append(getConfigListEntry(_("Show channel number"), config.merlin2.skin_showchannelnumber))
		else:
			config.merlin2.skin_showchannelnumber.value = False
		if fileExists(RENDERERDIR + "Picon.pyo"):
			self.list.append(getConfigListEntry(_("Show picon"), config.merlin2.skin_showpicon))
			if config.merlin2.skin_showpicon.value == True:
				self.list.append(getConfigListEntry(_("Picon mode"), config.merlin2.skin_piconmode))			
		self.list.append((_("Channel Selection"),))
		if fileExists(RENDERERDIR + "ChannelSelectionTitle.pyo"):
			self.list.append(getConfigListEntry(_("Channellist header style"), config.merlin2.skin_channellistheader))
		else:
			config.merlin2.skin_channellistheader.value = "0"
		self.list.append((_("Menu"),))
		self.list.append(getConfigListEntry(_("Show menu icon"), config.merlin2.skin_showmenuicon))
		self.list.append(getConfigListEntry(_("Ignore skin plugin icons"), config.merlin2.skin_ignoreskinpluginicons))
		self.list.append((_("Encryption/decryption info"),))
		self.list.append(getConfigListEntry(_("Show camd name"), config.merlin2.skin_showcamdname))
		self.list.append(getConfigListEntry(_("Show encryption"), config.merlin2.skin_showencryption))
		self.list.append(getConfigListEntry(_("Show decryption"), config.merlin2.skin_showdecryption))
		if config.merlin2.skin_showdecryption.value:
			self.list.append(getConfigListEntry(_("Show FTA"), config.merlin2.skin_showfta))
			if config.merlin2.skin_showfta.value:
				self.list.append(getConfigListEntry(_("FTA text"), config.merlin2.skin_ftatext))
			self.list.append(getConfigListEntry(_("Simple decodeinfo"), config.merlin2.skin_simpledecodeinfo))
			if not config.merlin2.skin_simpledecodeinfo.value:
				self.list.append(getConfigListEntry(_("Reader / source display"), config.merlin2.skin_readersource))
		else:
			config.merlin2.skin_showfta.value = False
			config.merlin2.skin_simpledecodeinfo.value = False

		if HardwareInfo().get_device_name() in ('dm820', 'dm7080', 'two'):
			self.list.append((_("Display"),))
			self.list.append(getConfigListEntry(_("Show picon in display"), config.skin.show_display_picon))
			
		self["config"].list = self.list
		self["config"].l.setList(self.list)

	def keySave(self):
		restartRequired = False
		for x in self["config"].list:
			if len(x) > 1:
				if x[1].isChanged() and x[0] in (_("Show SNR"),_("Name / Orbital display"),_("Channellist header style"), _("Ignore skin plugin icons")):
					restartRequired = True
				x[1].save()
				
		if restartRequired:
			info = self.session.openWithCallback(self.restartGUI,MessageBox,_("Please restart enigma2 for changes to take effect. Restart now?"), MessageBox.TYPE_YESNO)
			info.setTitle(_("Skin Settings"))
		else:
			self.close()
		
	def keyCancel(self):
		for x in self["config"].list:
			if len(x) > 1:
				x[1].cancel()
		config.merlin2.skin_showfta.cancel()
		config.merlin2.skin_piconmode.cancel()
		config.merlin2.skin_ftatext.cancel()
		config.merlin2.skin_simpledecodeinfo.cancel()
		
		self.close()

		
	def restartGUI(self, answer):
		if answer:
			self.session.open(TryQuitMainloop, 3)
		else:
			self.close()
