#!/usr/bin/python3

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio, GdkPixbuf

import gettext

# i18n
APP = "cinnamon-welcome"
LOCALE_DIR = "/usr/share/locale"
gettext.bindtextdomain(APP, LOCALE_DIR)
gettext.textdomain(APP)
_ = gettext.gettext

# --- Page Content ---
# Using placeholder icon names. These would be replaced with actual image files.
# Example: 'go-next' is a standard icon name in many icon themes.

CINNAMON_PAGES = [
    {
        "title": _("Welcome to Cinnamon!"),
        "text": _("This tour will highlight some of the key features of your new desktop."),
        "icon": "dialog-information"
    },
    {
        "title": _("The Panel"),
        "text": _("The panel at the bottom of your screen hosts the menu, window list, and system tray. You can customize it by right-clicking on it."),
        "icon": "preferences-system-windows"
    },
    {
        "title": _("The Main Menu"),
        "text": _("Click the Cinnamon icon to open the menu, where you can launch applications, access your favorite places, and search for files."),
        "icon": "start-here-cinnamon"
    },
    {
        "title": _("Workspaces"),
        "text": _("Use workspaces to organize your open windows. You can switch between them using the workspace switcher in the panel or with keyboard shortcuts."),
        "icon": "preferences-desktop-display"
    },
    {
        "title": _("All Set!"),
        "text": _("Enjoy using Cinnamon! You can find more settings and customization options in the System Settings application."),
        "icon": "dialog-ok-apply"
    }
]

ACCRETION_OS_PAGES = [
    {
        "title": _("Welcome to AccretionOS!"),
        "text": _("This tour will show you the unique features of AccretionOS, built on the solid foundation of Cinnamon."),
        "icon": "dialog-information"
    },
    {
        "title": _("Integrated Tools"),
        "text": _("AccretionOS comes with special pre-installed tools and configurations to enhance your productivity out of the box."),
        "icon": "system-utilities"
    },
    {
        "title": _("Custom Theming"),
        "text": _("Enjoy the custom AccretionOS look and feel, designed for a clean and modern experience."),
        "icon": "preferences-desktop-theme"
    },
    {
        "title": _("Ready to Go!"),
        "text": _("Enjoy using AccretionOS! For support, visit our community forums."),
        "icon": "dialog-ok-apply"
    }
]

ACCRETION_THANK_YOU_PAGE = {
    "page_type": "thank_you",
    "title": _("Thank you for using AcreetionOS!"),
    "text": _("This operating system is made with love by the developers, for the community, and by the community.\nWe have no desire for corporate interests; we are here to make a widely-accessible operating system for the masses."),
    "icon": "emblem-favorite",
    "links": [
        {"uri": "https://discord.acreetionos.org", "label": _("Get Support on Discord")},
        {"uri": "https://acreetionos.org", "label": _("Visit our Website")}
    ]
}


