from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLabel, QFrame
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import QPixmap, QFont, QIcon
import os
from src.config import UXUI_ASSETS, WINDOW_SIZE, BACKGROUND_COLOR, APP_NAME

class SelectionScreen(QWidget):
    # Signals for navigation
    open_utilities = pyqtSignal()
    open_demo_app = pyqtSignal()
    
    def __init__(self, parent=None):
        super().__init__(parent)
        self.init_ui()
        
    def init_ui(self):
        # Basic window setup
        self.setGeometry(100, 100, *WINDOW_SIZE)
        self.setStyleSheet(f"background-color: #F8F9FA;")  # Slightly lighter background
        
        # Main layout
        layout = QVBoxLayout(self)
        layout.setContentsMargins(40, 40, 40, 40)
        layout.setSpacing(20)
        
        # Header with logo
        header_frame = QFrame(self)
        header_frame.setStyleSheet("background-color: #34495E; border-radius: 10px;")  # Softer dark blue
        header_frame.setFixedHeight(100)
        
        header_layout = QHBoxLayout(header_frame)
        header_layout.setContentsMargins(20, 0, 20, 0)
        
        # Logo
        logo_label = QLabel(self)
        logo_path = os.path.join(UXUI_ASSETS, "Assets_png/kneron_logo.png")
        if os.path.exists(logo_path):
            logo_pixmap = QPixmap(logo_path)
            scaled_logo = logo_pixmap.scaled(150, 60, Qt.KeepAspectRatio, Qt.SmoothTransformation)
            logo_label.setPixmap(scaled_logo)
            logo_label.setAlignment(Qt.AlignCenter)
        
        header_layout.addWidget(logo_label, alignment=Qt.AlignCenter)
        layout.addWidget(header_frame)
        
        # Content container
        content_container = QFrame(self)
        content_container.setStyleSheet("""
            QFrame {
                background-color: white;
                border-radius: 10px;
                border: 1px solid #E0E0E0;
            }
        """)
        content_layout = QVBoxLayout(content_container)
        content_layout.setContentsMargins(30, 30, 30, 30)
        content_layout.setSpacing(20)
        
        # Title
        title_label = QLabel(APP_NAME, self)
        title_label.setAlignment(Qt.AlignCenter)
        title_label.setStyleSheet("font-size: 28px; font-weight: bold; color: #34495E;")
        content_layout.addWidget(title_label)
        
        # Subtitle
        subtitle_label = QLabel("請選擇您要使用的功能", self)
        subtitle_label.setAlignment(Qt.AlignCenter)
        subtitle_label.setStyleSheet("font-size: 16px; color: #7F8C8D;")
        content_layout.addWidget(subtitle_label)
        
        # Add some space
        content_layout.addSpacing(20)
        
        # Button container
        button_container = QWidget(self)
        button_layout = QHBoxLayout(button_container)
        button_layout.setContentsMargins(20, 0, 20, 0)
        button_layout.setSpacing(40)  # Increased spacing between buttons
        
        # Create card-style buttons with icons and descriptions
        
        # Utilities card button
        utilities_card = QFrame(self)
        utilities_card.setMinimumSize(300, 250)
        utilities_card.setCursor(Qt.PointingHandCursor)
        utilities_card.setStyleSheet("""
            QFrame {
                background-color: white;
                border-radius: 15px;
                border: 1px solid #E0E0E0;
            }
            QFrame:hover {
                background-color: #F5F9FF;
                border: 1px solid #5DADE2;
            }
        """)
        
        utilities_layout = QVBoxLayout(utilities_card)
        utilities_layout.setContentsMargins(20, 20, 20, 20)
        utilities_layout.setSpacing(15)
        utilities_layout.setAlignment(Qt.AlignCenter)
        
        # Icon for utilities
        utilities_icon_label = QLabel()
        utilities_icon_path = os.path.join(UXUI_ASSETS, "Assets_svg/ic_dialog_device.svg")
        if os.path.exists(utilities_icon_path):
            utilities_icon = QPixmap(utilities_icon_path)
            scaled_icon = utilities_icon.scaled(64, 64, Qt.KeepAspectRatio, Qt.SmoothTransformation)
            utilities_icon_label.setPixmap(scaled_icon)
            utilities_icon_label.setAlignment(Qt.AlignCenter)
        utilities_layout.addWidget(utilities_icon_label, alignment=Qt.AlignCenter)
        
        # Title for utilities
        utilities_title = QLabel("設備管理工具", self)
        utilities_title.setAlignment(Qt.AlignCenter)
        utilities_title.setStyleSheet("font-size: 22px; font-weight: bold; color: #34495E;")
        utilities_layout.addWidget(utilities_title)
        
        # Description for utilities
        utilities_desc = QLabel("連接、管理和更新您的 Kneron 設備", self)
        utilities_desc.setAlignment(Qt.AlignCenter)
        utilities_desc.setWordWrap(True)
        utilities_desc.setStyleSheet("font-size: 14px; color: #7F8C8D;")
        utilities_layout.addWidget(utilities_desc)
        
        # Connect the card to the signal
        utilities_card.mousePressEvent = lambda event: self.open_utilities.emit()
        
        # Demo App card button
        demo_card = QFrame(self)
        demo_card.setMinimumSize(300, 250)
        demo_card.setCursor(Qt.PointingHandCursor)
        demo_card.setStyleSheet("""
            QFrame {
                background-color: white;
                border-radius: 15px;
                border: 1px solid #E0E0E0;
            }
            QFrame:hover {
                background-color: #F5FFF7;
                border: 1px solid #7DCEA0;
            }
        """)
        
        demo_layout = QVBoxLayout(demo_card)
        demo_layout.setContentsMargins(20, 20, 20, 20)
        demo_layout.setSpacing(15)
        demo_layout.setAlignment(Qt.AlignCenter)
        
        # Icon for demo app
        demo_icon_label = QLabel()
        demo_icon_path = os.path.join(UXUI_ASSETS, "Assets_svg/ic_recording_camera.svg")
        if os.path.exists(demo_icon_path):
            demo_icon = QPixmap(demo_icon_path)
            scaled_icon = demo_icon.scaled(64, 64, Qt.KeepAspectRatio, Qt.SmoothTransformation)
            demo_icon_label.setPixmap(scaled_icon)
            demo_icon_label.setAlignment(Qt.AlignCenter)
        demo_layout.addWidget(demo_icon_label, alignment=Qt.AlignCenter)
        
        # Title for demo app
        demo_title = QLabel("AI 演示應用", self)
        demo_title.setAlignment(Qt.AlignCenter)
        demo_title.setStyleSheet("font-size: 22px; font-weight: bold; color: #34495E;")
        demo_layout.addWidget(demo_title)
        
        # Description for demo app
        demo_desc = QLabel("使用 AI 工具箱探索和測試 Kneron 設備的功能", self)
        demo_desc.setAlignment(Qt.AlignCenter)
        demo_desc.setWordWrap(True)
        demo_desc.setStyleSheet("font-size: 14px; color: #7F8C8D;")
        demo_layout.addWidget(demo_desc)
        
        # Connect the card to the signal
        demo_card.mousePressEvent = lambda event: self.open_demo_app.emit()
        
        # Add cards to layout
        button_layout.addWidget(utilities_card)
        button_layout.addWidget(demo_card)
        
        content_layout.addWidget(button_container)
        
        # Add content container to main layout
        layout.addWidget(content_container, 1)
        
        # Footer
        footer_label = QLabel(" 2025 Innovedus Inc. All rights reserved.", self)
        footer_label.setAlignment(Qt.AlignCenter)
        footer_label.setStyleSheet("font-size: 12px; color: #95A5A6;")
        layout.addWidget(footer_label)
