#!/usr/bin/python
# -*- coding: utf-8 -*-

from welcome.binary import NewBinary
from welcome.version import VersionChecker
from welcome.website import WebInvitation
from welcome.board import SmallBoard
from welcome.tip import TipOfTheDay
from gi.repository import GObject, Gtk, Gdk
from pychrysalide.gui.panels import PanelItem


class WelcomePanel(PanelItem):
    """Display a welcome panel if nothing is open in the main part of the editor."""


    def __init__(self):
        """Initialize the Python instance of the panel."""

        content = self._build_panel_content()

        super(WelcomePanel, self).__init__('Welcome', 'First commands', content, 'M')


    def _build_panel_content(self):
        """Build content for the welcome panel."""

        self._area = None

        # Constitution du support principal

        support = Gtk.Grid()

        support.props.halign = Gtk.Align.CENTER
        support.props.valign = Gtk.Align.CENTER
        support.props.margin = 20

        support.set_column_homogeneous(True)
        support.set_row_homogeneous(True)
        support.set_column_spacing(20)
        support.set_row_spacing(20)

        # Mise en place des différentes tuiles

        cells = { }

        for x in range(4):
            for y in range(3):
                cells[(x, y)] = False

        tiles = [

            NewBinary(),
            VersionChecker(),
            WebInvitation(),
            TipOfTheDay()

        ]

        for t in tiles:

            x, y, w, h = t.get_location()

            for i in range(x, x + w):
                for j in range(y, y + h):
                    assert(cells[(i, j)] == False)
                    cells[(i, j)] = True

            t.attach(self)
            support.attach(t, x, y, w, h)

        for x in range(4):
            for y in range(3):
                if not cells[(x, y)]:
                    tile = SmallBoard()
                    tile.attach(self)
                    support.attach(tile, x, y, 1, 1)

        # Charge les styles propres aux panneaux

        style_provider = Gtk.CssProvider()

        style_provider.load_from_data(SmallBoard.css.encode())

        Gtk.StyleContext.add_provider_for_screen(
            Gdk.Screen.get_default(), 
            style_provider,     
            Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
        )


        support.show_all()
        return support


    def define_selected_area(self, area):

        self._area = area

        ####
        #self.queue_draw()


    def draw_selected_area(self, widget, cr):


        alloc = widget.get_allocation()




        def draw_rounded2(cr, area, radius):
            """ draws rectangles with rounded (circular arc) corners """
            from math import pi
            a,b,c,d=area
            cr.arc(a + radius, c + radius, radius, 2*(pi/2), 3*(pi/2))
            cr.arc(b - radius, c + radius, radius, 3*(pi/2), 4*(pi/2))
            cr.arc(b - radius, d - radius, radius, 0*(pi/2), 1*(pi/2))  # ;o)
            cr.arc(a + radius, d - radius, radius, 1*(pi/2), 2*(pi/2))
            cr.rectangle(self._area.x, self._area.y, self._area.width, self._area.height)
            cr.close_path()
            cr.fill()


        if self._area != None:
            cr.set_source_rgba(0, 0, 0, 0.15)

            border = 4

            pts = ( self._area.x - border, self._area.x + self._area.width + border,
                    self._area.y - border, self._area.y + self._area.height + border )

            #cr.save()

            draw_rounded2(cr, pts, 16)

            ctx = self.get_style_context()


            Gtk.render_background(ctx, cr, 0, 20, 100, 100)
            Gtk.render_background(ctx, cr, self._area.x, self._area.y, self._area.width, self._area.height)


            #cr.restore()


        return False