# -*- coding: utf-8 -*-
from Plugins.Bp.geminimain.gLocale import _
from Plugins.Bp.geminimain.gTools import libgeminimain, cleanexit, isHDRes

from Screens.Screen import Screen
from Components.ActionMap import ActionMap
from Components.Sources.StaticText import StaticText
from Components.Sources.Progress import Progress
from Components.Pixmap import MultiPixmap, Pixmap
from Screens.ChoiceBox import ChoiceBox

class Cequalizer(Screen):
	BANDCOUNT=10
	MAXVALUE=26

	def __init__(self, session, val, libjukebox):
		Screen.__init__(self, session)
		self.__libjukebox = libjukebox
		self.skinName = []
		if isHDRes == 1:
			self.skinName.append("Equalizer-XD")
		elif isHDRes == 2:
			self.skinName.append("Equalizer-HD")
		self.skinName.append("Equalizer-SD")

		self["actions"] = ActionMap(["gActions"],
		{
			"exit": self.KeyExit,
			"left": self.KeyLeft,
			"right": self.KeyRight,
			"down": self.KeyDown,
			"up": self.KeyUp,
			"blue": self.KeyBlue,
			"green": self.KeyGreen,
			"yellow": self.KeyYellow
		}, -1)
		
		self.__index=4
		
		self["PixmapBlue"] = Pixmap()
		self["LabelBlue"] = StaticText(_("Setup"))
		self["PixmapGreen"] = Pixmap()
		self["LabelGreen"] = StaticText(_("Save"))
		self["PixmapYellow"] = Pixmap()
		self["LabelYellow"] = StaticText(_("Test"))
		self["db0"] = StaticText("+2 dB")
		self["db1"] = StaticText("-10 dB")
		self["db2"] = StaticText("-24 dB")
		self["db3"] = StaticText("+2 dB")
		self["db4"] = StaticText("-10 dB")
		self["db5"] = StaticText("-24 dB")

		for x in range(self.BANDCOUNT):
			self["Progress" + str(x)] = Progress()
			self["PixmapGreen" + str(x)] = MultiPixmap()
			self["hz" + str(x)] = StaticText()
		
		self.__valList=[24,24,24,24,24,24,24,24,24,24]
		bla = val.split(',')
		if len(bla)==self.BANDCOUNT:
			for x in range(self.BANDCOUNT):
				self.__valList[x]=int(bla[x])
				if self.__valList[x] > self.MAXVALUE:
					self.__valList[x] = self.MAXVALUE

		self.onLayoutFinish.append(self.__LayoutFinish)

	def KeyExit(self, val=None):
		self.close(val)
		cleanexit(__name__)
		
	def createString(self):
		text=""
		for x in range(self.BANDCOUNT):
			text += str(self.__valList[x])
			if x<self.BANDCOUNT-1:
				text += ","
		return text
		
	def KeyGreen(self):
		self.KeyExit(self.createString())
		
	def KeyYellow(self):
		self.__libjukebox.setEqualizer(self.createString())
		
	def __paintIndex(self):
		for x in range(self.BANDCOUNT):
			if self.__index == x:
				self["PixmapGreen" + str(x)].setPixmapNum(1)
			else:
				self["PixmapGreen" + str(x)].setPixmapNum(0)

	def __LayoutFinish(self):
		self.setTitle(_("Equalizer"))
		for x in range(self.BANDCOUNT):
			self["Progress" + str(x)].setRange(self.MAXVALUE)
			self["Progress" + str(x)].setValue(self.__valList[x])
		self.__paintIndex()
		
	def KeyLeft(self):
		self.__index -= 1
		if self.__index < 0:
			self.__index=self.BANDCOUNT-1
		self.__paintIndex()

	def KeyRight(self):
		self.__index += 1
		if self.__index > self.BANDCOUNT-1:
			self.__index=0
		self.__paintIndex()
		
	def KeyDown(self):
		self.__valList[self.__index] -= 1
		if self.__valList[self.__index]<0:
			self.__valList[self.__index]=0
		self["Progress" + str(self.__index)].setValue(self.__valList[self.__index])
	
	def KeyUp(self):
		self.__valList[self.__index] += 1
		if self.__valList[self.__index]>self.MAXVALUE:
			self.__valList[self.__index]=self.MAXVALUE
		self["Progress" + str(self.__index)].setValue(self.__valList[self.__index])
	
	def KeyBlue(self):
		menu = []
		menu.append(("Disco", [25,23,20,17,17,17,17,20,23,25]))#24 ist 0db
		menu.append(("Neutral", [24,24,24,24,24,24,24,24,24,24]))#24 ist 0db
		self.session.openWithCallback(self.__menuCallback, ChoiceBox, title="", list=menu)
		
	def __menuCallback(self, what=None):
		if what is None:
			return
		self.__valList = what[1]
		for x in range(self.BANDCOUNT):
			self["Progress" + str(x)].setValue(self.__valList[x])


