from PyQt6.QtWidgets import QWidget, QVBoxLayout, QPushButton
from PyQt6.QtGui import QFont
from PyQt6.QtCore import Qt, pyqtSignal
import sys
from . import start, install, common
from .bindings import bindings, start as bindingsStart
from .release_manifest import release_manifest

class Window(QWidget):
    startInst:bindingsStart.start
    isRunning:bool = False
    button:QPushButton

    closed = pyqtSignal()

    def on_start(self):
        if self.isRunning:
            return
        self.isRunning = True
        if self.button != None:
            self.button.setStyleSheet("background-color: gray;border-radius:15px;")
        def start_mc():
            window = start.Window()
            window.show()
            common.WinMan.add(window)
            def onMcClose():
                print("Minecraft closed")
                self.isRunning = False
                self.startInst = None
                self.button.setStyleSheet("background-color: green;border-radius:15px;")
            print("Bindings starting")
            self.startInst = bindingsStart.start(window,onMcClose)

        latest=bindings.latest_ver()
        if not bindings.installed(latest):
            installWindow = install.Window(latest)
            installWindow.show()
            
            common.WinMan.add(installWindow)
            common.WinMan.onClose(installWindow,start_mc)
        else:
            start_mc()

    def __init__(self):
        super().__init__()
        
        def get_caller_info():
            """
            Retrieves information about the caller function.

            Args:
                skip (int): The number of stack frames to skip (1 for the immediate caller, 2 for the caller of the caller, etc.).

            Returns:
                tuple: A tuple containing the filename, function name, and arguments of the caller.
                       Returns None if the stack depth is insufficient.
            """
            import inspect
            import os
            stack = inspect.stack()
            def process(caller_frame:inspect.FrameInfo):
                def inner(x):
                    caller_filename = os.path.basename(x.f_code.co_filename)
                    caller_function_name = x.f_code.co_name
                    caller_arguments = x.f_locals

                    return caller_filename, caller_function_name, caller_arguments
                return inner(caller_frame[0])
            return list(map(process,stack))

        print(get_caller_info())
        self.resize(600, 300)
        version=release_manifest["version"]
        print("Starting quickcraft " + version)
        self.setWindowTitle("Quickcraft " + version)
        self.setContentsMargins(20, 20, 20, 20)

        layout = QVBoxLayout(self)
        layout.setAlignment(Qt.AlignmentFlag.AlignCenter)
        common.initBackround(self)
        common.initLogo(self,layout)
        button = QPushButton("Start")
        button.setFont(QFont(common.families[0], 80))
        button.adjustSize()
        button.setStyleSheet("background-color: green;border-radius:15px;")
        layout.addWidget(button,alignment=Qt.AlignmentFlag.AlignCenter)
        button.clicked.connect(self.on_start)
        self.button = button
    def closeEvent(self, event):
        if self.isRunning and self.startInst:
            self.startInst.close()
        self.closed.emit()
        event.accept()

def startApp():
    window = Window()
    #window.setWindowIcon(QIcon('../logo.ico'))
    window.show()
    sys.exit(common.app.exec())
if __name__ == "__main__":
    startApp()