import sys
from PyQt5.QtWidgets import QApplication, QStackedWidget
from src.views.mainWindows import MainWindow
from src.views.selection_screen import SelectionScreen
from src.views.login_screen import LoginScreen
from src.views.utilities_screen import UtilitiesScreen
from src.config import APP_NAME, WINDOW_SIZE

class AppController:
    def __init__(self):
        self.app = QApplication(sys.argv)
        self.stack = QStackedWidget()
        self.stack.setGeometry(100, 100, *WINDOW_SIZE)
        self.stack.setWindowTitle(APP_NAME)
        
        # Initialize screens
        self.init_screens()
        
        # Configure navigation signals
        self.connect_signals()
        
        # Start with selection screen
        self.show_selection_screen()
        
    def init_screens(self):
        # Selection screen
        self.selection_screen = SelectionScreen()
        self.stack.addWidget(self.selection_screen)
        
        # Login screen
        self.login_screen = LoginScreen()
        self.stack.addWidget(self.login_screen)
        
        # Utilities screen
        self.utilities_screen = UtilitiesScreen()
        self.stack.addWidget(self.utilities_screen)
        
        # Demo app (main window)
        self.main_window = MainWindow()
        self.stack.addWidget(self.main_window)
    
    def connect_signals(self):
        # Selection screen signals
        self.selection_screen.open_utilities.connect(self.show_login_screen)
        self.selection_screen.open_demo_app.connect(self.show_demo_app)
        
        # Login screen signals
        self.login_screen.login_success.connect(self.show_utilities_screen)
        self.login_screen.back_to_selection.connect(self.show_selection_screen)
        
        # Utilities screen signals
        self.utilities_screen.back_to_selection.connect(self.show_selection_screen)
    
    def show_selection_screen(self):
        self.stack.setCurrentWidget(self.selection_screen)
    
    def show_login_screen(self):
        self.stack.setCurrentWidget(self.login_screen)
    
    def show_utilities_screen(self):
        self.stack.setCurrentWidget(self.utilities_screen)
    
    def show_demo_app(self):
        self.stack.setCurrentWidget(self.main_window)
    
    def run(self):
        self.stack.show()
        return self.app.exec_()

def main():
    controller = AppController()
    return controller.run()

if __name__ == "__main__":
    main()