from Components.Harddisk import harddiskmanager
from config import ConfigSubsection, ConfigYesNo, config, ConfigSelection, ConfigText, ConfigNumber, ConfigSet, ConfigLocations, ConfigInteger, ConfigSlider, ConfigOnOff, ConfigPosition, ConfigIP
from Tools.Directories import resolveFilename, SCOPE_HDD, fileExists
from enigma import setTunerTypePriorityOrder, eEnv
from SystemInfo import SystemInfo
from os import listdir as os_listdir

def BaseInitUsageConfig():
	config.usage = ConfigSubsection();

	config.usage.keymap = ConfigText(default = eEnv.resolve("${datadir}/enigma2/keymap.xml"))

	config.usage.setup_level = ConfigSelection(default = "intermediate", choices = [
		("simple", _("Simple")),
		("intermediate", _("Intermediate")),
		("expert", _("Expert")) ])

	config.seek = ConfigSubsection()
	config.seek.selfdefined_13 = ConfigNumber(default=15)
	config.seek.selfdefined_46 = ConfigNumber(default=60)
	config.seek.selfdefined_79 = ConfigNumber(default=300)

	config.seek.speeds_forward = ConfigSet(default=[2, 4, 8, 16, 32, 64, 128], choices=[2, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128])
	config.seek.speeds_backward = ConfigSet(default=[2, 4, 8, 16, 32, 64, 128], choices=[1, 2, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128])
	config.seek.speeds_slowmotion = ConfigSet(default=[2, 4, 8], choices=[2, 4, 6, 8, 12, 16, 25])

	config.seek.enter_forward = ConfigSelection(default = "2", choices = ["2", "4", "6", "8", "12", "16", "24", "32", "48", "64", "96", "128"])
	config.seek.enter_backward = ConfigSelection(default = "1", choices = ["1", "2", "4", "6", "8", "12", "16", "24", "32", "48", "64", "96", "128"])

	def updateEnterForward(configElement):
		if not configElement.value:
			configElement.value = [2]
		updateChoices(config.seek.enter_forward, configElement.value)
	config.seek.speeds_forward.addNotifier(updateEnterForward, immediate_feedback = False)

	def updateEnterBackward(configElement):
		if not configElement.value:
			configElement.value = [2]
		updateChoices(config.seek.enter_backward, configElement.value)
	config.seek.speeds_backward.addNotifier(updateEnterBackward, immediate_feedback = False)

