import sys
import os
import time
import pwd
from PyQt6.QtWidgets import (
    QApplication,
    QCheckBox,
    QMainWindow,
    QWidget,
    QStackedWidget,
    QLayout,
    QVBoxLayout,
    QHBoxLayout,
    QGridLayout,
    QPushButton,
    QLabel,
    QScrollArea,
    QMessageBox,
    QStyledItemDelegate,
    QListView,
    QGraphicsOpacityEffect,
)
from PyQt6.QtCore import (
    Qt,
    QPropertyAnimation,
    QEasingCurve,
    QPoint,
    QTimer,
    QSize,
    QObject,
    pyqtSlot,
    pyqtSignal,
)
from PyQt6.QtGui import (
    QColor,
    QPalette,
    QFont,
    QPixmap,
    QKeyEvent,
    QImage,
    QPainter,
    QLinearGradient,
    QColorConstants,
)
from PyQt6.QtNetwork import QNetworkInformation
import webbrowser
import subprocess
from functools import partial
from helpers import (
    get_desktop_name,
    get_desktop_name2,
    is_installed,
    NetworkState,
    ConfList,
    Autostart,
    SystemArch,
)
from AppList import AppList
from version import version

DEFAULT_WIDTH = 900
_ = QApplication.translate


class Commands():
    def weblink(self, url):
        webbrowser.open_new_tab(url)
        time.sleep(.5)
        self.restore_focus()

    def restore_focus(self):
        fenetre_active = QApplication.activeWindow()
        if fenetre_active:
            fenetre_active.setFocus()

    def command(self, app):
        print(f"Lancement de {app}")
        if type(app) == list:
            cmd = app
            try:
                subprocess.Popen(cmd)
            except FileNotFoundError as e:
                print(f"Exception running {cmd}")
                print(e)
                message = QMessageBox(
                    QMessageBox.Icon.Warning,
                    _("mw-ui", "Launching command"),
                    _("mw-ui", "This command is not installed"),
                )
                message.exec()
        self.restore_focus()

    def install(self, app):
        if type(app) == list:
            cmd = app
            repo = cmd[1]
            # Check if repositories are enabled
            core = False
            updates = False
            tainted = False
            t_updates = False
            nonfree = False
            nf_updates = False
            core32 = False
            core32_updates = False
            try:
                active = subprocess.run(
                    ["urpmq", "--list-media", "active"], capture_output=True, text=True
                )
                active.check_returncode()
            except subprocess.CalledProcessError:
                print("Error with urpmq")
                return
            for line in active.stdout.splitlines():
                if line.startswith("Core Release"):
                    core = True
                if line.startswith("Core Updates"):
                    updates = True
                if line.startswith("Tainted Release"):
                    tainted = True
                if line.startswith("Tainted Updates"):
                    t_updates = True
                if line.startswith("Nonfree Release"):
                    nonfree = True
                if line.startswith("Nonfree Updates"):
                    nf_updates = True
                if line.startswith("Core 32bit Release"):
                    core32 = True
                if line.startswith("Core 32bit Updates"):
                    core32_updates = True
            if repo == "tainted" and not (tainted and t_updates):
                #   repo tainted not enabled
                message = QMessageBox(
                    QMessageBox.Icon.Warning,
                    _("mw-ui", "Application installation"),
                    #: {} will be replaced with the 'Media sources' translation
                    _("mw-ui", "Tainted repositories are not enabled. See the '{}' tab.").format(_("Sources","Media sources")),
                )
                message.exec()
                return
            if repo == "non-free" and not (nonfree and nf_updates):
                #   repo nonfree not enabled
                message = QMessageBox(
                    QMessageBox.Icon.Warning,
                    _("mw-ui", "Application installation"),
                    #: {} will be replaced with the 'Media sources' translation
                    _("mw-ui", "Nonfree repositories are not enabled. See the '{}' tab.").format(_("Sources","Media sources")),
                )
                message.exec()
                return
            if repo == "steam" and not (
                core32 and core32_updates and nonfree and nf_updates
            ):
                #   repo not enabled . See the '%1' tab
                message = QMessageBox(
                    QMessageBox.Icon.Warning,
                    _("mw-ui", "Application installation"),
                    #: {} will be replaced with the 'Media sources' translation
                    _("mw-ui", "Steam needs that Nonfree and Core 32bit repositories are enabled. See the '{}' tab.").format(_("Sources", "Media sources")),
                )
                message.exec()
                return
            if repo == "" and not (core and updates):
                #   repo not enabled
                message = QMessageBox(
                    QMessageBox.Icon.Warning,
                    _("mw-ui", "Application installation"),
                    #: {} will be replaced with the 'Media sources' translation
                    _("mw-ui", "Core repositories are not enabled. See the '{}' tab.").format(_("Sources","Media sources")),
                )
                message.exec()
                return
            proc = subprocess.Popen(["/usr/bin/gurpmi", cmd[0]])
            proc.wait()
        self.restore_focus()

    def install_and_launch(self, app):
        """
        app should be an array with the package name to install and then the command to launch
        """
        if type(app) == list:
            cmd = app
            # app should contain package, repository
            is_app_installed, inst_repo = is_installed(cmd[0])
            if not is_app_installed:
                proc = subprocess.Popen(["/usr/bin/gurpmi", cmd[0]])
                proc.wait()
            self.command([cmd[1]])
        self.restore_focus()

class SlidePage(QWidget, Commands):
    """Widget pour une page individuelle du diaporama"""

    def __init__(self, title):
        super().__init__()

        # Stocker le titre pour référence
        self.title = title   

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.setRenderHint(QPainter.RenderHint.Antialiasing)
        
        gradient = QLinearGradient(0, 0, 0, self.height())
        gradient.setColorAt(0.0, QColor("#262F45"))
        gradient.setColorAt(1.0, QColor("#2397D4"))
        painter.setBrush(gradient)
        painter.setPen(Qt.PenStyle.NoPen)
        painter.drawRect(self.rect())

    def resizeEvent(self, event):
        """Gère le redimensionnement de la slide"""
        super().resizeEvent(event)
        # Forcer le repaint pour les gradients
        self.update()

