#! /usr/bin/python3

"""
A demo/test script for the XAppKbdLayoutController class
"""
import sys, os
import signal
import gettext

import gi
gi.require_version('Gtk', '3.0')
gi.require_version('XApp', '1.0')

from gi.repository import Gtk, XApp, GObject
import cairo

signal.signal(signal.SIGINT, signal.SIG_DFL)

class Main:
    def __init__(self):
        win = Gtk.Window()

        win.set_default_size(320, 200)

        frame = Gtk.Frame()
        frame.set_margin_start(2)
        frame.set_margin_end(2)
        frame.set_margin_top(2)
        frame.set_margin_bottom(2)

        win.add(frame)

        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        box.set_margin_start(2)
        box.set_margin_end(2)
        box.set_margin_top(2)
        box.set_margin_bottom(2)

        frame.add(box)

        self.use_caps = False

        self.controller = XApp.KbdLayoutController()
        self.controller.connect("layout-changed", self.on_layout_changed)
        self.controller.connect("config-changed", self.on_config_changed)

        self.label = Gtk.Label()
        self.label.set_text(self.controller.get_current_name())
        box.pack_start(self.label, True, True, 4)

        hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
        box.pack_start(hbox, True, True, 4)

        self.flag_button = Gtk.Button()
        hbox.pack_start(self.flag_button, True, True, 4)
        self.flag_button.connect("clicked", self.on_button_clicked)

        self.group_button = Gtk.Button()
        hbox.pack_start(self.group_button, True, True, 4)
        self.group_button.connect("clicked", self.on_button_clicked)

        self.variant_button = Gtk.Button()
        hbox.pack_start(self.variant_button, True, True, 4)
        self.variant_button.connect("clicked", self.on_button_clicked)

        check = Gtk.CheckButton.new_with_label("Use caps")
        check.connect("toggled", self.on_caps_toggled)
        box.pack_start(check, True, True, 4)

        frame.show_all()

        win.connect("delete-event", lambda w, e: Gtk.main_quit())

        self.on_layout_changed(self.controller)

        win.present()

        Gtk.main()

    def on_caps_toggled(self, widget):
        self.use_caps = widget.get_active()
        self.on_layout_changed(self.controller)

    def on_button_clicked(self, widget, data=None):
        self.controller.next_group()

    def on_layout_changed(self, controller, group=None):
        handled = False

        name = self.controller.get_current_icon_name()
        if name != None:
            filename = "/usr/share/iso-flag-png/%s.png" % name

            if os.path.exists(filename):
                valid, width, height = Gtk.IconSize.lookup(Gtk.IconSize.LARGE_TOOLBAR)

                surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
                flag_surface = cairo.ImageSurface.create_from_png(filename)

                cr = cairo.Context(surface)
                cr.save()

                factor = width / flag_surface.get_width()
                true_width = flag_surface.get_width() * factor
                true_height = flag_surface.get_height() * factor

                x_offset = y_offset = 0

                if flag_surface.get_width() >= flag_surface.get_height():
                    x_offset = 0
                    y_offset = ((height * (1 / factor)) - flag_surface.get_height()) / 2
                else:
                    x_offset = ((width * (1 / factor)) - flag_surface.get_width()) / 2
                    y_offset = 0

                true_x_offset = (width - true_width) / 2;
                true_y_offset = (height - true_height) / 2;

                cr.scale(factor, factor);

                cr.set_source_surface(flag_surface, x_offset, y_offset);

                cr.get_source().set_filter(cairo.FILTER_BEST);
                cr.set_operator(cairo.OPERATOR_SOURCE);

                cr.paint();
                cr.restore()

                XApp.KbdLayoutController.render_cairo_subscript(cr,
                                                                true_x_offset + (true_width / 2),
                                                                true_y_offset + (true_height / 2),
                                                                true_width / 2,
                                                                true_height / 2,
                                                                self.controller.get_current_flag_id())

                image = Gtk.Image.new_from_surface(cr.get_target())
                self.flag_button.set_image(image)
                handled = True
            else:
                print("Missing /usr/share/iso-flag-png flag image - text only")

        if not handled:
            name = self.controller.get_current_short_group_label()
            if self.use_caps:
                self.flag_button.set_label(name.upper())
            else:
                self.flag_button.set_label(name)

            self.flag_button.set_image(None)

        self.label.set_text(self.controller.get_current_name())

        group_label = self.controller.get_current_short_group_label()
        variant_label = self.controller.get_current_variant_label()

        if self.use_caps:
            group_label = group_label.upper()
            variant_label = variant_label.upper()

        self.group_button.set_label(group_label)
        self.variant_button.set_label(variant_label)

    def on_config_changed(self, controller):
        GObject.idle_add(self.on_layout_changed, controller)

if __name__ == "__main__":
    main = Main()
