#!/usr/bin/env python3
"""Popup GTK qui affiche l'interface Color Picker via WebKit"""
import os
import sys
from pathlib import Path

import gi

# for systems blocking direct GBM access we force WebKit to stay in software mode
os.environ.setdefault('WEBKIT_DISABLE_COMPOSITING_MODE', '1')
os.environ.setdefault('GDK_BACKEND', 'x11')

gi.require_version('Gtk', '3.0')
gi.require_version('WebKit2', '4.1')
from gi.repository import Gtk, WebKit2, GLib

SCRIPT_DIR = Path(__file__).resolve().parent
SRC_FILE = SCRIPT_DIR / 'src' / 'index.html'


class ColorPickerWindow(Gtk.Window):
    def __init__(self):
        super().__init__(title='Color Picker')
        self.set_default_size(980, 640)
        self.set_position(Gtk.WindowPosition.CENTER)
        self.set_border_width(8)
        self.set_resizable(True)

        settings = WebKit2.Settings()
        settings.set_property('enable-developer-extras', False)
        settings.set_property('enable-accelerated-2d-canvas', False)

        self.webview = WebKit2.WebView()
        self.webview.set_settings(settings)
        self.add(self.webview)

        uri = GLib.filename_to_uri(str(SRC_FILE), None)
        self.webview.load_uri(uri)

        self.connect('destroy', Gtk.main_quit)
        self.show_all()


def main() -> None:
    if not SRC_FILE.exists():
        print(f'Fichier introuvable : {SRC_FILE}', file=sys.stderr)
        sys.exit(1)
    win = ColorPickerWindow()
    Gtk.main()


if __name__ == '__main__':
    main()
