#!/usr/bin/env python3

import sys

import subprocess

from PyQt6.QtWidgets import (QApplication, QMainWindow, QWidget, QHBoxLayout, 

                             QVBoxLayout, QListWidget, QStackedWidget, QLabel, 

                             QPushButton, QCheckBox, QMessageBox)

from PyQt6.QtCore import Qt

class ShiftOSFeatures(QMainWindow):

    def __init__(self):

        super().__init__()

        self.setWindowTitle("ShiftOS Features")

        self.resize(650, 450)

        

        # Main Layout Setup

        central_widget = QWidget()

        self.setCentralWidget(central_widget)

        main_layout = QHBoxLayout(central_widget)

        

        # Sidebar Navigation

        self.sidebar = QListWidget()

        self.sidebar.setFixedWidth(180)

        self.sidebar.addItem("Experimental")

        self.sidebar.addItem("Settings")

        self.sidebar.currentRowChanged.connect(self.switch_page)

        

        # Stacked Widget to hold the different category pages

        self.pages = QStackedWidget()

        

        # Initialize Categories

        self.setup_experimental_page()

        self.setup_settings_page()

        

        main_layout.addWidget(self.sidebar)

        main_layout.addWidget(self.pages)

        

        # Set default selection to the first page

        self.sidebar.setCurrentRow(0)

    def setup_experimental_page(self):

        page = QWidget()

        layout = QVBoxLayout(page)

        

        title = QLabel("Experimental Features")

        title.setStyleSheet("font-size: 20px; font-weight: bold;")

        layout.addWidget(title)

        

        desc = QLabel("Enable or disable optional compatibility layers for ShiftOS. "

                      "Enabling these features will download additional system dependencies.")

        desc.setWordWrap(True)

        desc.setStyleSheet("margin-bottom: 20px;")

        layout.addWidget(desc)

        

        # Native Windows Toggle

        self.windows_toggle = QCheckBox("Enable Native Windows Support (.exe)")

        self.windows_toggle.setStyleSheet("font-size: 14px; padding: 5px;")

        self.windows_toggle.toggled.connect(self.toggle_windows)

        layout.addWidget(self.windows_toggle)

        

        # Native Android Toggle

        self.android_toggle = QCheckBox("Enable Native Android Support (.apk)")

        self.android_toggle.setStyleSheet("font-size: 14px; padding: 5px;")

        self.android_toggle.toggled.connect(self.toggle_android)

        layout.addWidget(self.android_toggle)

        

        layout.addStretch()

        self.pages.addWidget(page)

    def setup_settings_page(self):

        page = QWidget()

        layout = QVBoxLayout(page)

        

        title = QLabel("Settings")

        title.setStyleSheet("font-size: 20px; font-weight: bold;")

        layout.addWidget(title)

        

        desc = QLabel("Manage installed applications and configure active compatibility layers.")

        desc.setWordWrap(True)

        desc.setStyleSheet("margin-bottom: 20px;")

        layout.addWidget(desc)

        

        # Windows Management Button

        win_btn = QPushButton("Manage Windows apps and Settings")

        win_btn.setFixedHeight(40)

        win_btn.clicked.connect(self.manage_windows)

        layout.addWidget(win_btn)

        

        # Android Management Button

        and_btn = QPushButton("Manage Android apps and settings")

        and_btn.setFixedHeight(40)

        and_btn.clicked.connect(self.manage_android)

        layout.addWidget(and_btn)

        

        layout.addStretch()

        self.pages.addWidget(page)

    def switch_page(self, index):

        self.pages.setCurrentIndex(index)

    # --- Backend Trigger Methods ---

    def toggle_windows(self, checked):

        try:

            if checked:

                subprocess.run(["pkexec", "/usr/libexec/shiftos-features/Native-Windows.sh", "enable"], check=True)

            else:

                subprocess.run(["pkexec", "/usr/libexec/shiftos-features/Native-Windows.sh", "disable"], check=True)

        except subprocess.CalledProcessError:

            self.windows_toggle.blockSignals(True)

            self.windows_toggle.setChecked(not checked)

            self.windows_toggle.blockSignals(False)

            QMessageBox.critical(self, "Error", "Authentication failed or script encountered an error.")

    def toggle_android(self, checked):

        try:

            if checked:

                subprocess.run(["pkexec", "/usr/libexec/shiftos-features/Native-Android.sh", "enable"], check=True)

            else:

                subprocess.run(["pkexec", "/usr/libexec/shiftos-features/Native-Android.sh", "disable"], check=True)

        except subprocess.CalledProcessError:

            self.android_toggle.blockSignals(True)

            self.android_toggle.setChecked(not checked)

            self.android_toggle.blockSignals(False)

            QMessageBox.critical(self, "Error", "Authentication failed or script encountered an error.")

    def manage_windows(self):

        try:

            subprocess.Popen(["wine", "uninstaller"])

        except FileNotFoundError:

            QMessageBox.warning(self, "Not Enabled", "Windows support is not currently installed or enabled.")

    def manage_android(self):

        try:

            subprocess.Popen(["waydroid", "show-full-ui"])

        except FileNotFoundError:

            QMessageBox.warning(self, "Not Enabled", "Android support is not currently installed or enabled.")

if __name__ == "__main__":

    app = QApplication(sys.argv)

    window = ShiftOSFeatures()

    window.show()

    sys.exit(app.exec())

