from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLabel,
                             QLineEdit, QComboBox, QFrame, QMessageBox)
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import QPixmap, QFont
import os
from src.config import UXUI_ASSETS, WINDOW_SIZE, BACKGROUND_COLOR

class LoginScreen(QWidget):
    # Signals for navigation
    login_success = pyqtSignal()
    back_to_selection = 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: #F5F7FA;")  # Light gray 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: #2C3E50; border-radius: 10px;")
        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)
        
        # Login form container
        form_container = QFrame(self)
        form_container.setStyleSheet("""
            QFrame {
                background-color: white;
                border-radius: 10px;
                border: 1px solid #E0E0E0;
            }
        """)
        form_layout = QVBoxLayout(form_container)
        form_layout.setContentsMargins(30, 30, 30, 30)
        form_layout.setSpacing(20)
        
        # Title
        title_label = QLabel("Login", self)
        title_label.setAlignment(Qt.AlignCenter)
        title_label.setStyleSheet("font-size: 28px; font-weight: bold; color: #2C3E50; margin-bottom: 10px;")
        form_layout.addWidget(title_label)
        
        # Server type
        server_label = QLabel("Server Authentication Type", self)
        server_label.setStyleSheet("font-size: 14px; font-weight: bold; color: #2C3E50;")
        form_layout.addWidget(server_label)
        
        self.server_combo = QComboBox(self)
        self.server_combo.addItems(["Standard Password Authentication", "Other Authentication Method"])
        self.server_combo.setMinimumHeight(40)
        self.server_combo.setStyleSheet("""
            QComboBox {
                border: 1px solid #E0E0E0;
                border-radius: 5px;
                padding: 5px 10px;
                background-color: white;
                font-size: 14px;
                color: #2C3E50;
            }
            QComboBox::drop-down {
                border: none;
                width: 30px;
            }
            QComboBox::down-arrow {
                image: url(""" + os.path.join(UXUI_ASSETS, "Assets_svg/ic_result_folder_normal.svg").replace("\\", "/") + """);
                width: 12px;
                height: 12px;
            }
            QComboBox QAbstractItemView {
                border: 1px solid #E0E0E0;
                selection-background-color: #ECF0F1;
                selection-color: #2C3E50;
                background-color: white;
                outline: none;
            }
        """)
        form_layout.addWidget(self.server_combo)
        
        # Username
        username_label = QLabel("Username", self)
        username_label.setStyleSheet("font-size: 14px; font-weight: bold; color: #2C3E50;")
        form_layout.addWidget(username_label)
        
        self.username_input = QLineEdit(self)
        self.username_input.setPlaceholderText("Enter your username")
        self.username_input.setMinimumHeight(40)
        self.username_input.setStyleSheet("""
            QLineEdit {
                border: 1px solid #E0E0E0;
                border-radius: 5px;
                padding: 5px 10px;
                background-color: white;
                font-size: 14px;
                color: #2C3E50;
            }
            QLineEdit:focus {
                border: 1px solid #3498DB;
            }
        """)
        form_layout.addWidget(self.username_input)
        
        # Password
        password_label = QLabel("Password", self)
        password_label.setStyleSheet("font-size: 14px; font-weight: bold; color: #2C3E50;")
        form_layout.addWidget(password_label)
        
        self.password_input = QLineEdit(self)
        self.password_input.setPlaceholderText("Enter your password")
        self.password_input.setEchoMode(QLineEdit.Password)
        self.password_input.setMinimumHeight(40)
        self.password_input.setStyleSheet("""
            QLineEdit {
                border: 1px solid #E0E0E0;
                border-radius: 5px;
                padding: 5px 10px;
                background-color: white;
                font-size: 14px;
                color: #2C3E50;
            }
            QLineEdit:focus {
                border: 1px solid #3498DB;
            }
        """)
        form_layout.addWidget(self.password_input)
        
        # Error message (hidden by default)
        self.error_label = QLabel("", self)
        self.error_label.setStyleSheet("color: #E74C3C; font-size: 14px;")
        self.error_label.hide()
        form_layout.addWidget(self.error_label)
        
        # Buttons
        button_layout = QHBoxLayout()
        button_layout.setSpacing(15)
        
        back_button = QPushButton("Back", self)
        back_button.setMinimumHeight(45)
        back_button.setStyleSheet("""
            QPushButton {
                background-color: #95A5A6;
                color: white;
                border-radius: 5px;
                padding: 5px 15px;
                font-size: 14px;
                font-weight: bold;
            }
            QPushButton:hover {
                background-color: #7F8C8D;
            }
            QPushButton:pressed {
                background-color: #616A6B;
            }
        """)
        back_button.clicked.connect(self.back_to_selection.emit)
        
        login_button = QPushButton("Login", self)
        login_button.setMinimumHeight(45)
        login_button.setStyleSheet("""
            QPushButton {
                background-color: #3498DB;
                color: white;
                border-radius: 5px;
                padding: 5px 15px;
                font-size: 14px;
                font-weight: bold;
            }
            QPushButton:hover {
                background-color: #2980B9;
            }
            QPushButton:pressed {
                background-color: #1F618D;
            }
        """)
        login_button.clicked.connect(self.attempt_login)
        
        button_layout.addWidget(back_button)
        button_layout.addWidget(login_button)
        
        form_layout.addLayout(button_layout)
        
        # Add form to main layout
        layout.addWidget(form_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)
        
    def attempt_login(self):
        username = self.username_input.text()
        password = self.password_input.text()
        
        # For demo purposes, use a simple validation
        if not username or not password:
            self.show_error("Please enter both username and password")
            return
            
        # Simulate login success (in a real app, you would validate with your server)
        self.login_success.emit()
        
    def show_error(self, message):
        self.error_label.setText(message)
        self.error_label.show()
