# -*- coding: utf-8 -*-
#
#  Copyright (C) 2011 Thomaz de Oliveira dos Reis <thor27@gmail.com>
#  Copyright (C) 2021 Elton Fabrício Ferreira <eltonfabricio10@gmail.com>
#
# 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 os
import gi
gi.require_version("Gtk", "3.0")
gi.require_version("WebKit2", "4.1")
from gi.repository import Gtk, WebKit2, Gdk
from bbv.globaldata import ICON, TITLE

class Window(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.webview = WebKit2.WebView()
        settings = self.webview.get_settings()
        settings.set_user_agent("BigBashView-Agent")
        self.webview.show()
        self.add(self.webview)
        self.set_icon_from_file(ICON)
        if TITLE:
            self.set_title(TITLE)
        else:
            self.webview.connect("notify::title", self.title_changed)
        self.webview.connect("load-changed", self.add_script)
        self.webview.connect("close", self.close_window)
        self.webview.get_settings().set_property(
            "enable-developer-extras",
            True
        )
        self.connect("destroy", Gtk.main_quit)
        self.connect("key-press-event", self.key)

    def key(self, webview, event):
        # Reload the webview when the F5 key is pressed
        if event.keyval == 65474:
            self.webview.reload()
        # Show the web inspector when the F12 key is pressed
        if event.keyval == 65481:
            inspector = self.webview.get_inspector()
            inspector.show()

    def add_script(self, webview, event):
        # Add a JavaScript function to the webview that can be called from the loaded webpage
        script = "function _run(run){fetch('/execute$'+run);}"
        if event == WebKit2.LoadEvent.FINISHED:
            self.webview.run_javascript(script)

    def viewer(self, window_state):
        # Set the window state based on the provided argument
        if window_state == "maximized":
            self.maximize()
            self.show()
        elif window_state == "fullscreen":
            self.fullscreen()
            self.show()
        elif window_state == "frameless":
            self.set_decorated(False)
            self.show()
        elif window_state == "framelesstop":
            self.set_decorated(False)
            self.set_keep_above(True)
            self.show()
        elif window_state == "maximizedframelesstop":
            self.set_decorated(False)
            self.set_keep_above(True)
            self.maximize()
            self.show()
        elif window_state == "alwaystop":
            self.set_keep_above(True)
            self.show()
        else:
            self.show()

    @staticmethod
    def run():
        # Start the Gtk main loop
        Gtk.main()

    @staticmethod
    def close_window(webview):
        # Quit the Gtk main loop when the webview is closed
        Gtk.main_quit()

    def title_changed(self, webview, title):
        # Set the window title and WM_CLASS property based on the webview's title
        title = self.webview.get_title()
        os.system(f"xprop -id $(xprop -root '\t$0' _NET_ACTIVE_WINDOW|cut -f2) -f WM_CLASS 8s -set WM_CLASS '{title}'")
        self.set_title(title)

    def load_url(self, url):
        # Load the specified URL in the webview
        self.webview.load_uri(url)

    def set_size(self, width, height, window_state):
        # Set the window size and position based on the provided arguments
        display = Gdk.Display.get_primary_monitor(Gdk.Display.get_default())
        size = display.get_geometry()
        if width <= 0:
            width = size.width/2
        if height <= 0:
            height = size.height/2

        self.set_size_request(width, height)
        self.set_position(Gtk.WindowPosition.CENTER)
        if window_state == "fixed":
            self.set_resizable(False)

    def style(self, colorful):
        # Set the window and webview background color based on the provided argument
        if colorful == "black":
            self.override_background_color(
                Gtk.StateFlags.NORMAL,
                Gdk.RGBA(0, 0, 0, 1)
            )
            self.webview.set_background_color(Gdk.RGBA(0, 0, 0, 1))

        elif colorful == "transparent":
            screen = self.get_screen()
            visual = screen.get_rgba_visual()
            if visual is not None and screen.is_composited():
                self.set_visual(visual)
                self.override_background_color(
                    Gtk.StateFlags.NORMAL,
                    Gdk.RGBA(0, 0, 0, 0)
                )
                self.webview.set_background_color(Gdk.RGBA(0, 0, 0, 0))

        elif os.environ.get("XDG_CURRENT_DESKTOP") == "KDE":
            rgb = os.popen("kreadconfig5 --group WM --key activeBackground").read().split(",")

            if len(rgb) > 1:
                r, g, b = rgb if len(rgb) == 3 else rgb[:-1]
                r = float(int(r)/255)
                g = float(int(g)/255)
                b = float(int(b)/255)
                self.override_background_color(
                    Gtk.StateFlags.NORMAL,
                    Gdk.RGBA(r, g, b, 1)
                )
                self.webview.set_background_color(Gdk.RGBA(r, g, b, 1))
            else:
                color = self.get_style_context().get_background_color(Gtk.StateFlags.NORMAL)
                self.override_background_color(Gtk.StateFlags.NORMAL, color)
                self.webview.set_background_color(color)
        else:
            color = self.get_style_context().get_background_color(Gtk.StateFlags.NORMAL)
            self.override_background_color(Gtk.StateFlags.NORMAL, color)
            self.webview.set_background_color(color)