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

# Form implementation generated from reading ui file 'D:\FBLA\E-Book2\SignUp.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtSql import *
import hashlib

class Ui_SignUp(QWidget):
    student_sign_up_signal = pyqtSignal(str)
    
    def __init__(self):
        super(Ui_SignUp,  self).__init__()
        self.resize(486,  300)
        self.setWindowTitle("Notery")
        self.setupUi()
    
    def setupUi(self):
        self.label = QtWidgets.QLabel(self)
        self.label.setGeometry(QtCore.QRect(10, 100, 128, 128))
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.label.sizePolicy().hasHeightForWidth())
        self.label.setSizePolicy(sizePolicy)
        self.label.setStyleSheet("background-image:url(:/rsc/rsc/studying.png)")
        self.label.setText("")
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self)
        self.label_2.setGeometry(QtCore.QRect(10, -3, 91, 41))
        font = QtGui.QFont()
        font.setFamily("Bauhaus 93")
        font.setPointSize(22)
        self.label_2.setFont(font)
        self.label_2.setStyleSheet("color: rgb(14, 120, 134)\n"
"")
        self.label_2.setAlignment(QtCore.Qt.AlignCenter)
        self.label_2.setObjectName("label_2")
        self.line = QtWidgets.QFrame(self)
        self.line.setGeometry(QtCore.QRect(-3, 30, 491, 20))
        self.line.setFrameShape(QtWidgets.QFrame.HLine)
        self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line.setObjectName("line")
        #self.label_9 = QtWidgets.QLabel(self)
        #self.label_9.setGeometry(QtCore.QRect(260, 7, 181, 20))
        font = QtGui.QFont()
        font.setFamily("Candara")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        #self.label_9.setFont(font)
        #self.label_9.setStyleSheet("color:rgb(14, 120, 134)")
        #self.label_9.setObjectName("label_9")
        #self.SignInButton = QtWidgets.QPushButton(self)
        #self.SignInButton.setGeometry(QtCore.QRect(430, 2, 51, 31))
        font = QtGui.QFont()
        font.setFamily("Candara")
        font.setPointSize(11)
        #self.SignInButton.setFont(font)
        #self.SignInButton.setStyleSheet("color:rgb(32, 208, 194)")
        #self.SignInButton.setFlat(True)
        #self.SignInButton.setObjectName("SignInButton")
        self.layoutWidget = QtWidgets.QWidget(self)
        self.layoutWidget.setGeometry(QtCore.QRect(160, 58, 293, 221))
        self.layoutWidget.setObjectName("layoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.layoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.label_8 = QtWidgets.QLabel(self.layoutWidget)
        font = QtGui.QFont()
        font.setFamily("Candara")
        font.setPointSize(28)
        font.setBold(True)
        font.setWeight(75)
        self.label_8.setFont(font)
        self.label_8.setStyleSheet("color: rgb(32, 208, 194)\n"
"")
        self.label_8.setAlignment(QtCore.Qt.AlignCenter)
        self.label_8.setObjectName("label_8")
        self.verticalLayout.addWidget(self.label_8)
        self.formLayout = QtWidgets.QFormLayout()
        self.formLayout.setObjectName("formLayout")
        self.label_3 = QtWidgets.QLabel(self.layoutWidget)
        font = QtGui.QFont()
        font.setFamily("Candara")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_3.setFont(font)
        self.label_3.setStyleSheet("color:rgb(255, 117, 124);")
        self.label_3.setObjectName("label_3")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label_3)
        self.StudentNameLineEdit = QtWidgets.QLineEdit(self.layoutWidget)
        self.StudentNameLineEdit.setObjectName("StudentNameLineEdit")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.StudentNameLineEdit)
        self.label_4 = QtWidgets.QLabel(self.layoutWidget)
        font = QtGui.QFont()
        font.setFamily("Candara")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_4.setFont(font)
        self.label_4.setStyleSheet("color:rgb(255, 117, 124);")
        self.label_4.setObjectName("label_4")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.label_4)
        self.StudentIdLineEdit = QtWidgets.QLineEdit(self.layoutWidget)
        self.StudentIdLineEdit.setObjectName("StudentIdLineEdit")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.StudentIdLineEdit)
        self.label_5 = QtWidgets.QLabel(self.layoutWidget)
        font = QtGui.QFont()
        font.setFamily("Candara")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_5.setFont(font)
        self.label_5.setStyleSheet("color:rgb(255, 117, 124);")
        self.label_5.setObjectName("label_5")
        self.formLayout.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.label_5)
        self.StudentGradeLineEdit = QtWidgets.QLineEdit(self.layoutWidget)
        self.StudentGradeLineEdit.setObjectName("StudentGradeLineEdit")
        self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.StudentGradeLineEdit)
        self.label_6 = QtWidgets.QLabel(self.layoutWidget)
        font = QtGui.QFont()
        font.setFamily("Candara")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_6.setFont(font)
        self.label_6.setStyleSheet("color:rgb(255, 117, 124);")
        self.label_6.setObjectName("label_6")
        self.formLayout.setWidget(3, QtWidgets.QFormLayout.LabelRole, self.label_6)
        self.PasswordLineEdit = QtWidgets.QLineEdit(self.layoutWidget)
        self.PasswordLineEdit.setObjectName("PasswordLineEdit")
        self.formLayout.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.PasswordLineEdit)
        self.label_7 = QtWidgets.QLabel(self.layoutWidget)
        font = QtGui.QFont()
        font.setFamily("Candara")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_7.setFont(font)
        self.label_7.setStyleSheet("color:rgb(255, 117, 124);")
        self.label_7.setObjectName("label_7")
        self.formLayout.setWidget(4, QtWidgets.QFormLayout.LabelRole, self.label_7)
        self.ConfirmPassordLineEdit = QtWidgets.QLineEdit(self.layoutWidget)
        self.ConfirmPassordLineEdit.setObjectName("ConfirmPassordLineEdit")
        self.formLayout.setWidget(4, QtWidgets.QFormLayout.FieldRole, self.ConfirmPassordLineEdit)
        self.verticalLayout.addLayout(self.formLayout)
        self.CreateAccountButton = QtWidgets.QPushButton(self.layoutWidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.CreateAccountButton.sizePolicy().hasHeightForWidth())
        self.CreateAccountButton.setSizePolicy(sizePolicy)
        font = QtGui.QFont()
        font.setFamily("Candara")
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.CreateAccountButton.setFont(font)
        self.CreateAccountButton.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.CreateAccountButton.setStyleSheet("background-color:rgb(32, 208, 194);\n"
"color:white;")
        self.CreateAccountButton.setAutoDefault(True)
        self.CreateAccountButton.setDefault(False)
        self.CreateAccountButton.setFlat(False)
        self.CreateAccountButton.setObjectName("CreateAccountButton")
        self.verticalLayout.addWidget(self.CreateAccountButton)
        
        self.CreateAccountButton.clicked.connect(self.SignUp)
