#  Gnome15 - Suite of tools for the Logitech G series keyboards and headsets
#  Copyright (C) 2010 Brett Smith <tanktarta@blueyonder.co.uk>
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
import gnome15.g15locale as g15locale
_ = g15locale.get_translation("rundapp", modfile = __file__).ugettext

import gnome15.util.g15convert as g15convert
import gnome15.util.g15uigconf as g15uigconf
import gnome15.util.g15gconf as g15gconf
import gnome15.util.g15cairo as g15cairo
import gnome15.g15driver as g15driver
import gnome15.g15screen as g15screen
import gnome15.g15profile as g15profile
import gnome15.g15desktop as g15desktop
import cairo
import gtk
import os
import logging
import gconf
from lxml import etree
logger = logging.getLogger(__name__)

# Plugin details - All of these must be provided
id="rundapp"
name=_("aaaa")
description=_("Mi app en la pantalla")
author=_("Gustavo Adolfo Mesa Roldan <gore@hatthieves.es>")
copyright="Copyright (C)2020 Brett Gustavo Adolfo Mesa Roldan"
site="http://hatthieves.es"
has_preferences=False
unsupported_models = [ g15driver.MODEL_G110, g15driver.MODEL_G11, g15driver.MODEL_G930, g15driver.MODEL_G35 ]

time_2 = 0 
time_2 = time_2 + 1

def create(gconf_key, gconf_client, screen):
	return G19app(gconf_key, gconf_client, screen)

class G19appPainter(g15screen.Painter):    
	def __init__(self, screen):
		g15screen.Painter.__init__(self, g15screen.BACKGROUND_PAINTER, -9999)
		self.background_image = None
		self.brightness = 0
		self._screen = screen

	def paint(self, canvas):
		
		print("1111111111111")
		if self.background_image != None:
			canvas.set_source_surface(self.background_image, 0.0, 0.0)
			canvas.paint()
			if self.brightness > 0:
				canvas.set_source_rgba(1.0, 1.0, 1.0, ( self.brightness / 100.0 ))
			else:
				canvas.set_source_rgba(0.0, 0.0, 0.0, ( abs(self.brightness) / 100.0 ))
			size = self._screen.device.lcd_size
			canvas.rectangle(0,0,size[0],size[1])
			canvas.fill()

class G19app():    
	def __init__(self, gconf_key, gconf_client, screen):
		self.screen = screen
		self.gconf_client = gconf_client
		self.gconf_key = gconf_key
		self.target_surface = None
		self.target_context = None
		self.gconf_client.add_dir('/desktop/gnome/background', gconf.CLIENT_PRELOAD_NONE)
        
	def refresh(self):
		print("2222222222222222")
		self.painter = G19appPainter(self.screen)

	def activate(self):
		self.painter = G19appPainter(self.screen)
		self.notify_handlers = []
		self.screen.painters.append(self.painter)
		self.notify_handlers.append(self.gconf_client.notify_add(self.gconf_key + "/path", self.config_changed))
		self.notify_handlers.append(self.gconf_client.notify_add(self.gconf_key + "/type", self.config_changed))
		self.notify_handlers.append(self.gconf_client.notify_add(self.gconf_key + "/style", self.config_changed))
		self.notify_handlers.append(self.gconf_client.notify_add(self.gconf_key + "/brightness", self.config_changed))
		self.notify_handlers.append(self.gconf_client.notify_add("/apps/gnome15/%s/active_profile" % self.screen.device.uid, self._active_profile_changed))
       
		g15profile.profile_listeners.append(self._profiles_changed)
		self._do_config_changed()

	def deactivate(self):
		g15profile.profile_listeners.remove(self._profiles_changed)
		self.screen.painters.remove(self.painter)
		self.screen.redraw()

	def config_changed(self, client, connection_id, entry, args):
		self._do_config_changed()

	def destroy(self):
		pass

	'''
	Private
	'''
	def _active_profile_changed(self, client, connection_id, entry, args):
		self._do_config_changed()

	def _profiles_changed(self, profile_id, device_uid):
		self._do_config_changed()

	def _do_config_changed(self):
		print("2222222222222222")
		screen_size = self.screen.size
		surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, screen_size[0], screen_size[1])
		context = cairo.Context(surface)
		context.scale(screen_size[0], screen_size[1])

		context.select_font_face("Arial", cairo.FONT_SLANT_NORMAL, cairo.FontWeight.BOLD)
		context.move_to(0.1, 0.1)
		context.show_text("Paner Chupala")

		context.set_line_width(0.04)
		context.move_to(0.1, 0.5)
		context.curve_to(0.4, 0.9, 0.6, 0.1, 0.9, 0.5)
		context.stroke()
		context.set_source_rgba(1, 0.2, 0.2, 0.6)
		context.set_line_width(0.02)
		context.move_to(0.1, 0.5)
		context.line_to(0.4, 0.9)
		context.move_to(0.6, 0.1)
		context.line_to(0.9, 0.5)
		context.stroke()
		context.save()
		context.paint()	
		context.restore()
		self.painter.background_image = surface
		self.painter.brightness = self.gconf_client.get_int(self.gconf_key + "/brightness")
		self.screen.redraw()