class Links(SlidePage):
    """Widget pour une page individuelle du diaporama"""

    def __init__(self):
        super().__init__(_("Links", "More information"))

        data = [
            {
                "name": _("Links", "Release notes"),
                "url":
                #: Translate only if the link is to a specific page for your language
                _("Links", "https://wiki.mageia.org/en/Mageia_10_Release_Notes"),
            },
            {
                "name": _("Links", "Forums"),
                "url":
                #: Translate only if the link is to a specific page for your language
                _("Links", "https://forums.mageia.org/en/"),
            },
            {
                "name": _("Links", "Community Center"),
                "url": "https://www.mageia.org/community/",
            },
            {
                "name": _("Links", "Errata"),
                "url":
                #: Translate only if the link is to a specific page for your language
                _("Links", "https://wiki.mageia.org/en/Mageia_10_Errata"),
            },
            {
                "name": _("Links", "Wiki"),
                "url":
                #: Translate only if the link is to a specific page for your language
                _("Links", "https://wiki.mageia.org/en/Documentation"),
            },
            {
                "name": _("Links", "Contribute"),
                "url": "https://www.mageia.org/contribute/",
            },
            {
                "name": _("Links", "Newcomers Howto"),
                "url":
                #: Translate only if the link is to a specific page for your language
                _("Links", "https://wiki.mageia.org/en/Newcomers_start_here"),
            },
            {
                "name": _("Links", "Chat Room"),
                #: Translate only if the link is to a specific page for your language
                "url": _("Links", "ircs://irc.libera.chat:6697/#mageia"),
            },
            {"name": _("Links", "Donations"), "url": "https://www.mageia.org/donate/"},
            {"name": _("Links", "Documentation"), "url": "https://www.mageia.org/doc/"},
            {"name": _("Links", "Bugs tracker"), "url": "https://bugs.mageia.org/"},
            {"name": _("Links", "Join us!"), "url": "https://identity.mageia.org/"},
        ]
        layout = QGridLayout()

        layout.setSpacing(30)
        col = 1
        for title in (_("Links","Documentation"), _("Links", "Support"), _("Links", "Community")):
            button = QLabel(title)
            button.setAlignment(Qt.AlignmentFlag.AlignCenter)
            button.setWordWrap(True)
            button.setStyleSheet(
                """
                QLabel {
                    color: white; font-size: 18px; padding: 2px 8px; font-weight: bold;
                }
                QLabel:pressed {
                    background-color: lightgrey; font-size: 18px; padding: 2px 8px; font-weight: bold;
                }
                """
            )
            layout.addWidget(button, 0, col)
            col += 1

        iter_data = iter(data)
        for row in range(2, 6):
            for col in range(1, 4):
                item = next(iter_data)
                button = MyPushButton(item["name"])
                button.clicked.connect(partial(self.weblink, item["url"]))
                layout.addWidget(button, row, col)
        # Dummy widgets at corner to center the buttons
        layout.addWidget(QWidget(), 6, 4)
        layout.addWidget(QWidget(), 0, 0)

        self.setLayout(layout)


class Welcome(SlidePage):
    def __init__(self, user):
        super().__init__(_("Welcome", "Welcome"))

        #  vertical layout
        layout = QVBoxLayout()

        # Add title
        if user == "live":
            title = _("Welcome", "Welcome to Mageia")
        else:
            title = _("Welcome", "Welcome to Mageia, {}").format(user)
        title_label = QLabel(title)
        title_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        title_label.setStyleSheet("font-size: 28px; color: white; font-weight: bold;")
        layout.addWidget(title_label)

        # Add the content
        if user == "live":
            content = _(
                "Welcome",
                "We are going to guide you through a few important pieces of information and<BR />help you to go further with Mageia.<BR /><BR />Now, click on <i> {} </i> to go to the first step.",
            ).format(_("Welcome", "Live mode"))
        else:
            content = _(
                "Welcome",
                "We are going to guide you through some important steps and help<BR />you with the configuration of your newly installed system.<BR /><BR />Now, click on <i>{}</i> to go to the first step.",
            ).format(_("Sources", "Media sources"))
        content_label = QLabel(content)
        content_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        content_label.setStyleSheet("font-size: 22px; color: white;")
        layout.addWidget(content_label)
        bottom_layout = QHBoxLayout()
        self.cb_launch = QCheckBox(_("mw-ui", "Show this window at startup"))
        self.cb_launch.clicked.connect(self.toogle_start)
        bottom_layout.setAlignment(Qt.AlignmentFlag.AlignRight)
        bottom_layout.addWidget(self.cb_launch)
        layout.addLayout(bottom_layout)
        self.autostart = Autostart()
        self.cb_launch.setChecked(self.autostart.isEnabled())

        self.setLayout(layout)


    def toogle_start(self):
        if self.cb_launch.isChecked():
            self.autostart.enable()
        else:
            self.autostart.disable()

