from Screens.Screen import Screen
from Screens.HelpMenu import HelpableScreen
from Components.ActionMap import NumberActionMap, HelpableActionMap
from Components.Button import Button
from Components.FileList import FileList
from Components.Label import Label
from Components.MenuList import MenuList
from Components.Pixmap import Pixmap
from Components.Sources.StaticText import StaticText
from Tools.BoundFunction import boundFunction
from Tools.NumericalTextInput import NumericalTextInput
from enigma import eTimer

try:
	from Screens.LocationBox import LocationBox
	print "[PowerMetrixFHD] 013 -OK-- LocationBox"
except:
	print "[PowerMetrixFHD] 013 -NOK- LocationBox"

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

def LocationBox__init__(self, session, text = "", filename = "", currDir = None, bookmarks = None, userMode = False, windowTitle = _("Select Location"), minFree = None, autoAdd = False, editDir = False, inhibitDirs = [], inhibitMounts = []):
	# Init parents
	Screen.__init__(self, session)
	NumericalTextInput.__init__(self, handleTimeout = False)
	HelpableScreen.__init__(self)

	# Set useable chars
	self.setUseableChars(u'1234567890abcdefghijklmnopqrstuvwxyz')

	# Quickselect Timer
	self.qs_timer = eTimer()
	self.qs_timer_conn = self.qs_timer.timeout.connect(self.timeout)
	self.qs_timer_type = 0

	# Initialize Quickselect
	self.curr_pos = -1
	self.quickselect = ""

	# Set Text
	self["title"] = StaticText(text)
	self["textbook"] = Label(_("Bookmarks"))

	# Save parameters locally
	self.text = text
	self.filename = filename
	self.minFree = minFree
	self.realBookmarks = bookmarks
	self.bookmarks = bookmarks and bookmarks.value[:] or []
	self.userMode = userMode
	self.autoAdd = autoAdd
	self.editDir = editDir
	self.inhibitDirs = inhibitDirs

	# Initialize FileList
	self["filelist"] = FileList(currDir, showDirectories = True, showFiles = False, inhibitMounts = inhibitMounts, inhibitDirs = inhibitDirs)

	# Initialize BookList
	self["booklist"] = MenuList(self.bookmarks)

	# Buttons
	self["key_green"] = Button(_("OK"))
	self["key_yellow"] = Button(_("Rename"))
	self["key_blue"] = Button(_("Remove Bookmark"))
	self["key_red"] = Button(_("Cancel"))

	# Background for Buttons
	self["green"] = Pixmap()
	self["yellow"] = Pixmap()
	self["blue"] = Pixmap()
	self["red"] = Pixmap()

	# Initialize Target
	self["target"] = Label()

	if self.userMode:
		self.usermodeOn()

	# Custom Action Handler
	class LocationBoxActionMap(HelpableActionMap):
		def __init__(self, parent, context, actions = { }, prio=0):
			HelpableActionMap.__init__(self, parent, context, actions, prio)
			self.box = parent

		def action(self, contexts, action):
			# Reset Quickselect
			self.box.timeout(force = True)

			return HelpableActionMap.action(self, contexts, action)

	# Actions that will reset quickselect
	self["WizardActions"] = LocationBoxActionMap(self, "WizardActions",
		{
			"left": self.left,
			"right": self.right,
			"up": self.up,
			"down": self.down,
			"ok": (self.ok, _("select")),
			"back": (self.cancel, _("Cancel")),
		}, -2)

	self["ColorActions"] = LocationBoxActionMap(self, "ColorActions",
		{
			"red": self.cancel,
			"green": self.select,
			"yellow": self.changeName,
			"blue": self.addRemoveBookmark,
		}, -2)

	self["EPGSelectActions"] = LocationBoxActionMap(self, "EPGSelectActions",
		{
			"prevBouquet": (self.switchToBookList, _("switch to bookmarks")),
			"nextBouquet": (self.switchToFileList, _("switch to filelist")),
		}, -2)

	self["MenuActions"] = LocationBoxActionMap(self, "MenuActions",
		{
			"menu": (self.showMenu, _("menu")),
		}, -2)

	# Actions used by quickselect
	self["NumberActions"] = NumberActionMap(["NumberActions"],
	{
		"1": self.keyNumberGlobal,
		"2": self.keyNumberGlobal,
		"3": self.keyNumberGlobal,
		"4": self.keyNumberGlobal,
		"5": self.keyNumberGlobal,
		"6": self.keyNumberGlobal,
		"7": self.keyNumberGlobal,
		"8": self.keyNumberGlobal,
		"9": self.keyNumberGlobal,
		"0": self.keyNumberGlobal
	})

	# Run some functions when shown
	self.onShown.extend((
		boundFunction(self.setTitle, windowTitle),
		self.updateTarget,
		self.showHideRename,
	))

	self.onLayoutFinish.append(self.switchToFileListOnStart)

	# Make sure we remove our callback
	self.onClose.append(self.disableTimer)

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

def doLocationBoxChange():
	try:
		LocationBox.__init__ = LocationBox__init__
		print "[PowerMetrixFHD] 013 -OK-- LocationBox"
	except:
		print "[PowerMetrixFHD] 013 -NOK- LocationBox"
