import gi
import subprocess
import sys
import os

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk

class WelcomeScreen(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="TROMjaro Welcome")

        icon_path = "tromjaro-welcome.svg"
        if os.path.exists(icon_path):
            self.set_icon_from_file(icon_path)
        else:
            print("Error: Icon not found:", icon_path)
        
        self.set_border_width(10)
        self.set_default_size(800, 600)

        self.pages = [
            {"title": "Welcome to TROMjaro",
             "description": "TROMjaro is free and open source operating system which fights against the influences of trade!",
             "image_path": "tromjaro-welcome-thumb.png",
             "with_button": False},
            {"title": "Choose a Layout",
             "description": "Make it morph into any shape you love!",
             "image_path": "tromjaro-layout-switcher-thumb.png",
             "with_button": True,
             "button_text": "Open the Layout Switcher",
             "button_handler": self.on_layout_switcher_button_clicked},
            {"title": "Choose a Theme",
             "description": "Choose between the many variations of themes!",
             "image_path": "tromjaro-theme-switcher-thumb.png",
             "with_button": True,
             "button_text": "Open the Theme Switcher",
             "button_handler": self.on_theme_switcher_button_clicked},
            {"title": "Choose a Background",
             "description": "Pick a cool background to go along with your theme!",
             "image_path": "wallpapers-thumbnail.png",
             "with_button": True,
             "button_text": "Change the Wallpaper",
             "button_handler": self.on_background_button_clicked},
            {"title": "Setup the Internet Content Blocker",
             "description": "Setup your Operating System to block the pesky ads and trackers, system-wide.",
             "image_path": "content-blocker-thumbnail.png",
             "with_button": True,
             "button_text": "Setup Internet Content Blocker",
             "button_handler": self.on_content_blocker_button_clicked},
            {"title": "Settings Manager",
             "description": "You will find all of the settings into one single place.",
             "image_path": "settings-thumbnail.png",
             "with_button": True,
             "button_text": "Open Settings Manager",
             "button_handler": self.on_settings_button_clicked},
            {"title": "Donation",
             "description": "If you want to help for the cause of fighting the force of trade, please, consider a donation. Thank you!",
             "image_path": "donation-thumb.png",
             "with_button": True,
             "button_text": "Donate",
             "button_handler": self.on_donate_button_clicked},
        ]

        self.current_page = 0
        self.create_ui()

    def create_ui(self):
        for child in self.get_children():
            self.remove(child)

        grid = Gtk.Grid()
        grid.set_column_homogeneous(True)
        grid.set_row_spacing(10)

        header_label = Gtk.Label(label=self.pages[self.current_page]["title"].upper())
        header_label.set_markup("<big><b>{}</b></big>".format(self.pages[self.current_page]["title"].upper()))
        description_label = Gtk.Label(label=self.pages[self.current_page]["description"])

        header_label.set_hexpand(True)
        description_label.set_hexpand(True)

        grid.attach(header_label, 0, 0, 2, 1)
        grid.attach(description_label, 0, 1, 2, 1)

        image_path = self.pages[self.current_page]["image_path"]
        if image_path and os.path.exists(image_path):
            image = Gtk.Image.new_from_file(image_path)
        else:
            print("Error: Image not found:", image_path)
            image = Gtk.Image.new_from_icon_name("image-missing", Gtk.IconSize.DIALOG)
        image.set_size_request(600, 400)
        grid.attach(image, 0, 2, 2, 1)

        if self.pages[self.current_page]["with_button"]:
            button_text = self.pages[self.current_page]["button_text"]
            button_handler = self.pages[self.current_page]["button_handler"]
            button = Gtk.Button(label=button_text.upper())
            button.set_size_request(60, -1)  # Set fixed button width to 60px
            button.set_name("accent_button")  # Set the name of the button to "accent_button"
            button.set_margin_top(20)
            button.set_margin_bottom(20)
            button.connect("clicked", button_handler)
            grid.attach(button, 0, 3, 2, 1)

        if self.current_page > 0:
            left_button = Gtk.Button(label="Previous")
            left_button.set_size_request(60, -1)  # Set fixed button width to 60px
            left_button.set_halign(Gtk.Align.START)  # Align the button to the left
            left_button.set_margin_top(20)
            left_button.set_margin_bottom(20)
            left_button.connect("clicked", self.on_previous_button_clicked)
            grid.attach(left_button, 0, 4, 1, 1)

        if self.current_page < len(self.pages) - 1:
            right_button = Gtk.Button(label="Next")
            right_button.set_size_request(60, -1)  # Set fixed button width to 60px
            right_button.set_halign(Gtk.Align.END)  # Align the button to the right
            right_button.set_margin_top(20)
            right_button.set_margin_bottom(20)
            right_button.connect("clicked", self.on_next_button_clicked)
            grid.attach(right_button, 1, 4, 1, 1)
        
        if self.current_page == len(self.pages) - 1:
            right_button = Gtk.Button(label="Finish")
            right_button.connect("clicked", self.on_finish_button_clicked)
            right_button.set_size_request(60, -1)  # Set fixed button width to 60px
            right_button.set_halign(Gtk.Align.END)  # Align the button to the right
            right_button.set_margin_top(20)
            right_button.set_margin_bottom(20)
            grid.attach(right_button, 1, 4, 1, 1)

        checkbox = Gtk.CheckButton(label="Open the Welcome Screen on every boot")
        checkbox.set_active(True)
        checkbox.connect("toggled", self.on_checkbox_toggled)
        checkbox.set_halign(Gtk.Align.CENTER)
        grid.attach(checkbox, 0, 5, 2, 1)

        # Set up CSS style provider for the accent button
        css_provider = Gtk.CssProvider()
        css_provider.load_from_data(b"""
            button#accent_button {
                background-color: @theme_selected_bg_color;
                color: @theme_selected_fg_color;
                border: none;
            }
            button#accent_button:hover {
                background-color: @theme_selected_bg_color;
                color: @theme_selected_fg_color;
                border: none;
            }
        """)

        context = Gtk.StyleContext()
        context.add_provider_for_screen(Gdk.Screen.get_default(), css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

        self.add(grid)
        self.show_all()

    def on_previous_button_clicked(self, button):
        if self.current_page > 0:
            self.current_page -= 1
            self.create_ui()

    def on_layout_switcher_button_clicked(self, button):
        subprocess.run(["tromjaro-layout-switcher"])

    def on_theme_switcher_button_clicked(self, button):
        subprocess.run(["tromjaro-theme-switcher"])

    def on_background_button_clicked(self, button):
        subprocess.run(["xfdesktop-settings"])

    def on_content_blocker_button_clicked(self, button):
        subprocess.run(["tblockg"])

    def on_settings_button_clicked(self, button):
        subprocess.run(["xfce4-settings-manager"])

    def on_donate_button_clicked(self, button):
        subprocess.run(["xdg-open", "https://www.tromsite.com/donate/"])

    def on_checkbox_toggled(self, checkbox):
        if checkbox.get_active():
            print("Welcome Screen will be opened on every boot.")
        else:
            print("Welcome Screen will not be opened on every boot.")

    def on_next_button_clicked(self, button):
        if self.current_page < len(self.pages) - 1:
            self.current_page += 1
            self.create_ui()

    def on_finish_button_clicked(self, button):
        sys.exit(0)

win = WelcomeScreen()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()