#####################################################################################
#    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/>.

#   © 2024 Emmanuel Gyimah Annor. All rights reserved.
#####################################################################################

import psutil
import resources_rc

from PySide6.QtGui import QIcon
from PySide6.QtCore import Qt, QSize
from PySide6.QtWidgets import (
    QWidget, QVBoxLayout, QHBoxLayout, QFrame, QMenuBar, QLabel, QPushButton, QGridLayout,
    QProgressBar, QTableWidget, QTableWidgetItem, QStackedWidget, QLineEdit, QComboBox, QTextEdit,
    QHeaderView, QMenu, QButtonGroup, QSizePolicy, QStyledItemDelegate, QStyleOptionViewItem, QStyle
)


class NoFocusDelegate(QStyledItemDelegate):
    def paint(self, painter, option, index):
        if option.state & QStyle.State_HasFocus:
            option.state = option.state ^ QStyle.State_HasFocus
        super().paint(painter, option, index)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setWindowTitle("Download Manager")
        MainWindow.resize(1250, 750)
        MainWindow.setMinimumSize(800, 600)

        self.central = QWidget(MainWindow)
        MainWindow.setCentralWidget(self.central)

        self.main_layout = QVBoxLayout(self.central)
        self.main_layout.setContentsMargins(0, 0, 0, 0)
        self.main_layout.setSpacing(0)

        # Top Menu Bar
        self.top_frame = QFrame()
        self.top_frame.setObjectName("TopFrame")
        self.top_frame.setFixedHeight(35)

        self.top_layout = QHBoxLayout(self.top_frame)
        self.top_layout.setContentsMargins(0, 0, 0, 0)

        self.menubar = QMenuBar()

        # TASK
        # self.task_menu = self.menubar.addMenu("Task")
        # self.task_menu.addAction("Add New Download")
        # self.task_menu.addAction("Import List")

        # FILE
        self.file_menu = self.menubar.addMenu("File")
        self.open_file_menu = QMenu("Open", self.file_menu)
        self.file_menu.addMenu(self.open_file_menu)
        self.open_file_menu.addAction("")
        
        # self.file_menu.addAction("Open File")
        self.export_dl = self.file_menu.addAction("Export Downloads List")
        self.quitt = self.file_menu.addAction("Exit")

        # DOWNLOADS
        self.downloads_menu = self.menubar.addMenu("Downloads")
        self.downloads_menu.addAction("Resume All")
        self.downloads_menu.addAction("Stop All")
        self.downloads_menu.addAction("Clear Completed")

        # VIEW
        self.view_menu = self.menubar.addMenu("View")
        self.refresh_table = self.view_menu.addAction("Refresh Table")
        self.sort_menu = QMenu("Sort By", self.view_menu)
        self.view_menu.addMenu(self.sort_menu)
        self.status_action = self.sort_menu.addAction("Sort by Status")
        self.name_action = self.sort_menu.addAction("Sort by Name")
        self.progress_action = self.sort_menu.addAction("Sort by Progress")

        

        # TOOLS
        self.tools_menu = self.menubar.addMenu("Tools")

        self.settings_action = self.tools_menu.addAction("Settings")  # 🛠 Save it!

        self.browser_extension_menu = QMenu("Browser Extension", self.tools_menu)
        self.tools_menu.addMenu(self.browser_extension_menu)

        # Assuming icons are under :/icons/ in your .qrc file
        self.chrome_action = self.browser_extension_menu.addAction(
            QIcon(":/icons/google-chrome.svg"), "Chrome"
        )
        self.firefox_action = self.browser_extension_menu.addAction(
            QIcon(":/icons/firefox.svg"), "Firefox"
        )
        self.edge_action = self.browser_extension_menu.addAction(
            QIcon(":/icons/microsoft-edge.svg"), "Edge"
        )


        

        # HELP
        self.help_menu = self.menubar.addMenu("Help")
        self.help_menu.addAction("About")
        self.help_menu.addAction("Check for Updates")
        self.help_menu.addAction("User Guide")
        self.help_menu.addAction("Visual Tutorials")
        self.help_menu.addAction("Report Issues")
        # self.help_menu.addAction("Supported Sites")

        self.top_layout.addWidget(self.menubar)
        self.main_layout.addWidget(self.top_frame)

        # Content Area
        self.content_frame = QFrame()
        self.content_layout = QHBoxLayout(self.content_frame)
        self.content_layout.setContentsMargins(10, 10, 10, 10)
        self.content_layout.setSpacing(10)

        # Sidebar with square buttons
        self.sidebar_frame = QFrame()
        self.sidebar_frame.setObjectName("SidebarFrame")
        self.sidebar_frame.setFixedWidth(180)
        self.sidebar_layout = QVBoxLayout(self.sidebar_frame)
        self.sidebar_layout.setSpacing(8)  # 👈 No spacing between buttons
        self.sidebar_layout.setContentsMargins(0, 0, 0, 0)  # 👈 No margins, full edge-to-edge
        
        

        self.page_buttons = []
        icon_names = [":/icons/add.png", ":/icons/table.png", ":/icons/terminal.png"]
        self.button_group = QButtonGroup()
        self.button_group.setExclusive(True)  # 👈 ensures only one button is checked

        for idx, icon_path in enumerate(icon_names):
            btn = QPushButton()
            btn.setFixedSize(150, 100)
            btn.setIcon(QIcon(icon_path))
            btn.setIconSize(QSize(150, 150))
            btn.setCheckable(True)
            btn.setStyleSheet("""
                QPushButton {
                    background-color: transparent;
                    border: none;
                    padding: 80px;
                    text-align: center;
                }
                QPushButton:hover {
                    background-color: #1e1e1e;

                }
                QPushButton:checked {
                    border-bottom: 4px solid qlineargradient(x1:1, y1:0, x2:0, y2:0,
                    stop: 0 #00C853, stop: 1 #003d1f); /* 👈 indicator bar */
                    border-radius: 0px;
                    
                }
                
                
            """)
            btn.setCursor(Qt.PointingHandCursor)
            self.page_buttons.append(btn)
            self.sidebar_layout.addWidget(btn)
            self.button_group.addButton(btn, id=idx)  # 👈 Add to group with an ID

            if idx == 0:
                btn.setChecked(True)

        self.sidebar_layout.addStretch()
        total_gb, used_gb, free_gb, percent = self.get_disk_usage("/")
        self.disk_label = QLabel(f"Free: {free_gb} GB / {total_gb} GB")
        self.disk_label.setStyleSheet("color: white; font-size: 11px;")

        self.disk_bar = QProgressBar()
        self.disk_bar.setMinimum(0)
        self.disk_bar.setMaximum(100)
        self.disk_bar.setValue(percent)
        self.disk_bar.setTextVisible(False)
        self.disk_bar.setFixedHeight(10)
        self.disk_bar.setStyleSheet("""
            QProgressBar {
                background-color: #2a2a2a;
                border: 1px solid #444;
                border-radius: 5px;
            }
            QProgressBar::chunk {
                background-color: #00C853;
                border-radius: 5px;
            }
        """)

        self.sidebar_layout.addWidget(self.disk_label)
        self.sidebar_layout.addWidget(self.disk_bar)
        self.content_layout.addWidget(self.sidebar_frame)

        # Stacked Pages
        self.stacked_widget = QStackedWidget()

        # Page 0 - Add Download
        self.page_add = QWidget()
        self.page_add_layout = QVBoxLayout(self.page_add)
        self.page_add_layout.setContentsMargins(40, 40, 40, 40)
        self.page_add_layout.setSpacing(20)
        self.page_add.setStyleSheet("""
            QWidget#page_add {
                background-color: qlineargradient(
                    x1: 0, y1: 0, x2: 1, y2: 1,
                    stop: 0 #0F1B14,
                    stop: 1 #050708
                );
                border-radius: 14px;
            }
        """)
        self.page_add.setObjectName("page_add")

        # === LINK + Retry
        self.link_input = QLineEdit()
        self.link_input.setPlaceholderText("Place download link here")
        self.link_input.setStyleSheet("""
            QLineEdit {
                background-color: rgba(28, 28, 30, 0.55);
                color: #e0e0e0;
                border: 1px solid rgba(255, 255, 255, 0.05);
                border-radius: 6px;
                padding: 6px 10px;
            }
            QLineEdit:hover {
                border: 1px solid rgba(111, 255, 176, 0.18);
            }
        """)

        self.retry_btn = QPushButton("")
        self.retry_btn.setIcon(QIcon(":/icons/retry.png"))
        self.retry_btn.setIconSize(QSize(42, 42))
        self.retry_btn.setFixedSize(50, 50)
        self.retry_btn.setStyleSheet("""
            QPushButton {
                color: white;
                border-radius: 20px;
                padding: 4px; /* 👈 required to prevent offset */
            }
            QPushButton:hover {
                background-color: rgba(0, 255, 180, 0.1);  /* subtle hover */
                
            }
            
        """)

        link_row = QHBoxLayout()
        link_row.addWidget(self.link_input)
        link_row.addWidget(self.retry_btn)
        self.page_add_layout.addLayout(link_row)

        # === PROGRESS BAR
        self.progress = QProgressBar()
        self.progress.setValue(0)
        self.progress.setTextVisible(True)
        self.progress.setFormat("0%")
        self.progress.setStyleSheet("""
            QProgressBar {
                background-color: #2b2b2b;
                border: 1px solid #555;
                border-radius: 5px;
                height: 20px;
                color: white;
                text-align: center;
            }
            QProgressBar::chunk {
                background-color: #ff33cc;
                border-radius: 5px;
            }
        """)
        self.page_add_layout.addWidget(self.progress)

        # === FOLDER INPUT
        folder_section = QVBoxLayout()
        folder_section.setSpacing(6)

        self.folder_label = QLabel("CHOOSE FOLDER")
        self.folder_label.setStyleSheet("color: #aaa; font-size: 11px;")
        folder_section.addWidget(self.folder_label)

        folder_row = QHBoxLayout()
        self.folder_input = QLineEdit("/home/username/Downloads")
        self.folder_input.setStyleSheet(self.link_input.styleSheet())
        self.folder_btn = QPushButton()
        self.folder_btn.setIcon(QIcon(":/icons/folder.png"))
        self.folder_btn.setIconSize(QSize(42, 42))
        self.folder_btn.setFixedSize(55, 55)
        self.folder_btn.setStyleSheet(self.retry_btn.styleSheet())
        folder_row.addWidget(self.folder_input)
        folder_row.addWidget(self.folder_btn)
        folder_section.addLayout(folder_row)

        self.page_add_layout.addLayout(folder_section)

        # === FILENAME INPUT
        filename_section = QVBoxLayout()
        filename_section.setSpacing(6)

        self.filename_label = QLabel("FILENAME")
        self.filename_label.setStyleSheet("color: #aaa; font-size: 11px;")
        filename_section.addWidget(self.filename_label)

        self.filename_input = QLineEdit()
        self.filename_input.setPlaceholderText("Filename goes here")
        self.filename_input.setStyleSheet(self.link_input.styleSheet())
        filename_section.addWidget(self.filename_input)

        self.page_add_layout.addLayout(filename_section)

        # === CONTENT ROW (Thumbnail + Right Panel)
        content_row = QHBoxLayout()
        content_row.setSpacing(20)

        # LEFT PANEL (Thumbnail)
        left_frame = QFrame()
        left_frame.setFrameShape(QFrame.StyledPanel)
        left_frame.setStyleSheet("""
            QFrame {
                
                border-radius: 10px;
                
            }
        """)
        left_layout = QVBoxLayout(left_frame)
        left_layout.setContentsMargins(12, 12, 12, 12)
        left_layout.setAlignment(Qt.AlignCenter)

        self.thumbnail = QLabel()
        self.thumbnail.setPixmap(QIcon(":/icons/thumbnail-default.png").pixmap(400, 350))
        self.thumbnail.setAlignment(Qt.AlignCenter)
        self.thumbnail.setFixedSize(400, 350)
        self.thumbnail.setStyleSheet("border-radius: 8px;")
        left_layout.addWidget(self.thumbnail)

        

        # RIGHT PANEL
        right_frame = QFrame()
        right_frame.setFrameShape(QFrame.StyledPanel)
        right_frame.setStyleSheet("""
            QFrame {
                border: 1px solid #333;
                border-radius: 10px;
                background-color: rgba(20, 20, 20, 0.2);
            }
        """)
        right_layout = QVBoxLayout(right_frame)
        right_layout.setContentsMargins(16, 16, 16, 16)
        right_layout.setSpacing(12)

        self.combo1 = QComboBox()
        self.combo2 = QComboBox()
        self.combo3 = QComboBox()
        self.combo1.setStyleSheet(
            
            """

            QLineEdit, QComboBox {
                background-color: rgba(28, 28, 30, 0.55);  /* Neutral frosted charcoal */
                color: #e0e0e0;
                border: 1px solid rgba(255, 255, 255, 0.05);
                border-radius: 6px;
                padding: 6px 10px;
            }

            QLineEdit:hover, QComboBox:hover {
                border: 1px solid rgba(111, 255, 176, 0.18);  /* subtle emerald glow on hover */
            }

            QComboBox::drop-down {
                border: none;
                background-color: transparent;
            }

            QComboBox QAbstractItemView {
                background-color: rgba(20, 25, 20, 0.95);
                border: 1px solid rgba(60, 200, 120, 0.25);
                selection-background-color: #2DE099;
                color: white;
            }
        """
        )
        self.combo2.setStyleSheet(self.combo1.styleSheet())
        self.combo3.setStyleSheet(self.combo1.styleSheet())

        self.combo1.setFixedWidth(360)
        self.combo2.setFixedWidth(360)
        self.combo3.setFixedWidth(360)

        combo1_row = QHBoxLayout()
        self.combo1_label = QLabel("Download Item:")
        self.combo1_label.setStyleSheet("color: #ccc; font-size: 12px;")
        combo1_row.addWidget(self.combo1_label)
        combo1_row.addWidget(self.combo1)

        combo2_row = QHBoxLayout()
        self.combo2_label = QLabel("Resolution:")
        self.combo2_label.setStyleSheet("color: #ccc; font-size: 12px;")
        combo2_row.addWidget(self.combo2_label)
        combo2_row.addWidget(self.combo2)

        combo3_row = QHBoxLayout()
        self.combo3_label = QLabel("Queue:")
        self.combo3_label.setStyleSheet("color: #ccc; font-size: 12px;")
        combo3_row.addWidget(self.combo3_label)
        combo3_row.addWidget(self.combo3)

        right_layout.addLayout(combo1_row)
        right_layout.addLayout(combo2_row)
        right_layout.addLayout(combo3_row)

        # METADATA
        info_row = QGridLayout()
        info_row.setHorizontalSpacing(16)

        self.size_label = QLabel("Size:")
        self.size_value = QLabel("Unknown")
        self.type_label = QLabel("Type:")
        self.type_value = QLabel("Unknown")
        self.protocol_label = QLabel("Protocol:")
        self.protocol_value = QLabel("--")
        self.resume_label = QLabel("Resumable:")
        self.resume_value = QLabel("No")

        labels = [self.size_label, self.type_label, self.protocol_label, self.resume_label]
        values = [self.size_value, self.type_value, self.protocol_value, self.resume_value]

        for lbl in labels:
            lbl.setStyleSheet("color: #eee; font-size: 12px; background: transparent; border: none;")
        for val in values:
            val.setStyleSheet("color: #eee; font-size: 12px; background: transparent; border: none;")

        info_row.addWidget(self.size_label, 0, 0)
        info_row.addWidget(self.size_value, 0, 1)
        info_row.addWidget(self.type_label, 0, 2)
        info_row.addWidget(self.type_value, 0, 3)
        info_row.addWidget(self.protocol_label, 1, 0)
        info_row.addWidget(self.protocol_value, 1, 1)
        info_row.addWidget(self.resume_label, 1, 2)
        info_row.addWidget(self.resume_value, 1, 3)

        right_layout.addLayout(info_row)

        # BUTTONS
        button_row = QHBoxLayout()
        button_row.setSpacing(10)
        button_row.setContentsMargins(4, 1, 4, 4)
        self.playlist_btn = QPushButton("")
        self.playlist_btn.setIcon(QIcon(":/icons/playlist.png"))
        self.playlist_btn.setIconSize(QSize(62, 62))
        self.playlist_btn.setFixedSize(75, 75)
        self.playlist_btn.setStyleSheet(self.retry_btn.styleSheet())
        self.playlist_btn.setStyleSheet("""
            QPushButton {
                background-color: transparent;
                border-radius: 20px;
                padding: 5px; /* 👈 required to prevent offset */
            }
            QPushButton:hover {
                background-color: rgba(0, 255, 180, 0.08);  /* clean, modern hover */
            }
        """)
        self.download_btn = QPushButton()
        
        self.download_btn.setText("")  # Clear hidden text
        self.download_btn.setIcon(QIcon(":/icons/download.png"))
        self.download_btn.setIconSize(QSize(62, 62))
        self.download_btn.setFixedSize(75, 75)
        self.download_btn.setStyleSheet("""
            QPushButton {
                background-color: transparent;
                border-radius: 20px;
                padding: 5px; /* 👈 required to prevent offset */
            }
            QPushButton:hover {
                background-color: rgba(0, 255, 180, 0.08);  /* clean, modern hover */
            }
        """)

        self.download_btn.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        self.download_btn.setContentsMargins(0, 0, 0, 0)

        



        button_row.addWidget(self.playlist_btn)
        button_row.addWidget(self.download_btn)
        right_layout.addLayout(button_row)


        # NEW: Middle Frame
        middle_frame = QFrame()
        middle_frame.setFrameShape(QFrame.StyledPanel)
        middle_layout = QVBoxLayout(middle_frame)
        middle_layout.setContentsMargins(12, 12, 12, 12)
        middle_layout.setSpacing(8)

        # Optional placeholder content
        middle_label = QLabel("Subtitles / Extras")
        middle_label.setStyleSheet("color: white;")
        middle_layout.addWidget(middle_label)

        # content_row.addWidget(middle_frame, stretch=1)  # 👈 NEW PANEL
        content_row.addWidget(left_frame, stretch=1)
       
        content_row.addWidget(right_frame, stretch=1)

        # === WRAP CONTENT IN CONTAINER THAT STRETCHES
        content_container = QVBoxLayout()
        content_container.setContentsMargins(0, 0, 0, 0)
        content_container.setSpacing(0)
        content_container.addLayout(content_row)
        content_container.addStretch(1)

        self.page_add_layout.addLayout(content_container)


        self.stacked_widget.addWidget(self.page_add)
        

        # Page 1 - Toolbar + Table
        self.toolbar_frame = QFrame()
        self.toolbar_frame.setStyleSheet("""
            QFrame {
                background-color: rgba(10, 18, 15, 0.6);
                border-radius: 8px;
            }
        """)
        self.toolbar_layout = QHBoxLayout(self.toolbar_frame)
        self.toolbar_layout.setSpacing(10)
        self.toolbar_layout.setContentsMargins(10, 10, 10, 10)

        self.toolbar_buttons = {}
        icon_map = {
            "Resume": ":/icons/play.svg",
            "Pause": ":/icons/pause.svg",
            "Stop All": ":/icons/stop_all.svg",
            "Delete": ":/icons/trash.svg",
            "Delete All": ":/icons/multi_trash.svg",
            "Refresh": ":/icons/refresh.png",
            "Resume All": ":/icons/resume_all.svg",
            "Schedule All": ":/icons/sche.png",
            "Settings": ":/icons/setting.svg",
            "Download Window": ":/icons/d_window.png",
            "Queues": ":/icons/queues.png"
            
        }

        for label, icon_path in icon_map.items():
            btn = QPushButton()
            btn.setToolTip(label)
            icon = QIcon(icon_path)
            if not icon.isNull():
                btn.setIcon(icon)
                btn.setIconSize(QSize(42, 42))
                btn.setFixedSize(50, 50)
            else:
                btn.setText(label)
            btn.setStyleSheet("""
                QPushButton {
                    background-color: rgba(30, 40, 30, 0.2);
                    border-radius: 10px;
                    padding: 6px;
                    border: none;
                }
                QPushButton:hover {
                    background-color: rgba(0, 255, 180, 0.15);
                }
            """)
            
                    
            self.toolbar_buttons[label] = btn
            self.toolbar_layout.addWidget(btn)

        self.toolbar_layout.addStretch()

        self.table = QTableWidget(5, 9)
        self.table.setHorizontalHeaderLabels(["ID", "Name", "Progress", "Speed", "Left", "Done", "Size", "Status", "I"])
        #self.table.setColumnWidth(1, 200)  # Adjust the value (e.g., 240) as needed
        

        self.table.setSelectionBehavior(QTableWidget.SelectRows)
        self.table.setAlternatingRowColors(True)
        self.table.verticalHeader().setVisible(False)
        self.table.horizontalHeader().setSectionResizeMode(0, QHeaderView.Stretch)
        self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Interactive)
        self.table.setColumnWidth(1, 240)  # Customize this value as needed

        self.table.horizontalHeader().setSectionResizeMode(2, QHeaderView.Stretch)
        self.table.horizontalHeader().setSectionResizeMode(3, QHeaderView.Stretch)
        self.table.horizontalHeader().setSectionResizeMode(4, QHeaderView.Stretch)
        self.table.horizontalHeader().setSectionResizeMode(5, QHeaderView.Stretch)
        self.table.horizontalHeader().setSectionResizeMode(6, QHeaderView.Stretch)
        self.table.setItemDelegate(NoFocusDelegate())
        self.table.setStyleSheet("""
            QTableWidget {
                background-color: qlineargradient(
                x1: 0, y1: 0, x2: 1, y2: 1,
                stop: 0 #0F1B14,
                stop: 1 #050708
                ); 
                color: white;
                border: none;
                padding: 6px 16px;
                border: 1px solid rgba(0, 255, 180, 0.1);
                gridline-color: rgba(255, 255, 255, 0.05);
                font-size: 13px;
            }
            
            QTableWidget::item:alternate {
                background-color: rgba(255, 255, 255, 0.03);
            }
            QTableWidget::item {
                background-color: transparent;
            }
            
            QHeaderView::section {
                background-color: qlineargradient(
                x1: 0, y1: 0, x2: 1, y2: 1,
                stop: 0 #0F1B14,
                stop: 1 #050708
                );
                color: white;
                padding: 4px;
                border: none;
                font-weight: bold;
            }
            QTableWidget::item:selected {
                background-color: rgba(0, 255, 180, 0.25);
            }
            QTableWidget::item:focus {
                outline: none;
                border: none;
            }
            
        """)

        for row in range(5):
            for col in range(9):
                if col == 2:
                    progress = QProgressBar()
                    progress.setRange(0, 100)
                    progress.setTextVisible(True)
                    progress.setStyleSheet("""
                        QProgressBar {
                            background-color: rgba(20, 20, 20, 0.4);
                            border: 1px solid rgba(0, 255, 180, 0.1);
                            border-radius: 4px;
                            text-align: center;
                            color: white;
                        }
                        QProgressBar::chunk {
                            background-color: #00C853;
                            border-radius: 4px;
                        }
                    """)
                    self.table.setCellWidget(row, col, progress)
                else:
                    self.table.setItem(row, col, QTableWidgetItem(f"Sample {row}-{col}"))

        self.page_table = QWidget()
        self.page_table_layout = QVBoxLayout(self.page_table)
        self.page_table_layout.setContentsMargins(0, 0, 0, 0)
        self.page_table_layout.setSpacing(8)
        self.page_table_layout.addWidget(self.toolbar_frame)
        self.page_table_layout.addWidget(self.table)
        self.stacked_widget.addWidget(self.page_table)


        # Page 2 - Terminal Logs
        self.page_terminal = QWidget()
        self.page_terminal_layout = QVBoxLayout(self.page_terminal)
        # self.page_terminal.setStyleSheet("background-color: #1e1e1e;")
        self.page_terminal.setStyleSheet("""
            QWidget#page_add {
                background-color: qlineargradient(
                    x1: 0, y1: 0, x2: 1, y2: 1,
                    stop: 0 #0F1B14,
                    stop: 1 #050708
                );
                border-radius: 14px;
            }
        """)
        self.page_terminal_layout.setSpacing(10)

        # Top controls row
        top_row = QHBoxLayout()
        top_row.setContentsMargins(10, 10, 10, 0)

        self.detailed_label = QLabel("Detailed Events")
        self.detailed_label.setStyleSheet("color: white; font-size: 12px;")

        self.log_level_label = QLabel("Log Level:")
        self.log_level_label.setStyleSheet("color: white; font-size: 12px;")

        self.log_level_combo = QComboBox()
        self.log_level_combo.addItems(["1", "2", "3"])
        self.log_level_combo.setStyleSheet("""
            QComboBox {
                background-color: #2b2b2b;
                color: white;
                border: 1px solid #444;
                padding: 4px;
                border-radius: 4px;
            }
        """)

        self.log_clear_btn = QPushButton("Clear")
        self.log_clear_btn.setStyleSheet("""
            QPushButton {
                background-color: #00C853;
                color: black;
                padding: 4px 10px;
                border-radius: 6px;
                font-weight: bold;
            }
            QPushButton:hover {
                background-color: #33d47c;
            }
        """)

        top_row.addWidget(self.detailed_label)
        top_row.addStretch()
        top_row.addWidget(self.log_level_label)
        top_row.addWidget(self.log_level_combo)
        top_row.addWidget(self.log_clear_btn)

        # Log display QTextEdit
        self.terminal_log = QTextEdit()
        self.terminal_log.setReadOnly(True)
        self.terminal_log.setStyleSheet("""
            QTextEdit {
                background-color: qlineargradient(
                x1: 0, y1: 0, x2: 1, y2: 1,
                stop: 0 #0F1B14,
                stop: 1 #050708
                );
                border-radius: 16px;
                color: white;
                border: 1px solid #333;
                font-family: Consolas, Courier New, monospace;
                font-size: 12px;
                padding: 10px;
            }
        """)

        self.page_terminal_layout.addLayout(top_row)
        self.page_terminal_layout.addWidget(self.terminal_log)
        self.stacked_widget.addWidget(self.page_terminal)


        for i, btn in enumerate(self.page_buttons):
            btn.clicked.connect(lambda _, idx=i: self.stacked_widget.setCurrentIndex(idx))

        self.page_buttons[0].setChecked(True)
        self.stacked_widget.setCurrentIndex(0)

        self.content_layout.addWidget(self.stacked_widget)
        self.main_layout.addWidget(self.content_frame)

        # Global footer status bar
        self.status_frame = QFrame()
        self.status_frame.setObjectName("StatusFrame")
        self.status_frame.setFixedHeight(30)
        self.status_layout = QHBoxLayout(self.status_frame)
        self.status_layout.setContentsMargins(10, 0, 10, 0)
        self.status_layout.setSpacing(30)
        self.status_frame.setSizePolicy(self.content_frame.sizePolicy())

        self.brand = QLabel("YourBrand")
        self.status_layout.addWidget(self.brand)
        self.status_layout.addStretch(1)        
        self.status_layout.addWidget(QLabel("Status:"))
        self.status_value = QLabel("")
        self.status_layout.addWidget(self.status_value)
        self.status_layout.addStretch(1)
        self.status_layout.addWidget(QLabel("Speed:"))
        self.speed_value = QLabel("")
        self.status_layout.addWidget(self.speed_value)
        self.status_layout.addStretch(1)
        self.version_value = QLabel("")
        self.status_layout.addWidget(self.version_value)
        self.version_value.setStyleSheet("""
            color: #4CAF50;
            font-weight: bold;
            padding: 5px 10px;
            border-radius: 10px;
            background: rgba(76, 175, 80, 0.1);
        """)

        # Wrapper frame to enforce the same horizontal margins as content_frame
        self.status_wrapper = QFrame()
        self.status_wrapper_layout = QHBoxLayout(self.status_wrapper)
        self.status_wrapper_layout.setContentsMargins(195, 0, 10, 12)  # Match content_frame margins
        self.status_wrapper_layout.setSpacing(290)
        self.status_wrapper_layout.addWidget(self.status_frame)

        self.main_layout.addWidget(self.status_wrapper)
        
    

    def get_disk_usage(self, path="/"):
        usage = psutil.disk_usage(path)
        total_gb = usage.total // (1024**3)
        used_gb = usage.used // (1024**3)
        free_gb = total_gb - used_gb
        percent = usage.percent
        return total_gb, used_gb, free_gb, percent