from Components.config import config
from Components.Network import iNetworkInfo

try:
	from Plugins.SystemPlugins.StreamServer.StreamServerConfig import StreamServerConfig
	print "[PowerMetrixFHD] 005 -OK-- StreamServer"
except:
	print "[PowerMetrixFHD] 005 -NOK- StreamServer"

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

def StreamServerConfig_setInfoText(self):
	if not self._streamServerControl.isConnected():
		self._info.setText(_("ERROR: Streaming Server not available!"))
		return

	detailtext = _("Local client(s): %s") %(self._clientCount)
	self._details.setText(detailtext)

	localstreams = []
	if config.streamserver.rtsp.enabled.value or config.streamserver.hls.enabled.value:
		ifaces = iNetworkInfo.getConfiguredInterfaces()
		for iface in ifaces.itervalues():
			ip = iface.getIpv4()
			if not ip:
				ip = iface.getIpv6()
			if ip:
				if config.streamserver.rtsp.enabled.value:
					localstreams.append("rtsp://%s:%s/%s" %(ip.getAddress(), config.streamserver.rtsp.port.value, config.streamserver.rtsp.path.value))
				if config.streamserver.hls.enabled.value:
					localstreams.append("http://%s:%s/dream.m3u8" %(ip.getAddress(), config.streamserver.hls.port.value))
				break

	infotext = ""
	if not localstreams:
		infotext = _("no active streams...")
	else:
		infotext = "\n".join(localstreams)
	self._info.setText(infotext)

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

def doStreamServerChange():
	try:
		StreamServerConfig._setInfoText = StreamServerConfig_setInfoText
		print "[PowerMetrixFHD] 005 -OK-- StreamServer"
	except:
		print "[PowerMetrixFHD] 005 -NOK- StreamServer"
