import sys
import os
import json
from PyQt6.QtWidgets import (QApplication, QWidget, QVBoxLayout, QHBoxLayout, 
                             QPushButton, QLabel, QStackedWidget)
from PyQt6.QtCore import Qt, QSize
from PyQt6.QtGui import QIcon, QFont

from pages.status_page import StatusPage
from utils.aio_print_api import AIOPrrintSystemAPI

def load_config():
    config_path = os.path.join(os.path.dirname(__file__), "config.json")
    try:
        with open(config_path, "r", encoding="utf-8") as f:
            return json.load(f)
    except Exception as e:
        print(f"Failed to load config: {e}")
        return {}

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        # 初始化 API 客户端
        config = load_config()
        self.api_client = AIOPrrintSystemAPI(
            api_url=config.get("api_url", "http://127.0.0.1:5001/api/v1"),
            api_key=config.get("api_key", "")
        )
        self.init_ui()

    def init_ui(self):
        # 整体布局
        main_layout = QVBoxLayout(self)
        main_layout.setContentsMargins(0, 0, 0, 0)
        main_layout.setSpacing(0)

        # 顶部主显示区 (使用 QStackedWidget 切换不同页面)
        self.stacked_widget = QStackedWidget()
        self.stacked_widget.setStyleSheet("background-color: #555555;") # 深灰色主显示区
        
        # 添加测试页面
        self.page_status = StatusPage(self.api_client)
        self.page_control = QLabel("控制面板")
        self.page_settings = QLabel("系统设置")
        
        self.stacked_widget.addWidget(self.page_status)
        for page in [self.page_control, self.page_settings]:
            page.setAlignment(Qt.AlignmentFlag.AlignCenter)
            page.setStyleSheet("color: white; font-size: 48px; font-weight: bold;")
            self.stacked_widget.addWidget(page)

        # 底部按钮区
        bottom_layout = QHBoxLayout()
        bottom_layout.setContentsMargins(10, 10, 10, 10)
        bottom_layout.setSpacing(20)
        
        # 定义底部区域背景
        bottom_widget = QWidget()
        bottom_widget.setStyleSheet("background-color: #333333;")
        bottom_widget.setLayout(bottom_layout)
        bottom_widget.setFixedHeight(120) # 为触摸优化的按钮高度

        # 创建触摸友好的按钮
        self.btn_status = self.create_nav_button("状态", self.switchToStatus)
        self.btn_control = self.create_nav_button("控制", self.switchToControl)
        self.btn_settings = self.create_nav_button("设置", self.switchToSettings)

        bottom_layout.addWidget(self.btn_status)
        bottom_layout.addWidget(self.btn_control)
        bottom_layout.addWidget(self.btn_settings)

        # 将主显示区和底部按钮区加入整体布局
        main_layout.addWidget(self.stacked_widget)
        main_layout.addWidget(bottom_widget)

        self.setLayout(main_layout)
        self.setStyleSheet("background-color: #666666;") # 整体灰色背景

    def create_nav_button(self, text, callback):
        # 如果有图标，可以使用 btn.setIcon(QIcon("path/to/icon.png"))
        btn = QPushButton(text)
        # 为触摸屏优化：大字体，增加内边距
        btn.setStyleSheet("""
            QPushButton {
                background-color: #444444;
                color: white;
                border: 2px solid #555555;
                border-radius: 10px;
                font-size: 32px;
                padding: 10px;
            }
            QPushButton:pressed {
                background-color: #222222;
                border: 2px solid #4CAF50;
            }
        """)
        btn.setSizePolicy(btn.sizePolicy().Policy.Expanding, btn.sizePolicy().Policy.Expanding)
        btn.clicked.connect(callback)
        return btn

    def switchToStatus(self):
        self.stacked_widget.setCurrentIndex(0)

    def switchToControl(self):
        self.stacked_widget.setCurrentIndex(1)

    def switchToSettings(self):
        self.stacked_widget.setCurrentIndex(2)

def main():
    app = QApplication(sys.argv)

    # 隐藏鼠标光标，针对触摸屏优化
    app.setOverrideCursor(Qt.CursorShape.BlankCursor)

    window = MainWindow()
    
    # 全屏无边框显示
    window.showFullScreen()

    sys.exit(app.exec())

if __name__ == "__main__":
    main()
