import pymysql
from oxyapp.utils import tupleToList

db_location = pymysql.connect(host='azrod-server.ovh', port=3306, user='access', passwd='PROJET2607',
                              db='projet', autocommit=True)


# Récupère tous les utilisateurs
def list_users():
    try:
        with db_location.cursor() as _conn:
            _conn.execute("SELECT id, Nom, Prenom, Tel, Privilege FROM `users` ORDER BY `ID` ASC;")
            result = _conn.fetchall()
            return result
    finally:
        _conn.close()


# Récupère tous les patients
def list_patient(idManage):
    try:
        with db_location.cursor() as _conn:
            _conn.execute("SELECT users.ID, users.Nom, users.Prenom, users.Tel, access.UserAccess FROM users "
                          "LEFT JOIN access ON access.UserAccess = '" + str(idManage) + "' AND "
                          "users.ID = access.AccessedUser WHERE users.Privilege = 'patient' ORDER BY users.ID ASC;")
            result = _conn.fetchall()
            list_final = tupleToList(result, idManage)
            return list_final
    finally:
        _conn.close()


# Vérifie les données de connexion - AUCUN CRYPTAGE CAR PROJET mais essentiel à rajouter par la suite
def login_verify(nom, prenom, pw):
    try:
        with db_location.cursor() as _conn:
            _conn.execute(
                "SELECT id, nom, prenom, password, privilege FROM users WHERE nom = '" + nom + "' AND password = '" +
                pw + "' AND prenom = '" + prenom + "';")
            row = []
            for row in _conn:
                print(row)
            return row
    finally:
        _conn.close()


# Permet de récupérer l'utilisateur pour envoyer message - a modifier pour envoyer à un numéro spécifique
def messageSendToID(id):
    try:
        with db_location.cursor() as _conn:
            result = _conn.execute("SELECT id, nom, prenom FROM users WHERE id = '" + str(id) + "';")
            return result
    finally:
        _conn.close()


# Permet de récupérer les données de localisations d'un utilisateur
def getLocalisation(id):
    try:
        with db_location.cursor() as _conn:
            _conn.execute(
                "SELECT data.latitude, data.longitude FROM data, users WHERE users.ID = '" + str(id) + "' AND data.User = '" + str(id) + "' "
                "ORDER BY data.ID DESC LIMIT 1;")
            row = []
            for row in _conn:
                print(row)
            return row
    finally:
        _conn.close()


# Récupère les données vitales d'un utilisateurs
def getDataUser(id):
    try:
        with db_location.cursor() as _conn:
            _conn.execute(
                "SELECT users.prenom, users.nom, data.bpm, data.oxy, data.chute FROM data, users WHERE users.ID = '" +
                str(id) + "' AND data.User = '" + str(id) + "' ORDER BY data.ID DESC LIMIT 1;")
            row = []
            for row in _conn:
                print(row)
            return row
    finally:
        _conn.close()


# Ajoutes un utilisateurs
def add_user(name, prenom, number, pw, rank):
    try:
        with db_location.cursor() as _conn:
            var = ""
            if str(rank) == "Patient":
                rank = "patient"
                var = "INSERT INTO users (ID, Nom, Prenom, Password, Privilege, Tel) VALUES (NULL , '" + name \
                      + "', '" + prenom + "', '" + pw + "', '" + rank + "', '" + number + "');"
            if str(rank) == "Proche":
                rank = "proche"
                var = "INSERT INTO users (ID, Nom, Prenom, Password, Privilege, Tel) VALUES (NULL , '" + name \
                      + "', '" + prenom + "', '" + pw + "', '" + rank + "', '" + number + "');"
            if str(rank) == "Personnel Médical":
                rank = "med"
                var = "INSERT INTO users (ID, Nom, Prenom, Password, Privilege, Tel) VALUES (NULL , '" + name \
                      + "', '" + prenom + "', '" + pw + "', '" + rank + "', '" + number + "');"
            if str(rank) == "Administrateur":
                rank = "admin"
                var = "INSERT INTO users (ID, Nom, Prenom, Password, Privilege, Tel) VALUES (NULL , '" + name \
                      + "', '" + prenom + "', '" + pw + "', '" + rank + "', '" + number + "');"
            result = _conn.execute(var)
            return result
    finally:
        _conn.close()


# Permet de confirmer l'existence d'un utilisateur
def confirm_user(name, prenom, id):
    try:
        with db_location.cursor() as _conn:
            # Requête SQL
            result = _conn.execute("SELECT id, nom, prenom FROM users WHERE id = '" + id + "' AND nom = '" +
                                   name + "' AND prenom = '" + prenom + "';")
            return result
    finally:
        _conn.close()


# Permet de supprimer un utilisateur
def delete_user_from_db(id):  # Ajouter la suppresion liens entre proches et les data
    try:
        with db_location.cursor() as _conn:
            _conn.execute("DELETE FROM users WHERE id = '" + id + "';")
    finally:
        _conn.close()


if __name__ == "__main__":
    print(list_users())