#        self.StudentIdLineEdit.returnPressed.connect(self.SignUp)
#        self.StudentNameLineEdit.returnPressed.connect(self.SignUp)
#        self.PasswordLineEdit.returnPressed.connect(self.SignUp)
#        self.ConfirmPassordLineEdit.returnPressed.connect(self.SignUp)
        
        self.retranslateUi()
        QtCore.QMetaObject.connectSlotsByName(self)

    def retranslateUi(self):
        _translate = QtCore.QCoreApplication.translate
        self.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label_2.setText(_translate("Dialog", "Notery"))
        #self.label_9.setText(_translate("Dialog", "Already have an account?"))
        #self.SignInButton.setText(_translate("Dialog", "Sign in"))
        self.label_8.setText(_translate("Dialog", "Sign up for Notery"))
        self.label_3.setText(_translate("Dialog", "Student Name"))
        self.label_4.setText(_translate("Dialog", "Student Id"))
        self.label_5.setText(_translate("Dialog", "Grade"))
        self.label_6.setText(_translate("Dialog", "Password"))
        self.label_7.setText(_translate("Dialog", "Confirm Password"))
        self.CreateAccountButton.setText(_translate("Dialog", "Create an account"))
    def SignUp(self):
        studentId = self.StudentIdLineEdit.text()
        studentName = self.StudentNameLineEdit.text()
        studentGrade = self.StudentGradeLineEdit.text()
        password = self.PasswordLineEdit.text()
        confirmPassword = self.ConfirmPassordLineEdit.text()
        if (studentId == "" or studentName == "" or password == "" or confirmPassword == ""):
            print(QMessageBox.warning(self,  "Warning",  "Input cannot be empty", QMessageBox.Ok))
            return
        else:
            db = QSqlDatabase.addDatabase("QSQLITE")
            db.setDatabaseName('./db/FBLAE-Book2dbebook.db')
            db.open()
            query = QSqlQuery()
            if (confirmPassword != password):
                print(QMessageBox.warning(self,  "Warning",  "Password and confirm password must match", QMessageBox.Ok))
                return
            elif (confirmPassword == password):
                #md5 encode
                hl = hashlib.md5()
                hl.update(password.encode(encoding='utf-8'))
                md5password = hl.hexdigest()
                sql = "SELECT * FROM User WHRE StudentId='%s'" %(studentId)
                query.exec_(sql)
                if (query.next()):
                    print(QMessageBox.warning(self,  "Warning",  "Account already exists", QMessageBox.Ok))
                    return
                else:
                    sql = "INSERT INTO User VALUES('%s','%s','%s','%s',0,0,0)" %(studentId,  studentName,  md5password, studentGrade)
                    db.exec_(sql)
                    db.commit()
                    print(QMessageBox.information(self, "Congrats!", "You've create your account", QMessageBox.Ok))
                    self.student_sign_up_signal.emit(studentId)
                db.close()
                return
                    
import resources_rc

if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setWindowIcon(QIcon("./rsc/ereader.png"))
    mainWindow = Ui_SignUp()
    mainWindow.show()
    sys.exit(app.exec_())

