from PyQt5 import QtWidgets, uic
from PyQt5.QtWidgets import QFileDialog, QMessageBox
from lrm.exceptions import lrm_slot


class LoadScreen(QtWidgets.QDialog):
    def __init__(self, parent, display_picture, load_existing_rectangles) -> None:
        super().__init__()

        self.file_names = []

        self.ui4 = uic.loadUi("UI_FILES/LoadScreen.ui", self)
        self.ui4.setWindowTitle("Load Screen")
        self.setModal(True)
        self.show()

        # Parent is actually the main window, but there is a chain through buttons handler, combobox handler and then to main window
        self.parent = parent

        # instance of a buttons handler
        self.display_picture = display_picture
        self.load_existing_rectangles = load_existing_rectangles
        
        # Initialize everything
        self.load_user_input()

    @lrm_slot
    def load_user_input(self):
        # Connect everything that was selected or written by the user to the OK button, so that it is saved all the time
        self.screenshot_button.clicked.connect(self.open_screenhot_file_dialog)
        self.screen_definition_button.clicked.connect(self.open_screen_definition_file_dialog)
        self.ui4.buttonBox.accepted.connect(self.save_user_input)

# TODO 
    @lrm_slot
    def save_user_input(self):
        if len(self.file_names) == 2:
            
            # Call the screenshot handler (but through the buttons because that's where the screenshot instance is created)
            tab_name = self.json_file_name.split('/')[-1]
            tab_name = tab_name.split('.')[-2]
            self.display_picture(self.screenshot_file_name, tab_name)
            self.load_existing_rectangles(self.json_file_name)

            # This line of code will close the dialog window then a user presses ok.
            self.accept()
        else:
            QMessageBox.warning(self, "Error", "Please fill in all the required fields.")

            # This line of code will re-open the dialog window because something was missing!
            self.show()

    @lrm_slot
    def open_screenhot_file_dialog(self):
        options = QFileDialog.Options()
        options |= QFileDialog.ReadOnly
        self.screenshot_file_name, _ = QFileDialog.getOpenFileName(self.parent,"Select Screenshot", "","Images (*.png *.xpm *.jpg *.bmp);;All Files (*)", options=options)
        if self.screenshot_file_name:
            self.screenshot_button.setText(self.screenshot_file_name.split('/')[-1])
            self.file_names.append(self.screenshot_file_name)

    @lrm_slot
    def open_screen_definition_file_dialog(self):
        options = QFileDialog.Options()
        options |= QFileDialog.ReadOnly
        self.json_file_name, _ = QFileDialog.getOpenFileName(self.parent, "Select screen definition", "", "JSON Files (*.json);;All Files (*)", options=options)
        if self.json_file_name:
            self.screen_definition_button.setText(self.json_file_name.split('/')[-1])
            self.file_names.append(self.json_file_name)
