"""
Ce module contient la fonction permettant de démarrer le scoring sur le sous-vivier désiré
"""

import time

from django.utils import timezone

from .models import Calcul
from .models import StatutCalculChoices as StatutCalcul
from .utils.logging import get_logger
from .utils_extraction import to_table_liste_preference, to_table_liste_preference_selectif
from .utils_insertion import (insert_liste_preference, insert_matching,
                              insert_Notation)
from .utils_matching import (matching_parfait, preprocess_matching,
                             preprocess_matchingSelectif)
from .utils_scoring import notations, notations_liste

logger = get_logger(__name__)

# Fonction de lancement du scoring
def lancer_calculs(pam_id,sv_id):
    """
    Fonction de lancement du calcul du scoring

    :param sv_id: L'id de sous vivier étudié
    :type sv_id: char
    """
    Statut = StatutCalcul
    statut = Statut.TERMINE
    pourcentage_incr = 16.67
    calcul = Calcul.objects.get(id=str(sv_id)+str(pam_id),pam_id=pam_id,sous_vivier_id=sv_id)
    start_time = time.time()
    logger.info('---------------------Scoring beginning------------------------')
    notations_list = notations(pam_id,sv_id)
    logger.info('---------------------Scoring ending---------------------------')

    calcul = Calcul.objects.get(id=str(sv_id)+str(pam_id),pam_id=pam_id,sous_vivier_id=sv_id)
    calcul.ca_statut_pourcentage += pourcentage_incr
    calcul.ca_statut_pourcentage = round(calcul.ca_statut_pourcentage, 2)
    calcul.save()
    if calcul.ca_statut == Statut.EN_ATTENTE_ARRET:
        raise Exception('Arret du calcul')

    logger.info("------------------Scoring time -- %d seconds -----------------", time.time() - start_time)
    start_time_insert = time.time()
    logger.info('---------------------insert scoring results begining-----------')
    insert_Notation(notations_list,pam_id,sv_id)
    logger.info('---------------------insert scoring results ending-----------')

    calcul = Calcul.objects.get(id=str(sv_id)+str(pam_id),pam_id=pam_id,sous_vivier_id=sv_id)
    calcul.ca_statut_pourcentage += pourcentage_incr
    calcul.ca_statut_pourcentage = round(calcul.ca_statut_pourcentage, 2)
    calcul.save()
    if calcul.ca_statut == Statut.EN_ATTENTE_ARRET:
        raise Exception('Arret du calcul')

    logger.info("------------------Insert time -- %d seconds -----------------", time.time() - start_time_insert)

    start_time2 = time.time()
    logger.info('---------------------Preference list creation beginning-----------------')
    df_preference = to_table_liste_preference(pam_id,sv_id)

    logger.info('---------------------Preference list creation ending-------------------')
    calcul = Calcul.objects.get(id=str(sv_id)+str(pam_id),pam_id=pam_id,sous_vivier_id=sv_id)
    calcul.ca_statut_pourcentage += pourcentage_incr
    calcul.ca_statut_pourcentage = round(calcul.ca_statut_pourcentage, 2)
    calcul.save()
    if calcul.ca_statut == Statut.EN_ATTENTE_ARRET:
        raise Exception('Arret du calcul')

    try:


        start_time3 = time.time()
        logger.info('---------------------Matching beginning------------------------')
        proposant, disposant, capacite = preprocess_matching(pam_id, sv_id, df_preference)
        results = matching_parfait(proposant, disposant, capacite)
        logger.info('---------------------Matching ending---------------------------')

        calcul = Calcul.objects.get(id=str(sv_id)+str(pam_id),pam_id=pam_id,sous_vivier_id=sv_id)
        calcul.ca_statut_pourcentage += pourcentage_incr
        calcul.ca_statut_pourcentage = round(calcul.ca_statut_pourcentage, 2)
        calcul.save()
        if calcul.ca_statut == Statut.EN_ATTENTE_ARRET:
            raise Exception('Arret du calcul')

        logger.info("------------------Matching time -- %d seconds -----------------", time.time() - start_time3)
        start_time_insert = time.time()
        logger.info('---------------------insert matching results begining-----------')
        insert_matching(results, pam_id)
        logger.info('---------------------insert matching results ending-----------')

        calcul = Calcul.objects.get(id=str(sv_id)+str(pam_id),pam_id=pam_id,sous_vivier_id=sv_id)
        calcul.ca_statut_pourcentage = 100
        calcul.save()
        if calcul.ca_statut == Statut.EN_ATTENTE_ARRET:
            raise Exception('Arret du calcul')

        logger.info("------------------Insert time -- %d seconds -----------------", time.time() - start_time_insert)
    except Exception as e:
        logger.warning('Arret du scoring, veuillez renseigner au moins un département',e)