class Sources(SlidePage):
    def __init__(self):
        super().__init__(_("Sources", "Media sources"))

        #  vertical layout
        layout = QVBoxLayout()
        layout.addStretch(0)
        grid = QGridLayout()

        title_label = QLabel(_("Sources", "Configure software repositories"))
        title_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        title_label.setStyleSheet("font-size: 28px; color: white; font-weight: bold;")
        layout.addWidget(title_label)

        explain_label = QLabel(_("Sources", "Mageia official repositories contain:"))
        explain_label.setStyleSheet("font-size: 14px; color: white;")
        layout.addWidget(explain_label)

        core_legend = GradientLegend(
            _("Sources", "core"), QColor("lightgreen"), QColor("green")
        )
        grid.addWidget(core_legend, 2, 0)
        core_label = QLabel(_("Sources", "- the free-open-source packages"))
        core_label.setStyleSheet("font-size: 14px; color: white;")
        core_label.setWordWrap(True)
        grid.addWidget(core_label, 2, 1)

        nonfree_legend = GradientLegend(
            _("Sources", "nonfree"), QColor("red"), QColor("darkred")
        )
        grid.addWidget(nonfree_legend, 3, 0)
        nonfree_label = QLabel(
            _(
                "Sources",
                "- closed-source programs, e.g. Nvidia proprietary drivers, non-free drivers for some Wi-Fi cards, etc",
            )
        )
        nonfree_label.setWordWrap(True)
        nonfree_label.setStyleSheet("font-size: 14px; color: white;")
        grid.addWidget(nonfree_label, 3, 1)

        tainted_legend = GradientLegend(
            _("Sources", "tainted"), QColor("red"), QColor("darkred")
        )
        grid.addWidget(tainted_legend, 4, 0)
        tainted_label = QLabel(
            _(
                "Sources",
                "- these packages (eg audio and video codecs needed for certain multimedia files or commercial DVDs) may infringe on patents or copyright laws in certain countries. ",
            )
        )
        tainted_label.setWordWrap(True)
        tainted_label.setStyleSheet("font-size: 14px; color: white;")
        grid.addWidget(tainted_label, 4, 1)

        backports_legend = GradientLegend(
            _("Sources", "backports"), QColor("lightgrey"), QColor("darkgrey")
        )
        grid.addWidget(backports_legend, 5, 0)
        backports_label = QLabel(
            _(
                "Sources",
                "- include new versions of packages, and new packages, that do not meet the updates policy.",
            )
        )
        backports_label.setWordWrap(True)
        backports_label.setStyleSheet("font-size: 14px; color: white;")
        grid.addWidget(backports_label, 5, 1)

        note_legend = GradientLegend(
            _("Sources", "Note! "), QColor("#e6c200"), QColor("#e6c200")
        )
        grid.addWidget(note_legend, 6, 0)
        note_label = QLabel(
            _(
                "Sources",
                """If you enabled the online repositories during installation, some media sources should be installed already. Otherwise, we will now configure these online repositories. If this computer will have access to the Internet, you can delete the <i>Local</i> entry from the list of repositories.""",
            )
        )
        note_label.setWordWrap(True)
        note_label.setTextFormat(Qt.TextFormat.RichText)
        note_label.setStyleSheet("font-size: 14px; color: white;")
        grid.addWidget(note_label, 6, 1)
        note_legend = GradientLegend(
            _("Sources", "Note! "), QColor("#e6c200"), QColor("#e6c200")
        )
        layout.addLayout(grid)
        content_label = QLabel(
            _(
                "Sources",
                "Now, please enable or disable the online repositories of your choice: click on the <i>Edit software repositories</i> button. Select at least the <i>release</i> and <i>updates</i> pair. <i>Debug</i> and <i>Testing</i> are for special cases.",
            )
            + "<BR />"
            + _(
                "Sources",
                "After you have checked and enabled the repositories you need, you can go to the next slide.",
            )
        )
        content_label.setWordWrap(True)
        content_label.setTextFormat(Qt.TextFormat.RichText) 
        content_label.setStyleSheet("font-size: 14px; color: white;")
        layout.addWidget(content_label)
        ns = NetworkState()
        with_net_button = not (ns.reachability() == QNetworkInformation.Reachability.Online) 
        if with_net_button:
            net_layout = QHBoxLayout()
            net_layout.addStretch(1)
            network_button = MyPushButton(_("Sources", "Configure network"))
            net_layout.addWidget(network_button)
            net_layout.addStretch(1)
            network_button.clicked.connect(
                partial(
                    self.command,
                    [
                        "/usr/bin/draknetcenter",
                    ],
                )
            )
        button_layout = QHBoxLayout()
        button_layout.addStretch(1)
        configure_button = MyPushButton(_("Sources", "Edit software sources") + " *")
        configure_button.clicked.connect(
            partial(
                self.command,
                [
                    "/usr/bin/drakrpm-edit-media",
                ],
            )
        )
        button_layout.addWidget(configure_button)
        button_layout.addStretch(1)
        if with_net_button:
            button_box = QHBoxLayout()
            button_box.addLayout(net_layout)
            button_box.addLayout(button_layout)
            layout.addLayout(button_box)
        else:
            layout.addLayout(button_layout)
        layout.addStretch(0)
        admin_label = QLabel(
                _("Sources", "(*) Administrator password is needed.")
            )
        admin = QHBoxLayout()
        admin.addWidget(admin_label)
        layout.addLayout(admin)
        self.setLayout(layout)


class Updates(SlidePage):
    def __init__(self):
        #  The button in the buttons bar
        super().__init__(_("Updates", "Update"))

        #  vertical layout
        layout = QVBoxLayout()
        layout.addStretch(0)

        title_label = QLabel(_("Updates", "How Mageia manages updates"))
        title_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        title_label.setStyleSheet("font-size: 28px; color: white; font-weight: bold;")
        layout.addWidget(title_label)

        content_label = QLabel(
            _(
                "Updates",
                "Mageia provides software which may be updated in order to fix bugs or security issues. It is highly recommended that you update your system regularly. \
An Update icon will appear in your task bar when new updates are available. To run the updates, just click on the icon below and give your user password - or use the Software Manager (root password). \
This is a background process and you will be able to use your computer normally during the updates.",
            )
        )
        content_label.setWordWrap(True)
        content_label.setStyleSheet("font-size: 16px; color: white; padding: 0px 60px")
        layout.addWidget(content_label)

        button_layout = QHBoxLayout()
        button_layout.addStretch(1)
        check_button = MyPushButton(_("Updates", "Check system updates") + " *")
        check_button.clicked.connect(
            partial(
                self.command,
                [
                    "/usr/bin/drakrpm-update",
                ],
            )
        )
        button_layout.addWidget(check_button)
        button_layout.addStretch(1)
        layout.addLayout(button_layout)
        button2_layout = QHBoxLayout()
        button2_layout.addStretch(1)
        advisories_button = MyPushButton(_("Updates", "Advisories of updates (en)"))
        advisories_button.clicked.connect(
            partial(self.weblink, "https://advisories.mageia.org/")
        )
        button2_layout.addWidget(advisories_button)
        button2_layout.addStretch(1)
        layout.addLayout(button2_layout)
        layout.addStretch(0)
        admin_label = QLabel(
                _("Sources", "(*) Administrator password is needed.")
            )
        admin = QHBoxLayout()
        admin.addWidget(admin_label)
        layout.addLayout(admin)

        self.setLayout(layout)


