from PyQt5 import QtWidgets
from view.aes_window import Ui_AESWindow
from view.ui_thread import UIThread
from view.base import Base
from utils.aes_handler import AESHandler


class AES(QtWidgets.QMainWindow, Ui_AESWindow, Base):
    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        Ui_AESWindow.__init__(self)
        self.setupUi(self)
        self.btn_encode.clicked.connect(self.encode_event)
        # self.btn_decode.clicked.connect(self.decode_event)
        self.btn_decode.setVisible(False)

    def encode_event(self):
        self.encode_thread = UIThread(self.aes_encode)
        self.encode_thread.start()

    def aes_encode(self):
        try:
            source_str = self.txt_source.text()
            if source_str:
                result = AESHandler.cbc_encrypt(source_str)
                self.txt_result.setText(result)
        except Exception as ex:
            QtWidgets.QMessageBox.critical(self, "Error", str(ex))

    def decode_event(self):
        self.decode_thread = UIThread(self.aes_decode)
        self.decode_thread.start()

    def aes_decode(self):
        try:
            source_str = self.txt_source.text()
            if source_str:
                result = AESHandler.cbc_decrypt(source_str)
                self.txt_result.setText(result)
        except Exception as ex:
            QtWidgets.QMessageBox.critical(self, "Error", str(ex))