# Fonction de lancement du scoring sélectif
def lancer_calculSelectif(sv_id, pam_id, l_a_id, l_p_id):
    """
    Fonction de lancement du calcul du scoring sélectif

    :param sv_id: L'id de sous vivier étudié
    :param l_a_id: Liste d'ids d'administrés
    :param l_p_id: Liste d'ids de postes
    :type sv_id: char
    """
    Statut = StatutCalcul
    pourcentage_incr = 16.67
    calcul = Calcul.objects.get(id=str(sv_id)+str(pam_id)+'selectif',pam_id=pam_id,sous_vivier_id=sv_id)
    start_time = time.time()
    logger.info('---------------------Scoring beginning------------------------')
    notations_list = notations_liste(sv_id, pam_id, l_a_id, l_p_id)
    logger.info('---------------------Scoring ending---------------------------')

    calcul = Calcul.objects.get(id=str(sv_id)+str(pam_id)+'selectif',pam_id=pam_id,sous_vivier_id=sv_id)
    calcul.ca_statut_pourcentage += pourcentage_incr
    calcul.ca_statut_pourcentage = round(calcul.ca_statut_pourcentage, 2)
    calcul.save()
    if calcul.ca_statut == Statut.EN_ATTENTE_ARRET:
        raise Exception('Arret du calcul')

    logger.info("------------------Scoring time -- %d seconds -----------------", time.time() - start_time)
    start_time_insert = time.time()
    logger.info('---------------------insert scoring results begining-----------')
    insert_Notation(notations_list, pam_id, sv_id)
    logger.info('---------------------insert scoring results ending-----------')

    calcul = Calcul.objects.get(id=str(sv_id)+str(pam_id)+'selectif',pam_id=pam_id,sous_vivier_id=sv_id)
    calcul.ca_statut_pourcentage += pourcentage_incr
    calcul.ca_statut_pourcentage = round(calcul.ca_statut_pourcentage, 2)
    calcul.save()
    if calcul.ca_statut == Statut.EN_ATTENTE_ARRET:
        raise Exception('Arret du calcul')

    logger.info("------------------Insert time -- %d seconds -----------------", time.time() - start_time_insert)

    start_time2 = time.time()
    logger.info('---------------------Preference list creation beginning-----------------')
    df_preference = to_table_liste_preference_selectif(sv_id, pam_id,l_a_id,l_p_id)

    logger.info('---------------------Preference list creation ending-------------------')
    calcul = Calcul.objects.get(id=str(sv_id)+str(pam_id)+'selectif',pam_id=pam_id,sous_vivier_id=sv_id)
    calcul.ca_statut_pourcentage += pourcentage_incr
    calcul.ca_statut_pourcentage = round(calcul.ca_statut_pourcentage, 2)
    calcul.save()
    if calcul.ca_statut == Statut.EN_ATTENTE_ARRET:
        raise Exception('Arret du calcul')

    try:


        start_time3 = time.time()
        logger.info('---------------------Matching beginning------------------------')
        proposant, disposant, capacite = preprocess_matchingSelectif(sv_id, pam_id,l_a_id,l_p_id, df_preference)
        results = matching_parfait(proposant, disposant, capacite)
        logger.info('---------------------Matching ending---------------------------')

        calcul = Calcul.objects.get(id=str(sv_id)+str(pam_id)+'selectif',pam_id=pam_id,sous_vivier_id=sv_id)
        calcul.ca_statut_pourcentage += pourcentage_incr
        calcul.ca_statut_pourcentage = round(calcul.ca_statut_pourcentage, 2)
        calcul.save()
        if calcul.ca_statut == Statut.EN_ATTENTE_ARRET:
            raise Exception('Arret du calcul')

        logger.info("------------------Matching time -- %d seconds -----------------", time.time() - start_time3)
        start_time_insert = time.time()
        logger.info('---------------------insert matching results begining-----------')
        insert_matching(results, pam_id)
        logger.info('---------------------insert matching results ending-----------')

        calcul = Calcul.objects.get(id=str(sv_id)+str(pam_id)+'selectif',pam_id=pam_id,sous_vivier_id=sv_id)
        calcul.ca_statut_pourcentage = 100
        calcul.save()
        if calcul.ca_statut == Statut.EN_ATTENTE_ARRET:
            raise Exception('Arret du calcul')

        logger.info("------------------Insert time -- %d seconds -----------------", time.time() - start_time_insert)
    except Exception as e:
        logger.warning('Arret du scoring, veuillez renseigner au moins un département',e)

    return