class WelcomeWindow(Gtk.Window):
    def __init__(self):
        super().__init__()
        self.set_title(_("Welcome Tour"))
        self.set_default_size(800, 550)
        self.set_position(Gtk.WindowPosition.CENTER)
        self.connect("destroy", Gtk.main_quit)
        self.set_border_width(10)

        try:
            settings = Gio.Settings.new("org.cinnamon.welcome")
            self.accretion_mode = settings.get_boolean("accretion-os-enabled")
            self.show_thank_you = settings.get_boolean("show-accretion-thank-you-page")
        except Exception as e:
            print(f"Could not load GSettings: {e}. Defaulting to standard mode.")
            self.accretion_mode = False
            self.show_thank_you = False

        pages = ACCRETION_OS_PAGES if self.accretion_mode else CINNAMON_PAGES
        if self.accretion_mode and self.show_thank_you:
            pages.append(ACCRETION_THANK_YOU_PAGE)

        self.num_pages = len(pages)

        header = Gtk.HeaderBar()
        header.set_show_close_button(True)
        header.props.title = self.get_title()
        self.set_titlebar(header)

        self.stack = Gtk.Stack()
        self.stack.set_transition_type(Gtk.StackTransitionType.SLIDE_LEFT_RIGHT)
        self.stack.set_transition_duration(500)

        for i, page_data in enumerate(pages):
            if page_data.get("page_type") == "thank_you":
                page = self.create_thank_you_page(page_data)
            else:
                page = self.create_page(page_data["icon"], page_data["title"], page_data["text"])
            self.stack.add_titled(page, f"page{i}", page_data["title"])

        # Navigation buttons
        self.prev_button = Gtk.Button(label=_("Previous"))
        self.prev_button.connect("clicked", self.on_nav_clicked)
        self.next_button = Gtk.Button(label=_("Next"))
        self.next_button.connect("clicked", self.on_nav_clicked)
        self.next_button.get_style_context().add_class("suggested-action")

        nav_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=10)
        nav_box.pack_start(self.prev_button, False, False, 0)
        nav_box.pack_end(self.next_button, False, False, 0)
        
        self.reveal = Gtk.Revealer()
        self.reveal.set_reveal_child(True)
        self.reveal.add(nav_box)

        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=15)
        vbox.set_border_width(10)
        vbox.pack_start(self.stack, True, True, 0)
        vbox.pack_start(self.reveal, False, True, 0)
        self.add(vbox)
        
        self.update_nav_buttons()

    def create_page(self, icon_name, title, text):
        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=30)
        vbox.set_valign(Gtk.Align.CENTER)
        vbox.set_halign(Gtk.Align.CENTER)

        icon = Gtk.Image.new_from_icon_name(icon_name, Gtk.IconSize.DIALOG)
        icon.set_pixel_size(128)

        title_label = Gtk.Label()
        title_label.set_markup(f"<span size='xx-large' weight='bold'>{title}</span>")
        
        text_label = Gtk.Label(label=text)
        text_label.set_max_width_chars(60)
        text_label.set_line_wrap(True)
        text_label.set_justify(Gtk.Justification.CENTER)
        
        vbox.pack_start(icon, False, False, 0)
        vbox.pack_start(title_label, False, False, 0)
        vbox.pack_start(text_label, False, False, 0)
        return vbox

    def create_thank_you_page(self, page_data):
        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=20)
        vbox.set_valign(Gtk.Align.CENTER)
        vbox.set_halign(Gtk.Align.CENTER)

        icon = Gtk.Image.new_from_icon_name(page_data["icon"], Gtk.IconSize.DIALOG)
        icon.set_pixel_size(96)

        title_label = Gtk.Label()
        title_label.set_markup(f"<span size='xx-large' weight='bold'>{page_data['title']}</span>")
        
        text_label = Gtk.Label(label=page_data["text"])
        text_label.set_max_width_chars(70)
        text_label.set_line_wrap(True)
        text_label.set_justify(Gtk.Justification.CENTER)

        links_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=15)
        links_box.set_halign(Gtk.Align.CENTER)
        for link in page_data["links"]:
            link_button = Gtk.LinkButton(uri=link["uri"], label=link["label"])
            links_box.pack_start(link_button, False, False, 0)

        vbox.pack_start(icon, False, False, 0)
        vbox.pack_start(title_label, False, False, 0)
        vbox.pack_start(text_label, False, False, 0)
        vbox.pack_start(links_box, False, False, 15)
        return vbox

    def on_nav_clicked(self, button):
        current = self.stack.get_visible_child_name()
        current_idx = int(current.replace("page", ""))

        if button == self.next_button:
            if current_idx == self.num_pages - 1:
                Gtk.main_quit()
            else:
                new_idx = current_idx + 1
        else: # prev_button
            new_idx = current_idx - 1
            
        self.stack.set_visible_child_name(f"page{new_idx}")
        self.update_nav_buttons()

    def update_nav_buttons(self):
        current = self.stack.get_visible_child_name()
        current_idx = int(current.replace("page", ""))

        self.prev_button.set_sensitive(current_idx > 0)
        
        if current_idx == self.num_pages - 1:
            self.next_button.set_label(_("Finish"))
        else:
            self.next_button.set_label(_("Next"))

if __name__ == "__main__":
    win = WelcomeWindow()
    win.show_all()
    Gtk.main()
