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

# Form implementation generated from reading ui file 'information.ui'
#
# Created by: PyQt5 UI code generator 5.15.1
#
# 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 zeep import Client
from table2 import Table_UI
from dialog2 import Dialog_UI
from validate_email import validate_email

client = Client(wsdl="http://127.0.0.1:8000/index.php/soap?wsdl")


class Info_UI(object):
    emailUser = None

    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(491, 385)
        self.pushButton_4 = QtWidgets.QPushButton(Form)
        self.pushButton_4.setGeometry(QtCore.QRect(90, 150, 151, 31))
        font = QtGui.QFont()
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_4.setFont(font)
        self.pushButton_4.setObjectName("pushButton_4")
        self.pushButton_5 = QtWidgets.QPushButton(Form)
        self.pushButton_5.setGeometry(QtCore.QRect(300, 150, 79, 31))
        font = QtGui.QFont()
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_5.setFont(font)
        self.pushButton_5.setObjectName("pushButton_5")
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(20, 210, 79, 31))
        font = QtGui.QFont()
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton.setFont(font)
        self.pushButton.setObjectName("pushButton")
        self.pushButton_3 = QtWidgets.QPushButton(Form)
        self.pushButton_3.setGeometry(QtCore.QRect(170, 210, 107, 31))
        font = QtGui.QFont()
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_3.setFont(font)
        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton_2 = QtWidgets.QPushButton(Form)
        self.pushButton_2.setGeometry(QtCore.QRect(350, 210, 87, 31))
        font = QtGui.QFont()
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_2.setFont(font)
        self.pushButton_2.setObjectName("pushButton_2")
        self.widget = QtWidgets.QWidget(Form)
        self.widget.setGeometry(QtCore.QRect(31, 13, 381, 112))
        self.widget.setObjectName("widget")
        self.gridLayout = QtWidgets.QGridLayout(self.widget)
        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.gridLayout.setObjectName("gridLayout")
        self.label = QtWidgets.QLabel(self.widget)
        font = QtGui.QFont()
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
        self.lineEdit = QtWidgets.QLineEdit(self.widget)
        self.lineEdit.setObjectName("lineEdit")
        self.gridLayout.addWidget(self.lineEdit, 0, 1, 1, 1)
        self.label_2 = QtWidgets.QLabel(self.widget)
        font = QtGui.QFont()
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.gridLayout.addWidget(self.label_2, 1, 0, 1, 1)
        self.lineEdit_2 = QtWidgets.QLineEdit(self.widget)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.gridLayout.addWidget(self.lineEdit_2, 1, 1, 1, 1)
        self.label_3 = QtWidgets.QLabel(self.widget)
        font = QtGui.QFont()
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.label_3.setFont(font)
        self.label_3.setObjectName("label_3")
        self.gridLayout.addWidget(self.label_3, 2, 0, 1, 1)
        self.lineEdit_3 = QtWidgets.QLineEdit(self.widget)
        self.lineEdit_3.setObjectName("lineEdit_3")
        self.gridLayout.addWidget(self.lineEdit_3, 2, 1, 1, 1)
        self.label_4 = QtWidgets.QLabel(self.widget)
        font = QtGui.QFont()
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.label_4.setFont(font)
        self.label_4.setObjectName("label_4")
        self.gridLayout.addWidget(self.label_4, 3, 0, 1, 1)
        self.lineEdit_4 = QtWidgets.QLineEdit(self.widget)
        self.lineEdit_4.setObjectName("lineEdit_4")
        self.gridLayout.addWidget(self.lineEdit_4, 3, 1, 1, 1)

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

        self.pushButton.clicked.connect(self.addMethod)
        self.pushButton_2.clicked.connect(self.updateMethod)
        self.pushButton_3.clicked.connect(self.deleteMethod)
        self.pushButton_4.clicked.connect(self.searchMethod)
        self.pushButton_5.clicked.connect(self.listMethod)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton_4.setText(_translate("Form", "Rechercher"))
        self.pushButton_5.setText(_translate("Form", "Lister"))
        self.pushButton.setText(_translate("Form", "Ajouter"))
        self.pushButton_3.setText(_translate("Form", "Supprimer"))
        self.pushButton_2.setText(_translate("Form", "Modifier"))
        self.label.setText(_translate("Form", "Prenom"))
        self.label_2.setText(_translate("Form", "Nom"))
        self.label_3.setText(_translate("Form", "Email"))
        self.label_4.setText(_translate("Form", "Role"))

    def searchMethod(self):
        utilisateur = None
        result = False
        email = self.lineEdit_3.text()
        if validate_email(email):
            if len(Table_UI.listOfUser) == 0:
                Dialog_UI.info = "No accees for this service "
                self.Form = QtWidgets.QWidget()
                self.ui = Dialog_UI()
                self.ui.setupUi(self.Form)
                self.Form.show()
            else:
                for user in Table_UI.listOfUser:
                    if user[3] == email:
                        utilisateur = user
                        result = True

                if result:
                    self.lineEdit.setText(utilisateur[1])
                    self.lineEdit_2.setText(utilisateur[2])
                    self.lineEdit_3.setText(utilisateur[3])
                    self.lineEdit_4.setText(",".join(utilisateur[4:]))
                else:
                    Dialog_UI.info = "Existe pas!! "
                    self.Form = QtWidgets.QWidget()
                    self.ui = Dialog_UI()
                    self.ui.setupUi(self.Form)
                    self.Form.show()
        else:
            Dialog_UI.info = "Email invalide!! "
            self.Form = QtWidgets.QWidget()
            self.ui = Dialog_UI()
            self.ui.setupUi(self.Form)
            self.Form.show()

    def updateMethod(self):
        role = self.lineEdit_4.text()
        email = self.lineEdit_3.text()
        if role == "editeur":
            response = client.service.edit(email, 1, Info_UI.emailUser)
        elif role == "admin":
            response = client.service.edit(email, 2, Info_UI.emailUser)
        else:
            response = client.service.edit(email, 0, Info_UI.emailUser)

        if response == "OK":
            Dialog_UI.info = "Modification Reussi "
            self.Form = QtWidgets.QWidget()
            self.ui = Dialog_UI()
            self.ui.setupUi(self.Form)
            self.Form.show()
        else:
            Dialog_UI.info = "Tu n'as pas ce droit!!"
            self.Form = QtWidgets.QWidget()
            self.ui = Dialog_UI()
            self.ui.setupUi(self.Form)
            self.Form.show()

    def addMethod(self):
        prenom = self.lineEdit.text()
        nom = self.lineEdit_2.text()
        email = self.lineEdit_3.text()

        response = client.service.create(prenom, nom, email, Info_UI.emailUser)
        if response == "OK":
            Dialog_UI.info = "Ajout Reussi "
            self.Form = QtWidgets.QWidget()
            self.ui = Dialog_UI()
            self.ui.setupUi(self.Form)
            self.Form.show()
        else:
            Dialog_UI.info = "Tu n'as pas ce droit!!"
            self.Form = QtWidgets.QWidget()
            self.ui = Dialog_UI()
            self.ui.setupUi(self.Form)
            self.Form.show()

    def deleteMethod(self):
        email = self.lineEdit_3.text()
        response = client.service.delete(email, Info_UI.emailUser)
        print(response)
        if response == "OK":
            Dialog_UI.info = "Ajout Reussi "
            self.Form = QtWidgets.QWidget()
            self.ui = Dialog_UI()
            self.ui.setupUi(self.Form)
            self.Form.show()
        else:
            Dialog_UI.info = "Tu n'as pas ce droit!!"
            self.Form = QtWidgets.QWidget()
            self.ui = Dialog_UI()
            self.ui.setupUi(self.Form)
            self.Form.show()

    def listMethod(self):
        if len(Table_UI.listOfUser) == 0:
            Dialog_UI.info = "No accees for this service "
            self.Form = QtWidgets.QWidget()
            self.ui = Dialog_UI()
            self.ui.setupUi(self.Form)
            self.Form.show()
        else:
            self.Form = QtWidgets.QWidget()
            self.ui = Table_UI()
            self.ui.setupUi(self.Form)
            self.Form.show()
            print("List Method")
