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

# Form implementation generated from reading ui file 'fs-client-app.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# 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 PyQt5 import QtCore, QtWidgets


class Ui_MainWindow():
       
    def setupUi(self, MainWindow):
        self.client = ClientHandler()
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(438, 268)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.encryptButton = QtWidgets.QPushButton(self.centralwidget)
        self.encryptButton.setEnabled(False)
        self.encryptButton.setGeometry(QtCore.QRect(280, 20, 89, 25))
        self.encryptButton.setObjectName("encryptButton")
        self.fileNameLabel = QtWidgets.QLabel(self.centralwidget)
        self.fileNameLabel.setGeometry(QtCore.QRect(20, 80, 131, 31))
        self.fileNameLabel.setObjectName("fileNameLabel")
        self.getShareButton = QtWidgets.QPushButton(self.centralwidget)
        self.getShareButton.setGeometry(QtCore.QRect(218, 60, 151, 25))
        self.getShareButton.setObjectName("getShareButton")
        self.requestOpenButton = QtWidgets.QPushButton(self.centralwidget)
        self.requestOpenButton.setEnabled(False)
        self.requestOpenButton.setGeometry(QtCore.QRect(258, 100, 111, 25))
        self.requestOpenButton.setObjectName("requestOpenButton")
        self.sendShareButton = QtWidgets.QPushButton(self.centralwidget)
        self.sendShareButton.setEnabled(False)
        self.sendShareButton.setGeometry(QtCore.QRect(218, 140, 151, 25))
        self.sendShareButton.setObjectName("sendShareButton")
        self.decryptButton = QtWidgets.QPushButton(self.centralwidget)
        self.decryptButton.setEnabled(False)
        self.decryptButton.setGeometry(QtCore.QRect(280, 180, 89, 25))
        self.decryptButton.setObjectName("decryptButton")
        self.shareFileButton = QtWidgets.QPushButton(self.centralwidget)
        self.shareFileButton.setEnabled(False)
        self.shareFileButton.setGeometry(QtCore.QRect(280, 220, 89, 25))
        self.shareFileButton.setObjectName("shareFileButton")
        self.downloadFileButton = QtWidgets.QPushButton(self.centralwidget)
        self.downloadFileButton.setEnabled(False)
        self.downloadFileButton.setGeometry(QtCore.QRect(40, 160, 89, 25))
        self.downloadFileButton.setObjectName("downloadFileButton")
        self.openFileButton = QtWidgets.QPushButton(self.centralwidget)
        self.openFileButton.setGeometry(QtCore.QRect(40, 50, 89, 25))
        self.openFileButton.setObjectName("openFileButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setSizeGripEnabled(True)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.filename = "test.txt"

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        # Add event listeners
        self.openFileButton.clicked.connect(self._browse_file)
        self.encryptButton.clicked.connect(self._encrypt_and_send_key)
        self.getShareButton.clicked.connect(self._get_share)
        self.requestOpenButton.clicked.connect(self._request_open)
        self.sendShareButton.clicked.connect(self._send_share)
        self.decryptButton.clicked.connect(self._get_key_and_decrypt_file)
        self.shareFileButton.clicked.connect(self._send_file)
        self.downloadFileButton.clicked.connect(self._download_file)
    
    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", self.client.client_id))
        self.encryptButton.setText(_translate("MainWindow", "Mã hóa file"))
        self.fileNameLabel.setText(_translate("MainWindow", "Tệp tin được chọn"))
        self.getShareButton.setText(_translate("MainWindow", "Nhận key thành phần"))
        self.requestOpenButton.setText(_translate("MainWindow", "Yêu cầu mở file"))
        self.sendShareButton.setText(_translate("MainWindow", "Gửi key thành phần"))
        self.decryptButton.setText(_translate("MainWindow", "Giải mã file"))
        self.shareFileButton.setText(_translate("MainWindow", "Chia sẻ file"))
        self.downloadFileButton.setText(_translate("MainWindow", "Tải xuống..."))
        self.openFileButton.setText(_translate("MainWindow", "Chọn file..."))
        
    def _browse_file(self):
        fname = QtWidgets.QFileDialog.getOpenFileName(self.centralwidget, 'Open file')
        self.file_path = fname[0]
        self.filename = self.file_path.split('/')[-1]
        self.fileNameLabel.setText(self.filename)
        self.encryptButton.setEnabled(True)

    def _encrypt_and_send_key(self):
        self.client.encrypt_file(self.file_path)
        self.client.send_key()
        self.encryptButton.setEnabled(False)
        
    def _get_share(self):
        self.client.get_share()
        self.getShareButton.setEnabled(False)
        self.requestOpenButton.setEnabled(True)
        self.sendShareButton.setEnabled(True)
        self.downloadFileButton.setEnabled(True)
    
    def _request_open(self):
        self.client.request_open()
        self.requestOpenButton.setEnabled(False)
        self.sendShareButton.setEnabled(True)
        
    def _send_share(self):
        self.client.send_share()
        self.sendShareButton.setEnabled(False)
        self.decryptButton.setEnabled(True)
    
    def _get_key_and_decrypt_file(self):
        self.client.get_key()
        self.client.decrypt_file()
        self.decryptButton.setEnabled(False)
        self.shareFileButton.setEnabled(True)

    def _send_file(self):
        self.client.send_file()
        self.shareFileButton.setEnabled(False)
        
    def _download_file(self):
        download_path = QtWidgets.QFileDialog.getExistingDirectory(self.centralwidget, 'Open file')
        download_path = download_path +"/"
        self.client.download_file(download_path)
        

if __name__ == "__main__":
    import sys
    from pvssfs.client import ClientHandler
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