class Mcc(SlidePage):
    def __init__(self):
        #  The button in the buttons bar, shortcut for Mageia Control Center
        super().__init__(_("Mcc", "MCC"))

        #  vertical layout
        layout = QVBoxLayout()
        layout.addStretch(0)

        title_label = QLabel(_("Mcc", "Mageia Control Center"))
        title_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        title_label.setStyleSheet("font-size: 28px; color: white; font-weight: bold;")
        layout.addWidget(title_label)

        entries_list = [
            _("Mcc", "Software Management"),
            _("Mcc", "Hardware"),
            _("Mcc", "Network and Internet"),
            _("Mcc", "System"),
            _("Mcc", "Network Sharing"),
            _("Mcc", "Local Disks"),
            _("Mcc","Security"),
            _("Mcc", "Boot"),
        ]

        content_label = QLabel(
            _(
                "Mcc",
                "<b>Mageia Control Center</b> (aka drakconf) is a set of tools to help you configure your system.",
            )
            + "<BR>• "
            + "<BR>• ".join(entries_list)
        )
        content_label.setWordWrap(True)
        content_label.setStyleSheet("font-size: 14px; color: white; padding: 0px 60px")
        layout.addWidget(content_label)

        button_layout = QHBoxLayout()
        button_layout.addStretch(1)
        mcc_button = MyPushButton(_("Mcc", "Mageia Control Center") + " *")
        mcc_button.clicked.connect(
            partial(
                self.command,
                [
                    "/usr/bin/drakconf",
                ],
            )
        )
        button_layout.addWidget(mcc_button)
        button_layout.addStretch(1)
        layout.addLayout(button_layout)
        button2_layout = QHBoxLayout()
        button2_layout.addStretch(1)
        doc_button = MyPushButton(_("Mcc", "MCC documentation"))
        doc_button.clicked.connect(partial(self.weblink, "https://www.mageia.org/doc"))
        button2_layout.addWidget(doc_button)
        button2_layout.addStretch(1)
        layout.addLayout(button2_layout)
        layout.addStretch(0)
        admin_label = QLabel(
                _("Sources", "(*) Administrator password is needed.")
            )
        admin = QHBoxLayout()
        admin.addWidget(admin_label)
        layout.addLayout(admin)

        self.setLayout(layout)


class InstallSoftware(SlidePage):
    def __init__(self, mode_live):
        #:  The button in the buttons bar
        super().__init__(_("InstallSoftware", "Install software"))

        #  vertical layout
        layout = QVBoxLayout()
        layout.addStretch(0)

        title_label = QLabel(_("InstallSoftware", "Install and remove software"))
        title_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        title_label.setStyleSheet("font-size: 28px; color: white; font-weight: bold;")
        layout.addWidget(title_label)

        content_label = QLabel(
            _(
                "InstallSoftware",
                "With Mageia, you will find the software in the media repositories. Mageia users simply access these media via one of the Software Managers.",
            )
        )
        content_label.setWordWrap(True)
        content_label.setStyleSheet("font-size: 14px; color: white; padding: 0px 60px")
        layout.addWidget(content_label)

        button_layout = QHBoxLayout()
        button_layout.addStretch(1)
        #: Normally, this is not to translate
        button = MyPushButton(_("InstallSoftware", "Rpmdrake") + " *")
        button.clicked.connect(
            partial(
                self.command,
                [
                    "/usr/bin/rpmdrake",
                ],
            )
        )
        button_layout.addWidget(button)
        button_layout.addStretch(1)
        layout.addLayout(button_layout)
        button2_layout = QHBoxLayout()
        button2_layout.addStretch(1)
        #: Normally, this is not to translate
        doc_button = MyPushButton(_("InstallSoftware", "Dnfdragora"))
        doc_button.clicked.connect(
            partial(self.install_and_launch, ["dnfdragora", "/usr/bin/dnfdragora"])
        )
        button2_layout.addWidget(doc_button)
        button2_layout.addStretch(1)
        layout.addLayout(button2_layout)

        if not mode_live:
            text = _(
                "InstallSoftware",
                "The next slide shows a small selection of popular applications - any of which may be installed at this point.<BR/>",
            )
            text += _("InstallSoftware", "You can find a more detailed list here:")
        else:
            text = _("InstallSoftware", "You can find a more detailed list here:")
        content_label2 = QLabel(text)
        content_label2.setWordWrap(True)
        content_label2.setStyleSheet("font-size: 14px; color: white; padding: 0px 60px")
        layout.addWidget(content_label2)

        button3_layout = QHBoxLayout()
        button3_layout.addStretch(1)
        app_button = MyPushButton(_("InstallSoftware", "List of applications (wiki)"))
        app_button.clicked.connect(
            partial(
                self.weblink,
                _("InstallSoftware", "https://wiki.mageia.org/en/List_of_applications"),
            )
        )
        button3_layout.addWidget(app_button)
        button3_layout.addStretch(1)
        layout.addLayout(button3_layout)
        layout.addStretch(0)
        admin_label = QLabel(
                _("Sources", "(*) Administrator password is needed.")
            )
        admin = QHBoxLayout()
        admin.addWidget(admin_label)
        layout.addLayout(admin)

        self.setLayout(layout)


class Applications(SlidePage):
    def __init__(self):
        #:  The button in the buttons bar
        super().__init__(_("mw-ui", "Applications"))

        self.items = []
        layout = QVBoxLayout()
        content_layout = QHBoxLayout()
        nav_layout = QVBoxLayout()
        content_label = QLabel(
            "<b>"
            + _(
                "mw-ui",
                "Here is a small selection of popular applications - any of which may be installed or launched at this point.",
            )
            + "<BR />"
            + _("mw-ui", "Ensure that you have enabled the <i>Media sources</i>.")
            + "</b>"
        )
        content_label.setWordWrap(True)
        content_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        content_label.setStyleSheet(
            "font-size: 12px; color: black; background-color: gold;"
        )
        layout.addWidget(content_label)

        self.nav = Breadcrumb(lateral=True)
        entries = [
            #: category entries in Applications tab
            {"name": _("mw-ui", "Featured"), "group": "featured"},
            {"name": _("mw-ui", "Games"), "group": "games"},
            {"name": _("mw-ui", "Internet"), "group": "internet"},
            {"name": _("mw-ui", "Video"), "group": "video"},
            {"name": _("mw-ui", "Audio"), "group": "audio"},
            {"name": _("mw-ui", "Office"), "group": "office"},
            {"name": _("mw-ui", "Graphics"), "group": "graphics"},
            {"name": _("mw-ui", "System"), "group": "system"},
            {"name": _("mw-ui", "Programming"), "group": "programming"},
        ]
        i = 0
        for entry in entries:
            self.nav.add_item(entry["name"], i)
            i += 1
        self.nav.setFixedWidth(int(content_label.width() / 4))
        nav_layout.addWidget(self.nav)
        nav_layout.addStretch(0)

        content_layout.addLayout(nav_layout)
        layout.addLayout(content_layout)
        self.stack = AppListStack(entries, self.nav)
        self.nav.set_slideshow(self.stack)
        content_layout.addWidget(self.stack)
        self.setLayout(layout)

    def showEvent(self, event):
        self.stack.goto_slide(0)


