from Screens.Screen import Screen
from Components.ActionMap import ActionMap
from Components.Label import Label
from Components.Pixmap import Pixmap
from Components.Sources.List import List
from Components.Sources.StaticText import StaticText
from enigma import eNetworkManager, eNetworkService, eNetworkServicePtr

try:
	from Plugins.SystemPlugins.NetworkManager.NetworkConfig import NetworkServiceConfig, NetworkConfigGeneral
	print "[PowerMetrixFHD] 009 -OK-- NetworkManager"
except:
	print "[PowerMetrixFHD] 009 -NOK- NetworkManager"

#######################################################################################################################################################

def NetworkServiceConfig__init__(self, session):
	Screen.__init__(self, session)
	NetworkConfigGeneral.__init__(self)

	self["key_red"] = Label(_("Reset"))
	self["key_green"] = Label(_("Scan"))
	self["key_yellow"] = Label(_("IP"))
	self["key_blue"] = Label(_("DNS"))
	self["pixmap_red"] = Pixmap()
	self["pixmap_green"] = Pixmap()
	self["pixmap_yellow"] = Pixmap()
	self["pixmap_blue"] = Pixmap()
	self["hint"] = Label(_("Press OK to connect"))
	self["details_label"] = Label(_("Active Connection"))
	self["details"] = StaticText("")
	self["OkCancelActions"] = ActionMap(["OkCancelActions", "ColorActions", "MenuActions"],
	{
		"menu": self._menu,
		"cancel": self.close,
		"ok" : self._ok,
		"green": self._rescan,
		"red": self._removeService
	}, -3)
	self["ServiceActions"] = ActionMap(["ColorActions"],
	{
		"yellow": self._configIP,
		"blue": self._configDNS,
	}, -2)

	self["list"] = self._services
	self["summary_list"] = StaticText("")
	self._hasWireless = False

	self._services.buildfunc = self._buildListEntry
	self._services.onSelectionChanged.append(self._selectionChanged)

	self._addNotifiers()
	self._createSetup()

	self.onClose.append(self._onClose)
	self.onLayoutFinish.append(self.layoutFinished)

#######################################################################################################################################################

def _NetworkServiceConfigcheckButtons(self):
	self["hint"].setText("")
	if isinstance(self._currentService, eNetworkServicePtr): #a service
		self["key_green"].hide()
		self["pixmap_green"].hide()
		if self._currentService.connected():
			self["hint"].setText(_("Press OK to disconnect"))
		else:
			self["hint"].setText(_("Press OK to connect"))
		if self._currentService.favorite():
			self["key_red"].show()
			self["pixmap_red"].show()
		else:
			self["key_red"].hide()
			self["pixmap_red"].hide()
		self["key_yellow"].show()
		self["pixmap_yellow"].show()
		self["key_blue"].show()
		self["pixmap_blue"].show()
		self["ServiceActions"].setEnabled(True)
	else: #a technology
		if self._currentService and self._currentService.type() == eNetworkService.TYPE_WIFI:
			self["key_green"].show()
			self["pixmap_green"].show()
		else:
			self["key_green"].hide()
			self["pixmap_green"].hide()
		self["key_red"].hide()
		self["pixmap_red"].hide()
		if self._currentService:
			if self._currentService.powered():
				self["hint"].setText(_("Press OK to disable"))
			else:
				self["hint"].setText(_("Press OK to enable"))
		else:
			self["hint"].setText("")
		self["key_yellow"].hide()
		self["pixmap_yellow"].hide()
		self["key_blue"].hide()
		self["pixmap_blue"].hide()
		self["ServiceActions"].setEnabled(False)

#######################################################################################################################################################

def doNetworkManagerChange():
	try:
		NetworkServiceConfig.__init__ = NetworkServiceConfig__init__
		NetworkServiceConfig._checkButtons = _NetworkServiceConfigcheckButtons
		print "[PowerMetrixFHD] 009 -OK-- NetworkManager"
	except:
		print "[PowerMetrixFHD] 009 -NOK- NetworkManager"
