# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'warning_delete.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.



from .constants import *


class Ui_Dialog(object):
    def setupUi(self, Dialog, idx_delete, lobutton_label_block, button):
        self.button = button #button is different than items inside lobuton_label_block list
        self.lobutton_label_block = lobutton_label_block
        self.idx_delete = idx_delete
        self.Dialog = Dialog
        self.Dialog.setObjectName("Dialog")
        self.Dialog.resize(400, 182)
        self.buttonBox = QtWidgets.QDialogButtonBox(self.Dialog)
        self.buttonBox.setGeometry(QtCore.QRect(30, 130, 341, 32))
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName("buttonBox")
        self.label = QtWidgets.QLabel(self.Dialog)
        self.label.setGeometry(QtCore.QRect(20, 70, 341, 51))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self.Dialog)
        self.label_2.setGeometry(QtCore.QRect(120, 20, 221, 51))
        font = QtGui.QFont()
        font.setPointSize(14)
        self.label_2.setFont(font)
        self.label_2.setTextFormat(QtCore.Qt.PlainText)
        self.label_2.setScaledContents(False)
        self.label_2.setAlignment(QtCore.Qt.AlignCenter)
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(self.Dialog)
        self.label_3.setGeometry(QtCore.QRect(140, 20, 41, 41))
        self.label_3.setText("")
        self.label_3.setPixmap(QtGui.QPixmap("icons/warning_icon.png"))
        self.label_3.setScaledContents(True)
        self.label_3.setObjectName("label_3")

        self.retranslateUi(self.Dialog)
        self.buttonBox.accepted.connect(self.delete_idx) # type: ignore
        self.buttonBox.rejected.connect(self.Dialog.reject) # type: ignore
        QtCore.QMetaObject.connectSlotsByName(self.Dialog)

    def get_previous_color(self, stylesheet:str):
        stylesheet = stylesheet.replace(" ","")
        stylesheet = stylesheet.split("background-color:rgb")[1]
        stylesheet = stylesheet.split(";")[0]
        colors = stylesheet[1:-1].split(",")
        colors = [int(c) for c in colors]
        # turn it into bgr, which is cv2 default
        colors = (colors[2],colors[1],colors[0])
        return colors

    def delete_idx(self):
        # First remove the button or label widget from the layout
        widget_to_remove = self.lobutton_label_block[self.idx_delete]
        
        # Assuming `widget_to_remove` is a widget object (like a QPushButton, QLabel, etc.)
        if isinstance(widget_to_remove, QtWidgets.QWidget):
            widget_to_remove.setParent(None)  # Detach from layout
            widget_to_remove.deleteLater()    # Marks the widget for deletion
        
       
        # update brush color to default
        brush_settings["color"] = (0,255,0)

        ######## DELETING THE COLOR IN THE MASK ##################
        current_stylesheet = self.button.styleSheet()
        print("#"*60,current_stylesheet)
        previous_color = self.get_previous_color(current_stylesheet)
        
        # bgr_alpha is needed for the pixel values in the mask, as understood by cv2
        new_bgr_alpha = (0,0,0,0)
        # replace the pixels with the previous color value for the new color in RGB format
        for mask in display_settings["list_of_mask"]:
            filtered_rgb_from_mask = mask[...,:3] #filter for all the values expect alpha channel
            mask_indices = (filtered_rgb_from_mask == previous_color).all(axis=-1)  # Find matching pixels
            mask[mask_indices] = new_bgr_alpha  # Update those pixels with the new color
        

        # Refresh the layout after the widget is removed
        self.Dialog.update()
        
        # Remove the element from the list
        self.lobutton_label_block.pop(self.idx_delete)
        # Close the dialog after the deletion
        self.Dialog.accept()

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label.setText(_translate("Dialog", "This action cannot be undone. Please be sure before clicking OK"))
        self.label_2.setText(_translate("Dialog", "Careful!"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())