class AppListStack(QStackedWidget):
    """Collection of stacked pages of applications lists"""

    def __init__(self, entries, nav):
        super().__init__()
        self.applist = []
        self.nav = nav
        self.current_index = 0
        for entry in entries:
            self.addWidget(AppListPage(entry["group"], self))

    def goto_slide(self, index):
        self.current_index = index
        self.setCurrentIndex(index)
        self.nav.set_active_item(index)

    def update_wide_item(self, index, except_group):
        """
        Update the entry in any other tab except the current designated by except_group
        """
        item = AppList[index]
        group = item["group"]
        name = item["name"]
        repo = item["repo"]
        group_cleaned = [x for x in group.split(" ") if x != except_group]
        # for each tab in Applist
        for i in range(self.count()):
            widget_page = self.widget(i)
            print(f"niveau 1 : i={i} {name} {group_cleaned} {widget_page.metaObject().className()}")
            if widget_page and widget_page.metaObject().className() == "AppListPage" and widget_page.group in group_cleaned:
                # for each item in tab
                for j in range(len(widget_page.children())):
                    widget_item = widget_page.children()[j]
                    if widget_item and widget_item.metaObject().className() == "ApplistItem":
                        if widget_item.name == name and widget_item.repo == repo:
                            print(f"niveau 2 : j={j}  {name}")
                            widget_page.update_item(widget_item, index)
                            return
                    
            

class AppListPage(QScrollArea):
    """Page of applications lists for a group"""

    def __init__(self, group, parent):
        super().__init__()
        arch32 = (SystemArch().arch == "32-bit")
        self.parent = parent
        self.group = group
        self.widget = SlidePage("")
        self.setStyleSheet("border: none;")
        # self.setBackgroundRole(QPalette.ColorRole.Window);
        self.list_layout = QVBoxLayout()
        for index in range(len(AppList)):
            item = AppList[index]
            if group in item["group"] and not (item["arch"] == "64bits" and arch32):
                self.list_layout.addWidget(
                    ApplistItem(
                        item["group"],
                        item["icon"],
                        item["name"],
                        item["title"],
                        item["description"],
                        item["repo"],
                        item["command"],
                        item["arch"],
                        index,
                        self,
                    )
                )
            index += 1
        self.list_layout.addStretch(0)
        self.widget.setLayout(self.list_layout)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
        self.setWidgetResizable(True)
        self.setWidget(self.widget)

    def update_item(self, widget, applist_index):
        index = self.list_layout.indexOf(widget)
        widget.deleteLater()
        item = AppList[applist_index]
        self.list_layout.insertWidget(
                index,
                ApplistItem(
                    item["group"],
                    item["icon"],
                    item["name"],
                    item["title"],
                    item["description"],
                    item["repo"],
                    item["command"],
                    item["arch"],
                    applist_index,
                    self,
                )
            )
        self.update()
        

    def update_wide_item(self, index):
        self.parent.update_wide_item(index, self.group)

class Configuration(SlidePage):
    def __init__(self):
        #:  The button in the buttons bar
        super().__init__(_("Configuration", "Your configuration"))

        #  vertical layout
        layout = QVBoxLayout()
        layout.addStretch(0)
        ns = NetworkState()
        cf = ConfList(ns)
        for config in cf.configuration:
            content_label = QLabel(config)
            content_label.setWordWrap(True)
            content_label.setStyleSheet("font-size: 14px; color: white; padding: 0px 60px")
            layout.addWidget(content_label)

        layout.addStretch(0)
        # About button
        button_layout = QHBoxLayout()
        button_layout.addStretch(1)
        about_button = MyPushButton(_("Configuration", "About"))
        about_button.clicked.connect(self.about)
        button_layout.addWidget(about_button)
        button_layout.addStretch(1)
        layout.addLayout(button_layout)
        layout.addStretch(0)

        self.setLayout(layout)

    def about(self):
        message = QMessageBox(
            QMessageBox.Icon.Warning,
            _("Configuration", "About Mageiawelcome"),
            #: {0} will be replaced with the release number, {1} with author's names
            _("Configuration", "Release {0}<br />Authors : {1}").format(
                version,
                "Daniel Napora, Papoteur, Antony Baker<br />"
            ),
        )
        #: Replace with the list of translator's names
        message.setDetailedText(
            _("Configuration", "Translators: English is the source language")
        )
        message.exec()


class Live(SlidePage):
    def __init__(self):
        #:  The button in the buttons bar
        super().__init__(_("Live", "Live mode"))

        #  vertical layout
        layout = QVBoxLayout()
        content_label = QLabel(
            _(
                "Live",
                "This mode allows you to try out Mageia without having to actually install it, or make any changes to your computer. However, the Live media also includes an Installer, which can be started when booting the media, or after booting into Live mode, like now.",
            )
        )
        content_label.setWordWrap(True)
        content_label.setStyleSheet("font-size: 14px; color: white; padding: 0px 60px")
        layout.addWidget(content_label)

        content_label2 = QLabel(
            _(
                "Live",
                "Any customization, including installation of additional software, will only survive until you reboot the system, unless you have added a persistence partition.",
            )
        )
        content_label2.setWordWrap(True)
        content_label2.setStyleSheet("font-size: 14px; color: white; padding: 0px 60px")
        layout.addWidget(content_label2)

        
        button_layout = QHBoxLayout()
        button_layout.addStretch(1)
        doc_button = MyPushButton(_("Live", "Installer documentation"))
        doc_button.clicked.connect(
            partial(
                self.weblink,
                #: the link to the local file can be adapted to your language if the documentation is translated
                _("Live", "file:///usr/share/doc/mageia/en/draklive/index.html"),
            )
        )
        button_layout.addWidget(doc_button)
        button_layout.addStretch(1)
        layout.addLayout(button_layout)

        self.setLayout(layout)


