"""
Soft-LED

Mögliche Basisfarben:
  Rot, Grün, Blau, Orange, Gelb, Aqua, Violett, Weiß

Funktionen
  Farbe einstellen
  Helligkeit einstellen
  Blitzen
  Einschalten
  Ausschalten
  Leuchten, limitiert auf eine Zeitdauer

TODO Eigene Drawingarea im Frontend

"""

import math
from gi.repository import GLib, Gtk

colordef = {
    'red':    ((1.0, 0.0, 0.0), "0xff0000"),
    'green':  ((0.0, 1.0, 0.0), "0x00ff00"),
    'blue':   ((0.0, 0.0, 1.0), "0x0000ff"),
    'orange': ((1.0, 0.6, 0.0), "0xff9900"),
    'yellow': ((1.0, 1.0, 0.0), "0xffff00"),
    'aqua':   ((0.2, 0.4, 1.0), "0x3366ff"),
    'violet': ((1.0, 0.0, 0.4), "0xff0066"),
    'white':  ((1.0, 1.0, 1.0), "0xffffff")
}

class LED():

    def __init__(self, parent, fullscreen):
        # parent muß ein Gtk.Fixed-Objekt sein
        self._colorname = ""
        self._color = (0, 0, 0)                    # Farbe schwarz ist aus
        self._brightness = 0.0
        self._color_off = (0.3725, 0.4275, 0.6078) # RGB(95, 109, 155)
        self._flashcounter = 0
        self._flashcolor = (0, 0, 0)
        self._enabled = False

        # Box oberhalb des Displays
        self._da = Gtk.DrawingArea()
        if fullscreen:
            parent.put(self._da, 640, 0)
            self._da.set_size_request(160, 64)
            self._pos_x = 18
            self._pos_y = 20
            self._radius = 8
        else:
            parent.put(self._da, 64, 56)
            self._da.set_size_request(400, 32)
            self._pos_x = 30
            self._pos_y = 14
            self._radius = 5
        self._da.connect("draw", self.on_draw)

    def on_draw(self, widget, ctx):
        # Debug um Zeichenbereich zu sehen
        #ctx.set_line_width(1.0)
        #ctx.set_source_rgb(1, 0, 0)
        #ctx.rectangle(0.5, 0.5, 400, 32)
        #ctx.stroke()
        print("LED draw")
        ctx.set_line_width(2.0)
        if self._brightness == 0:
            ctx.set_source_rgb(*self._color_off)
        else:
            ctx.set_source_rgb(*self._color)
        ctx.arc(self._pos_x, self._pos_y, self._radius, 0, 2*math.pi)
        ctx.fill_preserve()
        ctx.set_source_rgb(0, 0, 0) # Schwarzer Rand
        ctx.stroke()
        ctx.arc(self._pos_x, self._pos_y, self._radius, 0, 2*math.pi)
        ctx.stroke()

    def on_timer(self):
        print("Timer")
        self.switchOff()
        return False

    def on_blink(self):
        print("Blink-Timer")
        return False

    def on_flash(self):
        # TODO do not wait for drawing event but draw immediately
        print("Flash-Timer", self._brightness)
        self._da.queue_draw()
        if self._brightness == 1:
            self._brightness = 0
        else:
            self._brightness = 1
        if  self._flashcounter > 0:
            if self._brightness == 0:
                self._flashcounter -=1
            print("Flash timer ongoing")
            return True
        print("Flash finished")
        self._brightness = 0
        return False

    def switchOn(self, duration=None):
        self._brightness = 1.0
        self._da.queue_draw()
        if duration:
            GLib.timeout_add_seconds(duration, self.on_timer)

    def switchOff(self):
        self._brightness = 0.0
        self._da.queue_draw()

    def doBlink(self, duration=None):
        pass

    def doFlash(self, count=1):
        self._cr = self._da.get_window().cairo_create() # Context for immediate drawing
        if (self._brightness > 0) or (count < 0):
            # Wenn aktiv oder falscher Parameter, dann passiert nichts
            return
        if count > 10:
            # maximal 10 Blitze
            self._flashcounter = 10
        else:
            self._flashcounter = count
        self._brightness = 1
        self._da.queue_draw()
        GLib.timeout_add_seconds(0.5, self.on_flash)

    def setColor(self, colorname, brightness=1.0):
        #TODO Helligkeit einstellen
        colorname = colorname.lower()
        if not colorname in colordef:
            return
        self._colorname = colorname
        self._color = colordef[colorname][0]

    def getColor(self):
        return self._color

    def getColorAsHex(self):
        return '#{:02x}{:02x}{:02x}'.format(
            int(self._color[0] * 255),
            int(self._color[1] * 255),
            int(self._color[2] * 255))

    def setBrightness(self, newbrightness):
        # 0% bis 100%, ausgehend von der aktuellen Basisfarbe
        pass
