# -*- coding: UTF-8 -*-

from Screens.Screen import Screen
from Screens.TextBox import TextBox
from API import api
from Components.About import about
from Components.ActionMap import ActionMap
from Components.config import config
from Components.Harddisk import harddiskmanager
from Components.NimManager import nimmanager
from Components.Sources.StaticText import StaticText
from Tools.Directories import resolveFilename, SCOPE_CURRENT_SKIN, SCOPE_PLUGINS, fileExists
from Tools.DreamboxHardware import getFPVersion
from Tools.Log import Log
from enigma import eNetworkManager

try:
	from Screens.About import About
	print "[PowerMetrixFHD] 023 -OK-- About"
except:
	print "[PowerMetrixFHD] 023 -NOK- About"

def getTextFromFile(name = "about.txt", scope = SCOPE_CURRENT_SKIN):
	text = ""
	filename = resolveFilename(scope, name)
	if fileExists(filename):
		file = open(filename, "r")
		for line in file:
			text += line
		file.close()
	return text

skinAboutText = getTextFromFile("about.txt", SCOPE_CURRENT_SKIN)
imageAboutText = "Image: " + about.getImageVersionString() + "\n\n" + getTextFromFile("PowerSat/about.txt", SCOPE_PLUGINS)

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

def About__init__(self, session):
	Screen.__init__(self, session)

	self["Model"] = StaticText(api.enigma2.systeminfo.modelname())
	self["EnigmaVersion"] = StaticText("Dreambox OS: " + about.getEnigmaVersionString())
	self["ImageVersion"] = StaticText("Image: " + about.getImageVersionString())

	self["TunerHeader"] = StaticText(_("Detected NIMs:"))

	fp_version = getFPVersion()
	if fp_version is None:
		fp_version = ""
	else:
		fp_version = _("Frontprocessor version: %d") % fp_version

	self["FPVersion"] = StaticText(fp_version)

	nims = nimmanager.nimList()
	for count in (0, 1, 2, 3, 4):
		if count < len(nims):
			self["Tuner" + str(count)] = StaticText(nims[count])
		else:
			self["Tuner" + str(count)] = StaticText("")

	self["HDDHeader"] = StaticText(_("Detected HDD:"))
	hdd = None
	hddlist = harddiskmanager.HDDList()
	defaultDisk = harddiskmanager.getDefaultStorageDevicebyUUID(config.storage_options.default_device.value)
	if defaultDisk:
		default_hdd = "/dev/%s" %(defaultDisk.device,)
		for hd in hddlist:
			hd = hd[1]
			if default_hdd.startswith(hd.getDeviceDir()):
				Log.i("Default HDD matched! (%s -> %s)" %(default_hdd,hd.getDeviceDir()))
				hdd = hd
				break
	if not hdd:
		hdd = hddlist and hddlist[0][1] or None
		Log.w("No default Harddisk found. Falling back to first in list -> %s" %(hdd,))

	if hdd is not None and hdd.model() != "":
		self["hddA"] = StaticText(_("%s\n(%s, %d MB free)") % (hdd.model(), hdd.capacity(),hdd.free()))
	else:
		self["hddA"] = StaticText(_("none"))

	self["IPHeader"] = StaticText(_("Current IP Address:"))

	ipA = _("none")
	services = eNetworkManager.getInstance().getServices()
	for service in services:
		ip = self.getServiceIP(service)
		name = service.name()
		if ip != None:
			ipA = "%s (%s)" %(ip, name)
	self["ipA"] = StaticText(ipA)

	self["key_red"] = StaticText(_("Close"))
	self["key_green"] = StaticText(_("Translation info"))
	self["key_yellow"] = StaticText(_("Skin info"))
	self["key_blue"] = StaticText(_("Image info"))

	self["actions"] = ActionMap(["SetupActions", "ColorActions"],
		{
			"cancel": self.close,
			"ok": self.close,
			"green": self.showTranslationInfo,
			"yellow": self.showSkinInfo,
			"blue": self.showImageInfo
		})

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

def AboutshowSkinInfo(self):
	text = skinAboutText
	self.session.open(SkinInfo, text)

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

def AboutshowImageInfo(self):
	text = imageAboutText
	self.session.open(ImageInfo, text)

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

class SkinInfo(TextBox):
	def __init__(self, session, text = ""):
		TextBox.__init__(self, session, text)

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

class ImageInfo(TextBox):
	def __init__(self, session, text = ""):
		TextBox.__init__(self, session, text)

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

def doAboutChange():
	try:
		About.showSkinInfo = AboutshowSkinInfo
		About.showImageInfo = AboutshowImageInfo
		About.__init__ = About__init__
		print "[PowerMetrixFHD] 023 -OK-- About"
	except:
		print "[PowerMetrixFHD] 023 -NOK- About"