class Install(SlidePage):
    def __init__(self):
        #:  The button in the buttons bar
        super().__init__(_("Install", "Install"))

        #  vertical layout
        layout = QVBoxLayout()
        content_label = QLabel(
            _(
                "Install",
                "Here you can choose to permanently install this Mageia system on your computer. Any customizations you have made before launching the installer will be included.",
            )
        )
        content_label.setWordWrap(True)
        content_label.setStyleSheet("font-size: 14px; color: white; padding: 0px 60px")
        layout.addWidget(content_label)

        image = QPixmap("file:/usr/share/icons/draklive-install.png")
        content_image = QLabel()
        content_image.setPixmap(image)
        layout.addWidget(content_image)

        button_layout = QHBoxLayout()
        button_layout.addStretch(1)
        install_button = MyPushButton(_("Install", "Launch installation"))
        install_button.clicked.connect(
            partial(self.command, ["/usr/bin/draklive-install"])
        )
        button_layout.addWidget(install_button)
        button_layout.addStretch(1)
        layout.addLayout(button_layout)

        self.setLayout(layout)


class GradientLegend(QWidget):
    def __init__(self, label, top_color, bottom_color):
        super().__init__()
        self.top_color = top_color
        self.bottom_color = bottom_color
        layout = QHBoxLayout()
        label_widget = QLabel(label)
        label_widget.setStyleSheet(
        f"font-size: 14px; color: white; font-weight: bold;")
        label_widget.setAlignment(Qt.AlignmentFlag.AlignCenter | Qt.AlignmentFlag.AlignVCenter)
        self.setStyleSheet(
        f"background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 {top_color.name()}, stop:1 {bottom_color.name()})")
        layout.addWidget(label_widget)
        self.setLayout(layout)
        self.setFixedWidth(110)
        # layout.setSizeConstraint(QLayout.SizeConstraint.SetFixedSize)
        label_widget.setFixedHeight(30)


class ApplistItem(QWidget, Commands):
    """An element in application list"""

    def __init__(self, group, icon, name, title, description, repo, command, arch, index, parent):
        super().__init__()

        self.index = index
        self.parent = parent
        self.name = name
        self.group = group
        self.repo = repo
        layout = QHBoxLayout()
        layout.setContentsMargins(2, 2, 2, 2)
        image = QPixmap(icon)
        icon_widget = QLabel()
        icon_widget.setPixmap(image.scaled(32, 32))
        icon_widget.setFixedWidth(32)
        icon_widget.setFixedHeight(32)
        layout.addWidget(icon_widget)

        desc_layout = QVBoxLayout()
        name_label = QLabel(f"<b>{title}</b><br /><i>{description}</i>")
        name_label.setStyleSheet(
                """
                    QLabel {
                        color: white;
                        font-size: 14px;
                    }
                """
        )
        self.setMinimumHeight(int(QApplication.font().pointSize() * 5))
        desc_layout.addWidget(name_label)
        layout.addLayout(desc_layout)
        release, inst_repo = is_installed(name)
        if (not release) or (repo != inst_repo and inst_repo == ""):
            # the application is not yet installed, we display an Install button
            button = MyPushButton(_("mw-ui", "Install"))
            button.clicked.connect(partial(self.installation, [name, repo]))
        else:
            if command == "":
                # there is no command associated, we display it is installed
                button = QLabel(_("mw-ui", "Installed"))
                button.setAlignment(Qt.AlignmentFlag.AlignCenter)
                button.setStyleSheet(
                    """
                    QLabel {
                        color: white;
                        font-size: 12px;
                        padding: 0px 0px;
                    }
                    """
                )
            else:
                # display a button for launching command associated
                button = MyPushButton(_("mw-ui", "Launch"))
                button.clicked.connect(partial(self.command, [command]))
        button.setFixedWidth(QApplication.font().pointSize() * 10)
        layout.addWidget(button)
        # label for repository
        repo_label = QLabel(repo)
        repo_label.setFixedWidth(QApplication.font().pointSize() * 8)
        repo_label.setFixedHeight(int(QApplication.font().pointSize() * 2))
        repo_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        if repo != "":
            repo_label.setStyleSheet(
                """
                    QWidget {
                        background-color: "#FF4C4C";
                        border-radius: 3
                    }
                    QLabel {
                        color: white;
                        font-size: 14px;
                    }
                """
            )
        layout.addWidget(repo_label)
        self.setLayout(layout)
    
    def installation(self, args):
        self.install(args)
        # Reload the app widget also in other tabs
        self.parent.update_item(self, self.index)
        for group in self.group.split(" "):
            if group != self.parent.group:
                self.parent.update_wide_item(self.index)
                

class MyPushButton(QPushButton):
    """ For styling PushButton """
    def __init__(self, label):
        super().__init__(label)
        
        self.setStyleSheet(
            """
            QWidget {color: #262F45; background-color: lightgray; border-radius: 5px; padding: 8px 8px;}
            QPushButton {font-size: 14px;}
            QPushButton:pressed {
                    background-color: white;
                }
            """
        )
        

