from PyQt5 import uic
from lrm.exceptions import lrm_slot, lrm_slot_param
from PyQt5.QtCore import Qt, QRect, QSize
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QDialog, QRubberBand, QFileDialog


class NewIcon(QDialog):
    def __init__(self, screenshot_file_name, receive_png_name):
        super().__init__()
        self.new_icon_dialog_ui = uic.loadUi('UI_FILES/New_Icon_Dialog.ui', self)
        self.new_icon_dialog_ui.setWindowTitle("New Icon")
        self.screenshot_file_name = screenshot_file_name
        self.receive_png_name = receive_png_name
        self.selection_pixmap = None

        self.display_picture()
        
        self.setModal(True)
        self.show()

        # self.ui6.finished.connect(self.dialog_finished)
        self.new_icon_dialog_ui.buttonBox.accepted.connect(self.dialog_accepted)
        self.new_icon_dialog_ui.buttonBox.rejected.connect(self.dialog_rejected)
    
    @lrm_slot
    def display_picture(self):
        # Access the QLabel widget using its object name
        
        # Load the image file and set it as the pixmap of the QLabel widget
        pixmap = QPixmap(self.screenshot_file_name)
        self.screenshot_label.setPixmap(pixmap)

        # Set the size of the QLabel to match the size of the pixmap
        self.screenshot_label.setFixedSize(pixmap.size())

        # Set up variables for tracking the rubber band selection
        self.selection = None
        self.selection_start = None
        self.selection_end = None

    @lrm_slot_param
    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            if self.selection is not None:
                self.selection.hide()
            # Start a new rubber band selection
            self.selection_start = event.pos()
            print(self.selection_start)
            self.selection_end = event.pos()
            self.selection = QRubberBand(QRubberBand.Rectangle, self)
            self.selection.setGeometry(QRect(self.selection_start, QSize()))
            self.selection.show()

    @lrm_slot_param
    def mouseMoveEvent(self, event):
        if self.selection is not None:
            # Update the rubber band selection
            self.selection_end = event.pos()
            self.selection.setGeometry(QRect(self.selection_start, self.selection_end).normalized())

    @lrm_slot_param
    def mouseReleaseEvent(self, event):
        if event.button() == Qt.LeftButton and self.selection is not None:
            # End the rubber band selection and capture the selected region
            self.selection_rect = QRect(self.selection_start, self.selection_end).normalized()
            print(self.selection_start)
            print(self.selection_end)
            print(self.selection_rect)
            self.selection_pixmap = self.screenshot_label.pixmap().copy(self.selection_rect)
            self.where_to_save_image()

    @lrm_slot
    def where_to_save_image(self):
        """Open file dialog and save the data when specifiying a path."""
        options = QFileDialog.Options()
        options |= QFileDialog.DontUseNativeDialog
        self.screenshot_path, _ = QFileDialog.getSaveFileName(self, "Save .png file", "", "Png Files (*.png)", options=options)
        if self.screenshot_path:
            self.selection = None
        else:
            self.selection.hide()
            self.selection_pixmap = None

    @lrm_slot
    def dialog_accepted(self):
        # Save the selected region to a file
        if self.selection_pixmap is not None:
            self.selection_pixmap.save(f"{self.screenshot_path}.png")
            self.receive_png_name(f"{self.screenshot_path.split('/')[-1]}.png")
    
    @lrm_slot
    def dialog_rejected(self):
        pass
