#!/usr/bin/env python2.5
# -*- coding: utf-8 -*-
__author__ = "M. Dietrich <mdt@pyneo.org>, F. Gau (fgau@pyneo.org), Thomas Gstaedtner (thomas (a) gstaedtner (.) net), Johannes 'josch' Schauer <j.schauer@email.de>"
__version__ = "prototype"
__copyright__ = "Copyright (c) 2009"
__license__ = "GPL3"

from epydial import *

class SettingsScreen():
	def register_pyneo_callbacks(self):
		PyneoController.register_callback("brightness_change", self.on_brightness_change)

	def __init__(self, screen_manager):
		self.FONT_COLOR = PyneoController.set_font_color
		self.red = int(self.FONT_COLOR[3:5], 16)
                self.green = int(self.FONT_COLOR[5:7], 16)
                self.blue = int(self.FONT_COLOR[7:], 16)
                self.alpha = int(self.FONT_COLOR[1:3], 16)

		self.state_type = PyneoController.alsacurrent
		self.vibrate_type = PyneoController.vibrate_current
		self.syslog_type = PyneoController.syslog_current
		self.buttons = {}
		self.settings = {}
		self.canvas = screen_manager.get_evas()

		self.bg = evas.Image(self.canvas, pos=(0, 0), size=(WIDTH, HEIGHT), file=PyneoController.set_bg_image)
		self.bg.fill = 0, 0, WIDTH, HEIGHT
		self.bg.layer = 97

		self.headline = evas.Text(self.canvas, text="settings", font=("Sans:style=Bold,Edje-Vera", 60), color=self.FONT_COLOR)
		self.headline.layer = 99
		self.headline.pos = ((480-self.headline.horiz_advance)/2, 25)

		for pos, text in enumerate(["back", "forward"]):
			self.buttons[text] = self.init_button(text, (pos+1)*16+pos*100, 524, 100, 100)

		for pos, text in enumerate(["syslog", "vibrate", "state", "brightness"]):
			for i, row in enumerate(["left", "right", "description", "box", "plus", "minus"]):
				self.settings[text, row] = self.init_settings(row, text, (pos+1)*25+pos*75+100)

		self.settings['syslog', 'description'].text = 'syslog: %s' % self.syslog_type
		x, y = self.settings['syslog', 'description'].pos_get()
		self.settings['syslog', 'description'].pos = ((WIDTH-self.settings['syslog', 'description'].horiz_advance)/2, y)

		self.settings['vibrate', 'description'].text = 'vibrate: %s' % self.vibrate_type
		x, y = self.settings['vibrate', 'description'].pos_get()
		self.settings['vibrate', 'description'].pos = ((WIDTH-self.settings['vibrate', 'description'].horiz_advance)/2, y)

		self.settings['state', 'description'].text = 'state: %s' % self.state_type
		x, y = self.settings['state', 'description'].pos_get()
		self.settings['state', 'description'].pos = ((WIDTH-self.settings['state', 'description'].horiz_advance)/2, y)

		self.settings['brightness', 'description'].text = 'brightness: %s%%' % PyneoController.brightness_value
		x, y = self.settings['brightness', 'description'].pos_get()
		self.settings['brightness', 'description'].pos = ((WIDTH-self.settings['brightness', 'description'].horiz_advance)/2, y)

		PyneoController.brightness_change(0)

	def init_settings(self, row, name, y):
		def button_callback(source, event):
			if name == 'brightness' and row == 'right':
				PyneoController.brightness_change(10)
			elif name == 'brightness' and row == 'left':
				PyneoController.brightness_change(-10)
			elif name == 'state' and row == 'right':
				states_avail = PyneoController.alsastate
				self.state_type = states_avail[(states_avail.index(self.state_type)+1)%len(states_avail)]
				PyneoController.set_state_file(self.state_type)
				self.settings['state', 'description'].text = 'state: %s' % self.state_type
				x, y = self.settings['state', 'description'].pos_get()
				self.settings['state', 'description'].pos = ((WIDTH-self.settings['state', 'description'].horiz_advance)/2, y)
			elif name == 'state' and row == 'left':
				states_avail = PyneoController.alsastate
				self.state_type = states_avail[(states_avail.index(self.state_type)-1)%len(states_avail)]
				PyneoController.set_state_file(self.state_type)
				self.settings['state', 'description'].text = 'state: %s' % self.state_type
				x, y = self.settings['state', 'description'].pos_get()
				self.settings['state', 'description'].pos = ((WIDTH-self.settings['state', 'description'].horiz_advance)/2, y)
			elif name == 'vibrate' and row == 'right':
				states_avail = PyneoController.vibrate_state
				self.vibrate_type = states_avail[(states_avail.index(self.vibrate_type)+1)%len(states_avail)]
				PyneoController.vibrate_current = self.vibrate_type
				self.settings['vibrate', 'description'].text = 'vibrate: %s' % self.vibrate_type
				x, y = self.settings['vibrate', 'description'].pos_get()
				self.settings['vibrate', 'description'].pos = ((WIDTH-self.settings['vibrate', 'description'].horiz_advance)/2, y)
			elif name == 'vibrate' and row == 'left':
				states_avail = PyneoController.vibrate_state
				self.vibrate_type = states_avail[(states_avail.index(self.vibrate_type)-1)%len(states_avail)]
				PyneoController.vibrate_current = self.vibrate_type
				self.settings['vibrate', 'description'].text = 'vibrate: %s' % self.vibrate_type
				x, y = self.settings['vibrate', 'description'].pos_get()
				self.settings['vibrate', 'description'].pos = ((WIDTH-self.settings['vibrate', 'description'].horiz_advance)/2, y)
			elif name == 'syslog' and row == 'right':
				states_avail = PyneoController.syslog_state
				self.syslog_type = states_avail[(states_avail.index(self.syslog_type)+1)%len(states_avail)]
				PyneoController.syslog_current = self.syslog_type
				PyneoController.set_debug_log(self.syslog_type)
				self.settings['syslog', 'description'].text = 'syslog: %s' % self.syslog_type
				x, y = self.settings['syslog', 'description'].pos_get()
				self.settings['syslog', 'description'].pos = ((WIDTH-self.settings['syslog', 'description'].horiz_advance)/2, y)
			elif name == 'syslog' and row == 'left':
				states_avail = PyneoController.syslog_state
				self.syslog_type = states_avail[(states_avail.index(self.syslog_type)-1)%len(states_avail)]
				PyneoController.syslog_current = self.syslog_type
				PyneoController.set_debug_log(self.syslog_type)
				self.settings['syslog', 'description'].text = 'syslog: %s' % self.syslog_type
				x, y = self.settings['syslog', 'description'].pos_get()
				self.settings['syslog', 'description'].pos = ((WIDTH-self.settings['syslog', 'description'].horiz_advance)/2, y)
			print '--- ', name, row
		if row == 'left':
			bg_left = evas.Rectangle(self.canvas, pos=(0,y), size=(WIDTH/2,80), color='#00ffffff')
			bg_left.layer = 99
			bg_left.on_mouse_up_add(button_callback)
			return bg_left
		if row == 'right':
			bg_right = evas.Rectangle(self.canvas, pos=(WIDTH/2,y), size=(WIDTH/2,80), color='#00ffffff')
			bg_right.layer = 99
			bg_right.on_mouse_up_add(button_callback)
			return bg_right
		if row == 'description':
			description = evas.Text(self.canvas, font=("Sans:style=Edje-Vera", 18), color=self.FONT_COLOR)
			description.text = '%s:' % name
			description.pos = ((WIDTH-description.horiz_advance)/2, y+26)
			description.layer = 99
			return description
		if row == 'box':
			box = evas.Rectangle(self.canvas, pos=(WIDTH/4,y+20), size=(WIDTH/2,40), color='#%02x%02x%02x%02x' % (self.alpha/2, self.red, self.green, self.blue))
			box.layer = 98
			return box
		if row == 'plus':
			plus = evas.Text(self.canvas, text="+", pos=(420,y), font=("Sans:style=Edje-Vera", 46), color=self.FONT_COLOR)
			plus.layer = 98
			return plus
		if row == 'minus':
			minus = evas.Text(self.canvas, text="-", pos=(40, y), font=("Sans:style='Edje-Vera", 46), color=self.FONT_COLOR)
			minus.layer = 98
			return minus

	def init_button(self, name, x, y, dx, dy):
		def button_callback(source, event):
			if name == 'back':
				PyneoController.show_dialer_screen()
			elif name == 'forward':
			        PyneoController.show_screen(FONTCOLOR_SCREEN_NAME)
			print '--- ', name
		button = evas.Image(self.canvas, pos=(x,y), size=(dx,dy), file="/usr/share/epydial/data/themes_data/blackwhite/images/%s.png" % name)
		button.fill = 0, 0, dx, dy
		button.layer = 99
		button.on_mouse_up_add(button_callback)
		return button

	def show(self):
		self.bg.show()
		self.headline.show()

		for pos, text in enumerate(["syslog", "vibrate", "state", "brightness"]):
			for i, row in enumerate(["left", "right", "description", "box", "plus", "minus"]):
				self.settings[text, row].show()

		for pos, text in enumerate(["back", "forward"]):
			self.buttons[text].show()

	def hide(self):
		self.bg.hide()
		self.headline.hide()

		for pos, text in enumerate(["syslog", "vibrate", "state", "brightness"]):
			for i, row in enumerate(["left", "right", "description", "box", "plus", "minus"]):
				self.settings[text, row].hide()

		for pos, text in enumerate(["back", "forward"]):
			self.buttons[text].hide()

	def on_brightness_change(self, status):
		self.settings['brightness', 'description'].text = 'brightness: %s%%' % status
		x, y = self.settings['brightness', 'description'].pos_get()
		self.settings['brightness', 'description'].pos = ((WIDTH-self.settings['brightness', 'description'].horiz_advance)/2, y)
