import time
from freesms import FreeClient
from flask import json
from datetime import datetime
from .database import messageSendToID, getDataUser
from .bdd_login import bdd_login


def messageAPI(userID):
    result = messageSendToID(userID)
    # Ici nous utilisons free donc uniquement le même numéro où l'on peut envoyer le message
    # A continuer en fonction du numéro ..
    if result:
        free = FreeClient(user="48224273", passwd="GzpMGIm0qcHnUj")
        resp = free.send_sms("Votre grand-parent a chuté")


# getDataUser est inutilisable car Multithread obligatoire - REGARDER POUR FUSIONNER LES DEUX JSON
def generate_charts(id):
    # Fonction SQL qui va être demandé
    var = "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 = []
    # Tentative de connexion à la bdd
    cur = bdd_login()
    result = cur.execute(var)
    # On ferme la connexion avec la bdd
    for row in cur:
        print(row)
    cur.close()
    if result:
        value_bpm = row[2]
        value_oxy = row[3]
        value_chute = row[4]
        # On transforme les données en json pour les récupérer sur le html
        json_data = json.dumps(
            {'time': datetime.now().strftime('%H:%M:%S'), 'value_bpm': value_bpm, 'value_oxy': value_oxy})
        yield f"data:{json_data}\n\n"
        # Message envoyé si
        if value_chute == 1:
            messageAPI(id)
        time.sleep(1)


# getDataUser est inutilisable car Multithread obligatoire
def generate_data_health(id):
    # Fonction SQL qui va être demandé
    var = "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 = []
    # Tentative de connexion à la bdd
    cur = bdd_login()
    result = cur.execute(var)
    # On ferme la connexion avec la bdd
    for row in cur:
        print(row)
    cur.close()
    if result:
        value_bpm = row[2]
        value_oxy = row[3]
        value_chute = row[4]
        if value_bpm < 40 or value_bpm > 100:
            if value_oxy < 80:
                if value_chute == 0:
                    state_bpm = 0
                    state_oxy = 0
                    state_chute = 0
                else:
                    state_bpm = 0
                    state_oxy = 0
                    state_chute = 1
            elif 95 <= value_oxy <= 100:
                if value_chute == 0:
                    state_bpm = 0
                    state_oxy = 1
                    state_chute = 0
                else:
                    state_bpm = 0
                    state_oxy = 1
                    state_chute = 1
            elif 80 <= value_oxy < 95:
                if value_chute == 0:
                    state_bpm = 0
                    state_oxy = 2
                    state_chute = 0
                else:
                    state_bpm = 0
                    state_oxy = 2
                    state_chute = 1
        elif 60 <= value_bpm <= 80:
            if value_oxy < 80:
                if value_chute == 0:
                    state_bpm = 1
                    state_oxy = 0
                    state_chute = 0
                else:
                    state_bpm = 1
                    state_oxy = 0
                    state_chute = 1
            elif 95 <= value_oxy <= 100:
                if value_chute == 0:
                    state_bpm = 1
                    state_oxy = 1
                    state_chute = 0
                else:
                    state_bpm = 1
                    state_oxy = 1
                    state_chute = 1
            elif 80 <= value_oxy < 95:
                if value_chute == 0:
                    state_bpm = 1
                    state_oxy = 2
                    state_chute = 0
                else:
                    state_bpm = 1
                    state_oxy = 2
                    state_chute = 1
        elif 40 <= value_bpm < 60 or 80 < value_bpm <= 100:
            if value_oxy < 80:
                if value_chute == 0:
                    state_bpm = 2
                    state_oxy = 0
                    state_chute = 0
                else:
                    state_bpm = 2
                    state_oxy = 0
                    state_chute = 1
            elif 95 <= value_oxy <= 100:
                if value_chute == 0:
                    state_bpm = 2
                    state_oxy = 1
                    state_chute = 0
                else:
                    state_bpm = 2
                    state_oxy = 1
                    state_chute = 1
            elif 80 <= value_oxy < 95:
                if value_chute == 0:
                    state_bpm = 2
                    state_oxy = 2
                    state_chute = 0
                else:
                    state_bpm = 2
                    state_oxy = 2
                    state_chute = 1
        # On transforme les données en json pour les récupérer sur le html
        json_data = json.dumps(
            {'time': datetime.now().strftime('%H:%M:%S'), 'bpm': value_bpm, 'oxy': value_oxy, 'chute': value_chute,
             'state_bpm': state_bpm, 'state_oxy': state_oxy, 'state_chute': state_chute})
        yield f"{json_data}\n\n"
        time.sleep(1)