class BreadcrumbItem(QWidget):
    """Un élément du fil d'Ariane"""

    def __init__(self, title, index, parent=None, lateral=False):
        super().__init__(parent)
        self.title = title
        self.index = index
        self.active = False
        self.lateral = lateral

        # Layout horizontal pour l'élément
        layout = QHBoxLayout(self)

        # Étiquette pour le titre
        self.label = QLabel(title)
        self.label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.label.setWordWrap(True)
        self.update_style()

        layout.addWidget(self.label)
        self.setLayout(layout)

        # Rendre l'élément cliquable
        self.setCursor(Qt.CursorShape.PointingHandCursor)

    def set_active(self, active):
        """Définit si cet élément est actif ou non"""
        self.active = active
        self.update_style()

    def set_width(self, width):
        self.setMinimumWidth(width)

    def update_style(self):
        """Met à jour le style en fonction de l'état actif"""
        if self.lateral:
            if self.active:
                self.setStyleSheet(
                    """
                    QLabel {
                        color: white;
                        background-color: #262F45;
                        font-size: 14px;
                        border-radius: 5px;
                        min-height: 30px;
                    }
                    """
                )
            else:
                self.setStyleSheet(
                    """
                    QWidget:hover {
                        background-color: #2397D4;
                        border-radius: 5px;
                    }
                    QLabel {
                        color: white;
                        font-size: 14px;
                        min-height: 30px;
                    }
                    """
                )


        else:
            if self.active:
                self.setStyleSheet(
                    """
                    QWidget {
                        background-color: #2397D4;
                        border-radius: 5px;
                        min-height: 40px;
                    }
                    QLabel {
                        color: white;
                        font-size: 12px;
                        padding: 2px 8px;
                    }
                    """
                )
            else:
                self.setStyleSheet(
                    """
                    QWidget:hover {
                        background-color: #2397D4;
                        border-radius: 5px;
                        min-height: 40px;
                    }
                    QLabel {
                        background-color: white;
                        border-radius: 5px;
                        color: #2c3e50;
                        font-size: 12px;
                        padding: 2px 8px;
                    }
                    """
                )

    def mousePressEvent(self, event):
        """Détecte le clic sur l'élément"""
        if event.button() == Qt.MouseButton.LeftButton:
            # Émettre un signal personnalisé pour indiquer que cet élément a été cliqué
            self.parent().item_clicked(self.index)

    def showEvent(self, event):
        self.update_style()


class Breadcrumb(QWidget):
    """Fil d'Ariane for navigation in pages or in application groups (lateral)"""

    def __init__(self, parent=None, lateral=False):
        super().__init__(parent)

        # Container for elements
        self.lateral = lateral
        self.container = self
        palette = QPalette()
        palette.setColor(
            QPalette.ColorRole.Window, QColor("#20FFFFFF" if lateral else "#262F45")
        )
        self.container.setPalette(palette)
        self.container.setAutoFillBackground(True)

        self.layout = (
            QVBoxLayout(self.container) if lateral else QHBoxLayout(self.container)
        )
        self.layout.setSpacing(0 if lateral else 5)
        self.layout.setContentsMargins(5, 0, 5, 0)

        # List for storing elements
        self.items = []

        # reference to slideshow
        self.slideshow = None

    def set_slideshow(self, slideshow):
        """Associe le fil d'Ariane à un diaporama"""
        self.slideshow = slideshow

    def add_item(self, title, index):
        """Ajoute un élément au fil d'Ariane"""
        item = BreadcrumbItem(title, index, self, lateral=self.lateral)
        self.layout.addWidget(item)
        self.items.append(item)
        if not self.lateral:
            width = int(DEFAULT_WIDTH / len(self.items))
            for item in self.items:
                item.set_width(width)

    def set_active_item(self, index):
        """Définit l'élément actif"""
        for i, item in enumerate(self.items):
            item.set_active(i == index)

    def item_clicked(self, index):
        """Appelé quand un élément est cliqué"""
        if self.slideshow:
            self.slideshow.goto_slide(index)


class HoverButton(QPushButton):
    def __init__(self, label):
        super().__init__(label)
        self.setAttribute(Qt.WidgetAttribute.WA_Hover, True)
        self.effect = QGraphicsOpacityEffect()
        self.setGraphicsEffect(self.effect)

    def enterEvent(self, event):
        # Disparition progressive
        self.effect.setOpacity(50)
        super().enterEvent(event)

    def leaveEvent(self, event):
        # Réapparition progressive
        self.effect.setOpacity(1)
        super().leaveEvent(event)

class SlideShowWidget(QStackedWidget):
    """Widget de diaporama avec animations de transition"""

    def __init__(self):
        super().__init__()

        self.current_index = 0
        self.next_index = 0
        self.in_transition = False
        self.animation_duration = 800

        self.setContentsMargins(0, 0, 0, 0)

        # Fil d'Ariane associé
        self.breadcrumb = None
        self.admin_rights = []
        self.slides = []
        self.i = 0


    def resizeEvent(self, event):
        """Gère le redimensionnement du widget de slideshow"""
        self.i +=1
        for i in range(self.count()):
            widget = self.widget(i)
            if widget:
                widget.setGeometry(0, 0, event.size().width(), event.size().height())
                # Déclencher le repaint pour les slides avec gradient
                if hasattr(widget, 'update'):
                    widget.update()

    def set_breadcrumb(self, breadcrumb):
        """Associe un fil d'Ariane au diaporama"""
        self.breadcrumb = breadcrumb
        self.breadcrumb.set_slideshow(self)

    def add_slide(self, slide, admin_rights=False):
        """Ajoute une diapositive au diaporama"""
        self.slides.append(slide)
        self.addWidget(slide)
        if self.breadcrumb:
            width = self.breadcrumb.add_item(slide.title, self.count() - 1)
            self.admin_rights.append(admin_rights)

    def next_slide(self):
        """Passe à la diapositive suivante avec animation"""
        if self.in_transition or self.count() <= 1:
            return

        self.next_index = (self.current_index + 1) % self.count()
        self._animate_horizontal_transition(True)  # True = at right

    def previous_slide(self):
        """Passe à la diapositive précédente avec animation"""
        if self.in_transition or self.count() <= 1:
            return

        self.next_index = (self.current_index - 1) % self.count()
        self._animate_horizontal_transition(False)  # False = at left

    def goto_slide(self, index):
        """Va directement à une diapositive spécifique"""
        if (
            self.in_transition
            or index == self.current_index
            or index < 0
            or index >= self.count()
        ):
            return

        self.next_index = index

        # Déterminer la direction de l'animation
        forward = self.next_index > self.current_index
        if self.next_index == 0 and self.current_index == self.count() - 1:
            forward = True
        elif self.next_index == self.count() - 1 and self.current_index == 0:
            forward = False

        self._animate_horizontal_transition(forward)

    def _animate_horizontal_transition(self, forward=True):
        """Anime la transition horizontale entre deux diapositives"""
        self.in_transition = True

        # Mettre à jour le fil d'Ariane
        if self.breadcrumb:
            self.breadcrumb.set_active_item(self.next_index)

        # Widget actuel
        current_widget = self.widget(self.current_index)

        # Widget suivant (à afficher)
        next_widget = self.widget(self.next_index)

        # S'assurer que le widget suivant est visible mais pas encore au premier plan
        next_widget.setGeometry(current_widget.geometry())
        next_widget.show()
        next_widget.raise_()

        # Positionner les widgets pour l'animation
        offset = self.width()

        # Position de départ du widget suivant (en dehors de l'écran)
        if forward:  # Glissement vers la gauche (la nouvelle diapo vient de droite)
            next_widget.move(offset, 0)
        else:  # Glissement vers la droite (la nouvelle diapo vient de gauche)
            next_widget.move(-offset, 0)

        # Animation pour le widget actuel
        self.current_anim = QPropertyAnimation(current_widget, b"pos")
        self.current_anim.setDuration(self.animation_duration)
        self.current_anim.setStartValue(current_widget.pos())
        if forward:  # Glissement vers la gauche
            self.current_anim.setEndValue(QPoint(-offset, 0))
        else:  # Glissement vers la droite
            self.current_anim.setEndValue(QPoint(offset, 0))
        self.current_anim.setEasingCurve(QEasingCurve.Type.OutCubic)

        # Animation pour le nouveau widget
        self.next_anim = QPropertyAnimation(next_widget, b"pos")
        self.next_anim.setDuration(self.animation_duration)
        self.next_anim.setStartValue(next_widget.pos())
        self.next_anim.setEndValue(QPoint(0, 0))
        self.next_anim.setEasingCurve(QEasingCurve.Type.OutCubic)

        # Connecter le signal de fin d'animation
        self.next_anim.finished.connect(self._finish_transition)

        # Démarrer les animations
        self.current_anim.start()
        self.next_anim.start()

    def _finish_transition(self):
        """Nettoie après la fin de l'animation"""
        # Mettre à jour l'indice courant
        self.current_index = self.next_index

        # Cacher tous les widgets sauf celui à l'indice courant
        for i in range(self.count()):
            if i != self.current_index:
                self.widget(i).hide()

        # Terminer la transition
        self.in_transition = False