def FinalInitUsageConfig():
	try:
		usage_old = config.usage.dict().copy()
	except KeyError:
		usage_old = { }

	try:
		seek_old = config.seek.dict().copy()
	except KeyError:
		seek_old = { }

	#We have do it again to ensure tranlsations are applied after language load has finished
	config.usage.setup_level.setChoices([
		("simple", _("Simple")),
		("intermediate", _("Intermediate")),
		("expert", _("Expert")) ], default = "intermediate")

	config.seek.on_pause = ConfigSelection(default = "play", choices = [
		("play", _("Play")),
		("step", _("Singlestep (GOP)")),
		("last", _("Last speed")) ])

	inactivity_shutdown_choices = [ ("never", _("disabled")) ]
	for i in range(1,6):
		inactivity_shutdown_choices.extend([("%d" % i, ngettext("%(num)d hour", "%(num)d hours",i) % {"num" : i})])
	config.usage.inactivity_shutdown = ConfigSelection(default = "3", choices = inactivity_shutdown_choices)
	config.usage.inactivity_shutdown_initialized = ConfigYesNo(default = False)
	config.usage.showdish = ConfigYesNo(default = True)
	config.usage.multibouquet = ConfigYesNo(default = False)
	config.usage.multiepg_ask_bouquet = ConfigYesNo(default = False)

	config.usage.quickzap_bouquet_change = ConfigYesNo(default = False)
	config.usage.e1like_radio_mode = ConfigYesNo(default = False)
	config.usage.infobar_timeout = ConfigSelection(default = "5", choices = [
		("0", _("no timeout")), ("1", "1 " + _("second")), ("2", "2 " + _("seconds")), ("3", "3 " + _("seconds")),
		("4", "4 " + _("seconds")), ("5", "5 " + _("seconds")), ("6", "6 " + _("seconds")), ("7", "7 " + _("seconds")),
		("8", "8 " + _("seconds")), ("9", "9 " + _("seconds")), ("10", "10 " + _("seconds"))])
	config.usage.show_infobar_on_zap = ConfigYesNo(default = True)
	config.usage.show_infobar_on_skip = ConfigYesNo(default = True)
	config.usage.show_infobar_on_event_change = ConfigYesNo(default = True)
	config.usage.hdd_standby = ConfigSelection(default = "600", choices = [
		("0", _("no standby")), ("10", "10 " + _("seconds")), ("30", "30 " + _("seconds")),
		("60", "1 " + _("minute")), ("120", "2 " + _("minutes")),
		("300", "5 " + _("minutes")), ("600", "10 " + _("minutes")), ("1200", "20 " + _("minutes")),
		("1800", "30 " + _("minutes")), ("3600", "1 " + _("hour")), ("7200", "2 " + _("hours")),
		("14400", "4 " + _("hours")) ])

	config.usage.pip_zero_button = ConfigSelection(default = "standard", choices = [
		("standard", _("standard")), ("swap", _("swap PiP and main picture")),
		("swapstop", _("move PiP to main picture")), ("stop", _("stop PiP")) ])

	config.usage.default_path = ConfigText(default = resolveFilename(SCOPE_HDD))
	config.usage.timer_path = ConfigText(default = "<default>")
	config.usage.instantrec_path = ConfigText(default = "<default>")
	config.usage.timeshift_path = ConfigText(default = "/media/hdd/")
	config.usage.allowed_timeshift_paths = ConfigLocations(default = ["/media/hdd/"])

	config.usage.on_movie_start = ConfigSelection(default = "ask", choices = [
		("ask", _("Ask user")), ("resume", _("Resume from last position")), ("beginning", _("Start from the beginning")) ])
	config.usage.on_movie_stop = ConfigSelection(default = "ask", choices = [
		("ask", _("Ask user")), ("movielist", _("Return to movie list")), ("quit", _("Return to previous service")) ])
	config.usage.on_movie_eof = ConfigSelection(default = "ask", choices = [
		("ask", _("Ask user")), ("movielist", _("Return to movie list")), ("quit", _("Return to previous service")), ("pause", _("Pause movie at end")) ])

	config.usage.resume_treshold = ConfigInteger(default=30, limits=[0,300])

	config.usage.on_long_powerpress = ConfigSelection(default = "show_menu", choices = [
		("show_menu", _("show shutdown menu")),
		("shutdown", _("immediate shutdown")),
		("standby", _("Idle Mode")) ] )

	config.usage.on_short_powerpress = ConfigSelection(default = "standby", choices = [
		("show_menu", _("show shutdown menu")),
		("shutdown", _("immediate shutdown")),
		("standby", _("Idle Mode")) ] )

	config.usage.alternatives_priority = ConfigSelection(default = "0", choices = [
		("0", "DVB-S/-C/-T"),
		("1", "DVB-S/-T/-C"),
		("2", "DVB-C/-S/-T"),
		("3", "DVB-C/-T/-S"),
		("4", "DVB-T/-C/-S"),
		("5", "DVB-T/-S/-C") ])

	config.usage.show_event_progress_in_servicelist = ConfigYesNo(default = False)

	config.usage.blinking_display_clock_during_recording = ConfigYesNo(default = False)

	config.usage.show_message_when_recording_starts = ConfigYesNo(default = True)

	config.usage.load_length_of_movies_in_moviellist = ConfigYesNo(default = True)

	config.usage.timerlist_finished_timer_position = ConfigSelection(default = "beginning", choices = [("beginning", _("at beginning")), ("end", _("at end"))])

	config.usage.text_subtitle_presentation = ConfigSelection(default = "black box", choices = [("black box", _("black box")), ("drop-shadow", _("drop-shadow"))])
	config.usage.text_subtitle_black_box_transparency = ConfigSlider(default = 0x64, increment = 5, limits = (0, 0xff))
	config.usage.ttx_subtitle_prefer_pmt_language_code = ConfigYesNo(default=True)

	# Channelselection settings
	config.usage.configselection_showsettingsincontextmenu = ConfigYesNo(default=True)
	config.usage.configselection_showlistnumbers = ConfigYesNo(default=True)
	config.usage.configselection_listnumbersalignment = ConfigSelection(default = "1", choices = [("0",_("left")),("1",_("right"))])
	config.usage.configselection_showservicename = ConfigYesNo(default=True)
	config.usage.configselection_progressbarposition = ConfigSelection(default = "0", choices = [("0",_("After servicenumber")),("1",_("After servicename")), ("2",_("After servicedescription"))])
	config.usage.configselection_servicenamecolwidth = ConfigInteger(200, limits =(100,400))
	config.usage.configselection_columnstyle = ConfigYesNo(default=False)
	config.usage.configselection_additionaltimedisplayposition = ConfigSelection(default = "1", choices = [("0",_("ahead")),("1",_("behind"))])
	config.usage.configselection_tagBouquetServices = ConfigYesNo(default=False)
	config.usage.configselection_showadditionaltimedisplay = ConfigSelection(default = "0",choices = [("0", _("Off")), ("1", _("Percent")), ("2", _("Remain")),("3", _("Remain / duration")), ("4", _("Elapsed")), ("5", _("Elapsed / duration")), ("6", _("Elapsed / remain / duration")),("7", _("Time"))])
	config.usage.configselection_showpicons = ConfigYesNo(default=False)
	config.usage.configselection_bigpicons = ConfigYesNo(default=False)
	config.usage.configselection_secondlineinfo =  ConfigSelection(default = "0", choices = [("0",_("nothing")),("1",_("short description")), ("2",_("upcoming event"))])
	config.usage.configselection_showdvbicons = ConfigYesNo(default=True)

	from os import path as os_path # weazle: Use data partition on 7020hd and V2 boxes

	choiceList = []
	blockList = ["hdd","cf","usb","sdcard"]
	mediadirList = os_listdir("/media")
	
	if fileExists("/proc/mounts"):
		mountsFile = open("/proc/mounts" ,"r")
		for line in mountsFile:
			entry = line.split()
			if entry[2] in ["nfs", "nfs4", "smbfs", "cifs"]:
				if entry[1].startswith("/media/"):
					blockList.append(entry[1][7:])
		mountsFile.close()
	
	dirList = [eEnv.resolve('${datadir}/enigma2/'),'/media/cf/','/media/usb/','/media/sdcard/']
	if os_path.ismount('/data'):
		dirList.append('/data/')
	if os_path.ismount('/dreambox-data'):
		dirList.append('/dreambox-data/')
	
	for mediadir in mediadirList:
		if mediadir in blockList:
			continue
		if os_path.ismount("/media/%s" %(mediadir)) or (os_path.islink("/media/%s" %(mediadir)) and os_path.ismount(os_path.realpath("/media/%s" %(mediadir)))):
			dirList.append("/media/%s/" %(mediadir))
			
	for size in ['picon_50x30','picon']:
		for dir in dirList:
			choiceList.append(("%s%s/" %(dir, size), "%s%s" %(dir, size)))
				
	config.usage.configselection_piconspath = ConfigSelection(default = eEnv.resolve('${datadir}/enigma2/picon_50x30/'), choices = choiceList)


	config.usage.configselection_showrecordings = ConfigYesNo(default=False)
	config.usage.standby_zaptimer_wakeup = ConfigYesNo(default=True)

	seek = config.seek.dict()
	for (key, value) in seek_old.items():
		value_old = value.value
		configEntry = seek[key]
		value_new = configEntry.value
		if value_old != value_new:
			configEntry.value = value_old
		configEntry._ConfigElement__notifiers = value._ConfigElement__notifiers
		configEntry._ConfigElement__notifiers_final = value._ConfigElement__notifiers_final

	usage = config.usage.dict()
	for (key, value) in usage_old.items():
		value_old = value.value
		configEntry = usage[key]
		value_new = configEntry.value
		if value_old != value_new:
			configEntry.value = value_old
		configEntry._ConfigElement__notifiers = value._ConfigElement__notifiers
		configEntry._ConfigElement__notifiers_final = value._ConfigElement__notifiers_final

	if usage_old.get("alternatives_priority", None) == None:
		def TunerTypePriorityOrderChanged(configElement):
			setTunerTypePriorityOrder(int(configElement.value))
		config.usage.alternatives_priority.addNotifier(TunerTypePriorityOrderChanged, immediate_feedback=False)

	if usage_old.get("hdd_standby", None) == None:
		def setHDDStandby(configElement):
			for hdd in harddiskmanager.HDDList():
				hdd[1].setIdleTime(int(configElement.value))
		config.usage.hdd_standby.addNotifier(setHDDStandby, immediate_feedback=False)

	config.usage.record_mode = ConfigSelection(default = "direct_io", choices = [
		("direct_io", _("Direct IO (default)")),
		("cached_io", _("Cached IO")) ] )

	config.usage.max_stream_resolution = ConfigSelection(default = "1080", choices = ["480", "720", "1080", "1440", "2160"])
	config.usage.ts_streaming_buffer = ConfigInteger(default = 500, limits=(0,10000))
	config.usage.non_ts_streaming_buffer = ConfigInteger(default = 500, limits=(0,10000))

	config.media_database = ConfigSubsection()
	config.media_database.readmeta = ConfigYesNo(default=False)

	# Dr.Best: Merlin2 settings
	config.merlin2 = ConfigSubsection()
	config.merlin2.showfoldersinmovielist = ConfigYesNo(default=False)

	config.merlin2.showcolorstatusinmovielist = ConfigYesNo(default=False)
	config.merlin2.showcoverinmovielist = ConfigYesNo(default=True)

	config.merlin2.infobar_position_offset_x = ConfigInteger(default = 0)
	config.merlin2.infobar_position_offset_y = ConfigInteger(default = 0)

	config.merlin2.movieplayer_infobar_position_offset_x = ConfigInteger(default = 0)
	config.merlin2.movieplayer_infobar_position_offset_y = ConfigInteger(default = 0)

	config.merlin2.minitv = ConfigYesNo(default = False)

	config.merlin2.beta = ConfigYesNo(default = False)
	config.merlin2.movieplayer_select_last_seen_movie = ConfigYesNo(default=False)
	config.merlin2.movieplayer_last_service = ConfigText(default = "")
	config.merlin2.moviepercentseen = ConfigInteger(default = 95, limits=(50,100))
	
	config.merlin2.infobar_toggleshow_close_by_user = ConfigYesNo(default=False)
	
	config.merlin2.channelselection_eventinfo = ConfigYesNo(default = False)

	config.merlin2.show_event_not_available = ConfigYesNo(default = False)

	config.merlin2.show_favourites_bouquetup = ConfigYesNo(default = True)

	config.merlin2.spinner = ConfigText(default = "")

	config.merlin2.timeout_message_channel_switch = ConfigNumber(default=20)

	config.merlin2.custom_plugin_sort_order = ConfigYesNo(default=False)

	config.merlin2.keep_outdated_epg = ConfigInteger(default = 0, limits=(0,96))
	
	config.merlin2.jump_first_mark = ConfigYesNo(default=False)

	config.merlin2.enable_stoprec_and_delete_option = ConfigYesNo(default=False)

	config.merlin2.use_mediacache = ConfigYesNo(default=False)
	
	config.merlin2.replaceTimerNameWithEventName = ConfigYesNo(default = False)
	
	config.merlin2.skin_showcamdname = ConfigOnOff(default = True)
	
	config.merlin2.skin_showencryption = ConfigOnOff(default = True)
	
	config.merlin2.skin_showdecryption = ConfigOnOff(default = True)

	config.merlin2.skin_showfta = ConfigOnOff(default = False)
	
	config.merlin2.skin_ftatext = ConfigText(default = "FreeToAir")

	config.merlin2.skin_simpledecodeinfo = ConfigOnOff(default = False)
	
	config.merlin2.skin_readersource = ConfigSelection([("Source", _("Source")),("RS", _("Reader and Source")), ("Reader", _("Reader"))], default = "Source")

	config.merlin2.skin_showsnrindb = ConfigSelection([("skin", _("as in skin")),("SNRdB" ,_("in dB")), ("SNR",_("in %"))], default = "skin")
	
	config.merlin2.skin_shownameorbital = ConfigSelection([("skin", _("as in skin")),("Orbital",_("Orbital")),("Name",_("Name")),("NameOrbital",_("Name & Orbital"))], default="skin")
	
	config.merlin2.skin_showpicon = ConfigOnOff(default = True)

	config.merlin2.skin_showchannelnumber = ConfigOnOff(default = False)
	
	config.merlin2.skin_channellistheader = ConfigSelection([("2", _("Short")),("1", _("No 'Channel Selection'")),("0", _("Standard"))], default = "0")
	
	config.merlin2.skin_showmenuicon = ConfigOnOff(default = True)

	config.merlin2.skin_piconmode = ConfigSelection([("Reference",_("Reference")),("Name", _("Name"))], default="Reference")	

	config.merlin2.skin_ignoreskinpluginicons = ConfigOnOff(default = False)
		
	config.misc.customSpinnerPath = ConfigText(default = "")

	config.merlin2.movieplayer_close_on_standby = ConfigYesNo(default=False)

	config.merlin2.skip_notavailable_services = ConfigYesNo(default=False)

	config.merlin2.show_only_available_services = ConfigYesNo(default=False)

	config.merlin2.show_hidden_folders_in_movielist = ConfigYesNo(default=False)

	config.merlin2.movie_list_force_audio_track_null_hidden_feature = ConfigYesNo(default=False)

	# Shaderman --> PiconLoader
	config.merlin2.showPiconLoader = ConfigYesNo(default = False)
	
	config.merlin2.filename_dateposition = ConfigSelection([("beginning", _("beginning")),("end", _("end")),("none", _("no date"))], default="beginning")
	
	config.merlin2.useextendedzap = ConfigOnOff(default = False)
	
	optionlist = [("default", _("default")),("ask", _("Ask user"))]
	if fileExists("/usr/lib/enigma2/python/Plugins/Extensions/AutoTimer/plugin.pyo"):
		optionlist.append(("autotimer", _("AutoTimer")))
	
	config.merlin2.timertypeselection = ConfigSelection(optionlist, default = "default")
	
	config.merlin2.lengthinmovielist = ConfigSelection([("hms",_("h:m:s")),("ms",_("m:s"))], default="ms")
	
	config.merlin2.useMerlinContextMenu = ConfigYesNo(default = False)
	
	config.merlin2.useMerlinSubservicesQuickzap = ConfigYesNo(default = False)
	
	config.merlin2.calculate_non_ts_video_duration_in_movielist = ConfigYesNo(default = False)
	
	config.merlin2.multiepg_eventview = ConfigSelection([("multiepg", _("Multi EPG")), ("graphmultiepg", _("Graphical Multi EPG")), ("merlinepgcenter", _("Merlin EPG Center"))], default = "multiepg")
	
	config.merlin2.singleepg_eventview = ConfigSelection([("singleepg", _("Single EPG")), ("merlinepgcenter", _("Merlin EPG Center"))], default = "singleepg")
	
	config.merlin2.multiepg_eventview_disable_other_plugins = ConfigYesNo(default=False)

	config.merlin2.channelselection_more_on_color = ConfigSelection(default="key_red", choices=[("key_red", _("red")), ("key_green", _("green")), ("key_yellow", _("yellow"))])
	
	config.merlin2.volume_move_position = ConfigYesNo(default = False)
	config.merlin2.volume_hide_timeout = ConfigSelection(default = "0", choices = [
		("0", _("no timeout")), ("1", "1 " + _("second")), ("2", "2 " + _("seconds")), ("3", "3 " + _("seconds")),
		("4", "4 " + _("seconds")), ("5", "5 " + _("seconds")), ("6", "6 " + _("seconds")), ("7", "7 " + _("seconds")),
		("8", "8 " + _("seconds")), ("9", "9 " + _("seconds")), ("10", "10 " + _("seconds"))])

	config.merlin2.show_radio_in_main = ConfigYesNo(default = False)
	def showRadioInMain(configElement = None):
		SystemInfo["RadioInMain"] = configElement.value
	config.merlin2.show_radio_in_main.addNotifier(showRadioInMain, initial_call = True)

	config.merlin2.use_custom_folder_moviesort = ConfigYesNo(default = False)
	config.merlin2.default_moviesort = ConfigSelection([("1", _("alphabetic sort")), ("3", _("sort by date asc")), ("4", _("sort by date desc"))], default = "4")

	config.merlin2.enable_picture_view_in_movielist = ConfigYesNo(default = False)
	config.merlin2.merlinpictureviewer_picture_duration = ConfigInteger(default = 5, limits = (0, 999))
	config.merlin2.merlinpictureviewer_picture_scale_to_screen = ConfigYesNo(default = False)
	config.merlin2.merlinpictureviewer_transition_duration = ConfigInteger(default = 1000, limits = (0, 5000))
	config.merlin2.merlinpictureviewer_transition_default_duration = ConfigYesNo(default = True)
	config.merlin2.merlinpictureviewer_kenburns = ConfigYesNo(default = False)
	config.merlin2.merlinpictureviewer_transition_mode = ConfigSelection(
		[("0", _("Slice")), ("1", _("Swap")), ("2", _("Water drop")), ("3", _("Doorway")), ("4", _("Cube")), ("5", _("Squares")), 
		("6", _("Circle")), ("7", _("Ripple")), ("8", _("Angular")), ("9", _("Dreamy")), ("10", _("Fade")), ("11", _("Random"))], default = "11")
	

	config.merlin2.disable_alternate_row_color = ConfigYesNo(default = False)
	def disableAlternativeRowColor(configElement = None):
		from enigma import eListbox
		eListbox.enableAlternateRowColor(0 if config.merlin2.disable_alternate_row_color.value else 1)
	config.merlin2.disable_alternate_row_color.addNotifier(disableAlternativeRowColor, initial_call = True)

	config.merlin2.enable_video_widget = ConfigYesNo(default = False)
	config.merlin2.disable_ts_in_video_widget = ConfigYesNo(default = True)
	config.merlin2.streamrelayip = ConfigIP(default=[127, 0, 0, 1])
	config.merlin2.streamrelayport = ConfigNumber(default= 17999)

def updateChoices(sel, choices):
	if choices:
		defval = None
		val = int(sel.value)
		if not val in choices:
			tmp = choices[:]
			tmp.reverse()
			for x in tmp:
				if x < val:
					defval = str(x)
					break
		sel.setChoices(map(str, choices), defval)

def preferredPath(path):
	if config.usage.setup_level.index < 2 or path == "<default>":
		return None  # config.usage.default_path.value, but delay lookup until usage
	elif path == "<current>":
		return config.movielist.last_videodir.value
	elif path == "<timer>":
		return config.movielist.last_timer_videodir.value
	else:
		return path

def preferredTimerPath():
	return preferredPath(config.usage.timer_path.value)

def preferredInstantRecordPath():
	return preferredPath(config.usage.instantrec_path.value)

def defaultMoviePath():
	return config.usage.default_path.value

def defaultStorageDevice():
	return config.storage_options.default_device.value
