from django.core.mail import send_mail, EmailMultiAlternatives, EmailMessage
from django.template.loader import render_to_string
from django.utils.html import strip_tags
import re
from N3wtSchool import settings

def envoieReinitMotDePasse(recipients, code):
    errorMessage = ''
    try:
        EMAIL_REINIT_SUBJECT = 'Réinitialisation du mot de passe'
        context = {
            'BASE_URL': settings.BASE_URL,
            'code': str(code)
        }
        subject = EMAIL_REINIT_SUBJECT
        html_message  = render_to_string('emails/resetPassword.html', context)
        plain_message = strip_tags(html_message)
        from_email = settings.EMAIL_HOST_USER
        send_mail(subject, plain_message, from_email, [recipients], html_message=html_message)
    except Exception as e:
        errorMessage = str(e)

    return errorMessage


def sendRegisterForm(recipients, establishment_id):
    errorMessage = ''
    try:
        # Préparation du contexte pour le template
        EMAIL_INSCRIPTION_SUBJECT = '[N3WT-SCHOOL] Dossier Inscription'
        context = {
            'BASE_URL': settings.BASE_URL,
            'email': recipients,
            'establishment': establishment_id
        }

        subject = EMAIL_INSCRIPTION_SUBJECT
        html_message  = render_to_string('emails/inscription.html', context)
        plain_message = strip_tags(html_message)
        from_email = settings.EMAIL_HOST_USER

        send_mail(subject, plain_message, from_email, [recipients], html_message=html_message)

    except Exception as e:
        errorMessage = str(e)

    return errorMessage

def sendMandatSEPA(recipients, establishment_id):
    errorMessage = ''
    try:
        # Préparation du contexte pour le template
        EMAIL_INSCRIPTION_SUBJECT = '[N3WT-SCHOOL] Mandat de prélèvement SEPA'
        context = {
            'BASE_URL': settings.BASE_URL,
            'email': recipients,
            'establishment': establishment_id
        }

        subject = EMAIL_INSCRIPTION_SUBJECT
        html_message  = render_to_string('emails/sepa.html', context)
        plain_message = strip_tags(html_message)
        from_email = settings.EMAIL_HOST_USER

        send_mail(subject, plain_message, from_email, [recipients], html_message=html_message)

    except Exception as e:
        errorMessage = str(e)

    return errorMessage

def envoieRelanceDossierInscription(recipients, code):
    EMAIL_RELANCE_SUBJECT = '[N3WT-SCHOOL] Relance - Dossier Inscription'
    EMAIL_RELANCE_CORPUS = 'Bonjour,\nN\'ayant pas eu de retour de votre part, nous vous renvoyons le lien vers le formulaire d\'inscription : ' + BASE_URL + '/users/login\nCordialement'
    errorMessage = ''
    try:
        send_mail(
                EMAIL_RELANCE_SUBJECT,
                EMAIL_RELANCE_CORPUS%str(code),
                settings.EMAIL_HOST_USER,
                [recipients],
                fail_silently=False,
            )
    except Exception as e:
        errorMessage = str(e)

    return errorMessage

def isValid(message, fiche_inscription):
    # Est-ce que la référence du dossier est VALIDATED
    subject = message.subject
    print ("++++ " + subject)
    responsableMail = message.from_header
    result = re.search('<(.*)>', responsableMail)

    if result:
        responsableMail = result.group(1)

    result = re.search(r'.*\[Ref(.*)\].*', subject)
    idMail = -1
    if result:
        idMail = result.group(1).strip()

    eleve = fiche_inscription.eleve
    responsable = eleve.getMainGuardian()
    mailReponsableAVerifier = responsable.mail

    return responsableMail == mailReponsableAVerifier and str(idMail) == str(fiche_inscription.eleve.id)