class Banner(QLabel):
    def __init__(self):
        super().__init__()

    def resizeEvent(self, event):
        size = event.size()
        # if the width of the picture matches the one of banner, we can't reduce the size of the window
        new_picture = self.createGradientBanner(size.width() - 4, 120)
        self.setPixmap(QPixmap.fromImage(new_picture))
        self.setAlignment(Qt.AlignmentFlag.AlignCenter)
        # super().resizeEvent(event)


    def createGradientBanner(self, width, height):
        image = QImage(width, height, QImage.Format.Format_ARGB32)
        image.fill(QColorConstants.Transparent)

        painter = QPainter(image)
        logo = QPixmap("img/mageia-2013-black-alpha.png")
        gradient = QLinearGradient(0, 0, width, 0)
        gradient.setColorAt(0.0, QColorConstants.Svg.lightgray)
        gradient.setColorAt(1.0, QColorConstants.White)

        painter.fillRect(0, 0, width, height, gradient)
        painter.setCompositionMode(QPainter.CompositionMode.CompositionMode_SourceOver)
        painter.drawPixmap(
            QPoint((width - logo.width()) // 2, (height - logo.height()) // 2), logo
        )
        painter.end()

        return image

class SlideShowApp(QMainWindow):
    """Application principale de diaporama"""

    def __init__(self):
        super().__init__()
        #: the application title
        self.setWindowTitle( _("Welcome", "Welcome to Mageia"))
        self.setMinimumSize(DEFAULT_WIDTH, 550)  # Taille minimale
        self.setGeometry(100, 100, 100 + DEFAULT_WIDTH, 700)
        
        # central Widget
        central_widget = QWidget()
        main_layout = QGridLayout(central_widget)
        main_layout.setContentsMargins(0, 0, 0, 0)
        banner = Banner()
        main_layout.addWidget(banner, 0, 0)

        # Créer le fil d'Ariane
        self.breadcrumb = Breadcrumb()
        main_layout.addWidget(self.breadcrumb, 1, 0)

        # Build diaporama
        self.slideshow = SlideShowWidget()
        self.slideshow.set_breadcrumb(self.breadcrumb)

        self.slideshow.add_slide(Welcome(self.username()))
        if not self.mode_live():
            self.slideshow.add_slide(Sources(), admin_rights=True)
            self.slideshow.add_slide(Updates(), admin_rights=True)
            self.slideshow.add_slide(Mcc(), admin_rights=True)
            self.slideshow.add_slide(
                InstallSoftware(self.mode_live()), admin_rights=True
            )
            self.slideshow.add_slide(Applications())
            self.slideshow.add_slide(Configuration())
        else:
            # Live mode
            self.slideshow.add_slide(Live())
            self.slideshow.add_slide(Mcc())
            self.slideshow.add_slide(InstallSoftware(self.mode_live()))
            # Install on HD
            self.slideshow.add_slide(Install())
        self.slideshow.add_slide(Links())
        
        # Mettre à jour le fil d'Ariane pour la première diapositive
        self.breadcrumb.set_active_item(0)

        # Ajouter au layout principal
        main_layout.addWidget(self.slideshow, 2, 0)

        self.setCentralWidget(central_widget)
        self.setFocus()

    def username(self):
        user = pwd.getpwuid(os.getuid())[4]  # pw_gecos, i e the real name
        if user == "":
            user = pwd.getpwuid(os.getuid())[0]  # login
        return user
    
    def mode_live(self):
        return os.path.exists("/run/mgalive")

    def keyPressEvent(self, event):
        if isinstance(event, QKeyEvent):
            key = event.key()
            if key == Qt.Key.Key_Right:
                self.slideshow.next_slide()
            elif key == Qt.Key.Key_Left:
                self.slideshow.previous_slide()
            else:
                super().keyPressEvent(event)
                

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = SlideShowApp()
    window.show()
    sys.exit(app.exec())
