# -*- coding: UTF-8 -*
##############################################################################
# ScoDoc
# Copyright (c) 1999 - 2022 Emmanuel Viennet.  All rights reserved.
# See LICENSE
##############################################################################

from threading import Thread

from flask import current_app, g
from flask_mail import Message

from app import mail
from app.scodoc import sco_preferences


def send_async_email(app, msg):
    "Send an email, async"
    with app.app_context():
        mail.send(msg)


def send_email(
    subject: str,
    sender: str,
    recipients: list,
    text_body: str,
    html_body="",
    bcc=(),
    attachments=(),
):
    """
    Send an email. _All_ ScoDoc mails SHOULD be sent using this function.

    If html_body is specified, build a multipart message with HTML content,
    else send a plain text email.

    attachements: list of dict { 'filename', 'mimetype', 'data' }
    """
    msg = Message(subject, sender=sender, recipients=recipients, bcc=bcc)
    msg.body = text_body
    msg.html = html_body
    if attachments:
        for attachment in attachments:
            msg.attach(
                attachment["filename"], attachment["mimetype"], attachment["data"]
            )

    send_message(msg)


def send_message(msg: Message):
    """Send a message.
    All ScoDoc emails MUST be sent by this function.

    In mail debug mode, addresses are discarded and all mails are sent to the
    specified debugging address.
    """
    if hasattr(g, "scodoc_dept"):
        # on est dans un département, on peut accéder aux préférences
        email_test_mode_address = sco_preferences.get_preference(
            "email_test_mode_address"
        )
        if email_test_mode_address:
            # Mode spécial test: remplace les adresses de destination
            orig_to = msg.recipients
            orig_cc = msg.cc
            orig_bcc = msg.bcc
            msg.recipients = [email_test_mode_address]
            msg.cc = None
            msg.bcc = None
            msg.subject = "[TEST SCODOC] " + msg.subject
            msg.body = (
                f"""--- Message ScoDoc dérouté pour tests ---
Adresses d'origine:
 to : {orig_to}
 cc : {orig_cc}
 bcc: {orig_bcc}
--- 
            \n\n"""
                + msg.body
            )

    Thread(
        target=send_async_email, args=(current_app._get_current_object(), msg)
    ).start()
