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

# Form implementation generated from reading ui file 'login.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets
import pymysql
import os

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1280, 720)
        MainWindow.setMinimumSize(QtCore.QSize(1280, 720))
        font = QtGui.QFont()
        font.setFamily("OpenSymbol")
        MainWindow.setFont(font)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setMinimumSize(QtCore.QSize(1280, 720))
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout_4.setObjectName("verticalLayout_4")
        spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.verticalLayout_4.addItem(spacerItem)
        spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.verticalLayout_4.addItem(spacerItem1)
        self.layoutTexto = QtWidgets.QVBoxLayout()
        self.layoutTexto.setObjectName("layoutTexto")
        self.labelLogin = QtWidgets.QLabel(self.centralwidget)
        font = QtGui.QFont()
        font.setFamily("Nimbus Roman No9 L")
        font.setPointSize(22)
        font.setBold(True)
        font.setWeight(75)
        self.labelLogin.setFont(font)
        self.labelLogin.setObjectName("labelLogin")
        self.layoutTexto.addWidget(self.labelLogin, 0, QtCore.Qt.AlignHCenter|QtCore.Qt.AlignVCenter)
        self.verticalLayout_4.addLayout(self.layoutTexto)
        spacerItem2 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.verticalLayout_4.addItem(spacerItem2)
        self.layoutInput = QtWidgets.QGridLayout()
        self.layoutInput.setObjectName("layoutInput")
        self.lineEditUsername = QtWidgets.QLineEdit(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.lineEditUsername.sizePolicy().hasHeightForWidth())
        self.lineEditUsername.setSizePolicy(sizePolicy)
        self.lineEditUsername.setMinimumSize(QtCore.QSize(350, 0))
        self.lineEditUsername.setMaximumSize(QtCore.QSize(350, 16777215))
        self.lineEditUsername.setObjectName("lineEditUsername")
        self.layoutInput.addWidget(self.lineEditUsername, 0, 2, 1, 1, QtCore.Qt.AlignLeft)
        self.lineEditPassword = QtWidgets.QLineEdit(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.lineEditPassword.sizePolicy().hasHeightForWidth())
        self.lineEditPassword.setSizePolicy(sizePolicy)
        self.lineEditPassword.setMinimumSize(QtCore.QSize(350, 0))
        self.lineEditPassword.setMaximumSize(QtCore.QSize(350, 16777215))
        self.lineEditPassword.setEchoMode(QtWidgets.QLineEdit.Password)
        self.lineEditPassword.setObjectName("lineEditPassword")
        self.layoutInput.addWidget(self.lineEditPassword, 1, 2, 1, 1, QtCore.Qt.AlignLeft)
        self.labelUsername = QtWidgets.QLabel(self.centralwidget)
        self.labelUsername.setMinimumSize(QtCore.QSize(0, 0))
        self.labelUsername.setMaximumSize(QtCore.QSize(100, 16777215))
        font = QtGui.QFont()
        font.setFamily("Nimbus Roman No9 L")
        font.setPointSize(12)
        self.labelUsername.setFont(font)
        self.labelUsername.setObjectName("labelUsername")
        self.layoutInput.addWidget(self.labelUsername, 0, 1, 1, 1, QtCore.Qt.AlignHCenter)
        spacerItem3 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.layoutInput.addItem(spacerItem3, 1, 0, 1, 1)
        spacerItem4 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.layoutInput.addItem(spacerItem4, 0, 3, 1, 1)
        spacerItem5 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.layoutInput.addItem(spacerItem5, 0, 0, 1, 1)
        self.labelPassword = QtWidgets.QLabel(self.centralwidget)
        self.labelPassword.setMinimumSize(QtCore.QSize(0, 0))
        self.labelPassword.setMaximumSize(QtCore.QSize(100, 16777215))
        font = QtGui.QFont()
        font.setFamily("Nimbus Roman No9 L")
        font.setPointSize(12)
        self.labelPassword.setFont(font)
        self.labelPassword.setObjectName("labelPassword")
        self.layoutInput.addWidget(self.labelPassword, 1, 1, 1, 1, QtCore.Qt.AlignHCenter)
        spacerItem6 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.layoutInput.addItem(spacerItem6, 1, 3, 1, 1)
        spacerItem7 = QtWidgets.QSpacerItem(60, 20, QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Minimum)
        self.layoutInput.addItem(spacerItem7, 0, 4, 1, 1)
        spacerItem8 = QtWidgets.QSpacerItem(60, 20, QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Minimum)
        self.layoutInput.addItem(spacerItem8, 1, 4, 1, 1)
        self.verticalLayout_4.addLayout(self.layoutInput)
        spacerItem9 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.verticalLayout_4.addItem(spacerItem9)
        self.signInButton = QtWidgets.QPushButton(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.signInButton.sizePolicy().hasHeightForWidth())
        self.signInButton.setSizePolicy(sizePolicy)
        self.signInButton.setMinimumSize(QtCore.QSize(150, 0))
        self.signInButton.setMaximumSize(QtCore.QSize(150, 16777215))
        font = QtGui.QFont()
        font.setFamily("Nimbus Roman No9 L")
        font.setPointSize(11)
        font.setBold(True)
        font.setWeight(75)
        self.signInButton.setFont(font)
        self.signInButton.setObjectName("signInButton")
        self.signInButton.setCheckable(False)
        self.signInButton.setChecked(False)
        self.signInButton.clicked.connect(self.verifica_login) 
        self.verticalLayout_4.addWidget(self.signInButton, 0, QtCore.Qt.AlignHCenter)
        spacerItem10 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.verticalLayout_4.addItem(spacerItem10)
        spacerItem11 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.verticalLayout_4.addItem(spacerItem11)
        MainWindow.setCentralWidget(self.centralwidget)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.labelLogin.setText(_translate("MainWindow", "Sign In"))
        self.labelUsername.setText(_translate("MainWindow", "Username:"))
        self.labelPassword.setText(_translate("MainWindow", "Password:"))
        self.signInButton.setText(_translate("MainWindow", "Sign In"))

    def verifica_login(self):  
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        # Conecta ao banco
        db = pymysql.connect(host="18.229.103.40", user="root", passwd="F5fm5q2i@", db="facedb")
        # Print para debug da conexao
        # print(db)
        cursor = db.cursor()
        query = "SELECT nome FROM usuarios WHERE login=%s and senha=md5(%s)"
        cursor.execute(query, (self.lineEditUsername.text(), self.lineEditPassword.text()))
        #result = cursor.fetchone()
        result = str(cursor.fetchone()[0]) 
        # Print para debug 
        print(result)
        if result:
            MainWindow.hide()
            import os 
            os.system("python3 main.py " + result)
            sys.exit(app.exec_())


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

