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

# Form implementation generated from reading ui file 'forget_password.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# 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, QtGui, QtWidgets
from PyQt5.QtGui import QPixmap
from captcha_gen import *
import sqlite3, sys
from sqlite3 import Error
from PyQt5.QtWidgets import QMessageBox


class Ui_Dialog1(object):
    def call_generate(self):
        generate_image_captcha()
        pixmap = QPixmap('one.png')
        self.labelCaptcha.setPixmap(pixmap)

    def forget_pwd(self):
        pwd = self.lineEditNewPassword.text()
        pwd1 = self.lineEditRepeatPassword.text()
        user = self.lineEditUsername.text()
        cap = self.lineEditCaptcha.text()
        select_stmt = "SELECT Username FROM Users where Username like '"+user+"'"
        update_stmt = "UPDATE Users SET Password = '" + pwd + "', `Repeat Password` = '" + pwd1 + "', Captcha = '" + cap + "' WHERE Username like '" + user + "'"
        try:
            conn = sqlite3.connect("admin.db")
            cur = conn.cursor()
            if(user==""):
                msg = QMessageBox()
                msg.setWindowTitle("Alert!")
                msg.setText("Username must not be empty")
                msg.setDefaultButton(QMessageBox.Ok)
                msg.exec()
            else:
                cur.execute(select_stmt)
                row = cur.fetchone()
                if(row==None):
                    msg = QMessageBox()
                    msg.setWindowTitle("Warning!")
                    msg.setText("Account Not Found! Please Sign up")
                    msg.setDefaultButton(QMessageBox.Ok)
                    msg.exec()
                elif (pwd == "" or pwd1 == "" or pwd != pwd1):
                    msg = QMessageBox()
                    msg.setWindowTitle("Error!")
                    msg.setText("Password and Repeat Password did not match try again!")
                    msg.setDefaultButton(QMessageBox.Ok)
                    msg.exec()
                else:
                    if(pwd==pwd1):
                        with conn:
                            cur = conn.cursor()
                            if(user!="" and pwd!="" and pwd1!="" and pwd==pwd1 and cap!=""):
                                cur.execute(update_stmt)
                                msg = QMessageBox()
                                msg.setWindowTitle("Updated!")
                                msg.setText("Updated successfully!")
                                msg.setDefaultButton(QMessageBox.Ok)
                                msg.exec()
                            else:
                                msg = QMessageBox()
                                msg.setWindowTitle("Alert!")
                                msg.setText("Enter captcha to process")
                                msg.setDefaultButton(QMessageBox.Ok)
                                msg.exec()

        except Error as e:
            msg = QMessageBox()
            msg.setWindowTitle("Alert!")
            msg.setText("Error! Cannot replace the password")
            msg.setDefaultButton(QMessageBox.Ok)
            msg.exec()
        finally:
            conn.close()

    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(458, 302)
        self.layoutWidget = QtWidgets.QWidget(Dialog)
        self.layoutWidget.setGeometry(QtCore.QRect(50, 40, 351, 246))
        self.layoutWidget.setObjectName("layoutWidget")
        self.verticalLayout_5 = QtWidgets.QVBoxLayout(self.layoutWidget)
        self.verticalLayout_5.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_5.setObjectName("verticalLayout_5")
        self.verticalLayout_4 = QtWidgets.QVBoxLayout()
        self.verticalLayout_4.setObjectName("verticalLayout_4")
        self.verticalLayout_3 = QtWidgets.QVBoxLayout()
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout()
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.labelForgetPassword = QtWidgets.QLabel(self.layoutWidget)
        self.labelForgetPassword.setObjectName("labelForgetPassword")
        self.verticalLayout_2.addWidget(self.labelForgetPassword)
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.labelUsername_1 = QtWidgets.QLabel(self.layoutWidget)
        self.labelUsername_1.setObjectName("labelUsername_1")
        self.horizontalLayout.addWidget(self.labelUsername_1)
        self.lineEditUsername = QtWidgets.QLineEdit(self.layoutWidget)
        self.lineEditUsername.setObjectName("lineEditUsername")
        self.horizontalLayout.addWidget(self.lineEditUsername)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.label = QtWidgets.QLabel(self.layoutWidget)
        self.label.setObjectName("label")
        self.horizontalLayout_2.addWidget(self.label)
        self.lineEditNewPassword = QtWidgets.QLineEdit(self.layoutWidget)
        self.lineEditNewPassword.setEchoMode(QtWidgets.QLineEdit.PasswordEchoOnEdit)
        self.lineEditNewPassword.setObjectName("lineEditNewPassword")
        self.horizontalLayout_2.addWidget(self.lineEditNewPassword)
        self.verticalLayout.addLayout(self.horizontalLayout_2)
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.label_2 = QtWidgets.QLabel(self.layoutWidget)
        self.label_2.setObjectName("label_2")
        self.horizontalLayout_3.addWidget(self.label_2)
        self.lineEditRepeatPassword = QtWidgets.QLineEdit(self.layoutWidget)
        self.lineEditRepeatPassword.setEchoMode(QtWidgets.QLineEdit.PasswordEchoOnEdit)
        self.lineEditRepeatPassword.setObjectName("lineEditRepeatPassword")
        self.horizontalLayout_3.addWidget(self.lineEditRepeatPassword)
        self.verticalLayout.addLayout(self.horizontalLayout_3)
        self.verticalLayout_2.addLayout(self.verticalLayout)
        self.verticalLayout_3.addLayout(self.verticalLayout_2)
        self.labelCaptcha = QtWidgets.QLabel(self.layoutWidget)

        self.call_generate()


        self.labelCaptcha.setObjectName("labelCaptcha")
        self.verticalLayout_3.addWidget(self.labelCaptcha)
        self.toolButton = QtWidgets.QToolButton(self.layoutWidget)
        self.toolButton.setObjectName("toolButton")

        self.verticalLayout_3.addWidget(self.toolButton)
        self.verticalLayout_4.addLayout(self.verticalLayout_3)
        self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_4.setObjectName("horizontalLayout_4")
        self.labelCaptchaEnter = QtWidgets.QLabel(self.layoutWidget)
        self.labelCaptchaEnter.setObjectName("labelCaptchaEnter")
        self.horizontalLayout_4.addWidget(self.labelCaptchaEnter)
        self.lineEditCaptcha = QtWidgets.QLineEdit(self.layoutWidget)
        self.lineEditCaptcha.setObjectName("lineEditCaptcha")
        self.horizontalLayout_4.addWidget(self.lineEditCaptcha)
        self.verticalLayout_4.addLayout(self.horizontalLayout_4)
        self.verticalLayout_5.addLayout(self.verticalLayout_4)
        self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_5.setObjectName("horizontalLayout_5")
        self.pushButton_2 = QtWidgets.QPushButton(self.layoutWidget)
        self.pushButton_2.setObjectName("pushButton_2")
        self.horizontalLayout_5.addWidget(self.pushButton_2)
        self.pushButton = QtWidgets.QPushButton(self.layoutWidget)
        self.pushButton.setObjectName("pushButton")

        self.pushButton.clicked.connect(self.forget_pwd)

        self.horizontalLayout_5.addWidget(self.pushButton)
        self.verticalLayout_5.addLayout(self.horizontalLayout_5)

        self.retranslateUi(Dialog)
        self.pushButton_2.clicked.connect(Dialog.close)
        self.toolButton.clicked.connect(self.call_generate)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.labelForgetPassword.setText(_translate("Dialog", "Forget Password"))
        self.labelUsername_1.setText(_translate("Dialog", "Enter Username"))
        self.label.setText(_translate("Dialog", "New Password"))
        self.label_2.setText(_translate("Dialog", "Repeat Password"))
        self.toolButton.setText(_translate("Dialog", "re-captcha"))
        self.labelCaptchaEnter.setText(_translate("Dialog", "Enter Captcha"))
        self.pushButton_2.setText(_translate("Dialog", "Cancel"))
        self.pushButton.setText(_translate("Dialog", "Update"))


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