from datetime import datetime

from pyramid.renderers import render

from pyramid_mailer import get_mailer
from pyramid_mailer.message import Message

from eduid_am.tasks import update_attributes

from eduid_signup.utils import generate_verification_link, generate_eppn


def send_verification_mail(request, email):
    mailer = get_mailer(request)
    (verification_link, code) = generate_verification_link(request)
    _ = request.translate

    context = {
        "email": email,
        "verification_link": verification_link,
        "site_url": request.route_url("home"),
        "site_name": request.registry.settings.get("site.name", "eduid_signup"),
        "code": code,
        "verification_code_form_link": request.route_url("verification_code_form"),
    }

    message = Message(
        subject=_("eduid-signup verification email"),
        sender=request.registry.settings.get("mail.default_sender"),
        recipients=[email],
        body=render(
            "templates/verification_email.txt.jinja2",
            context,
            request,
        ),
        html=render(
            "templates/verification_email.html.jinja2",
            context,
            request,
        ),
    )

    eppn = generate_eppn(request)
    result = request.db.registered.find_and_modify(
        query={
            'email': email,
        }, update={
            '$set': {
                "subject": "physical person",
                "eduPersonPrincipalName": eppn,
                "email": email,
                "created_ts": datetime.utcnow(),
                "code": code,
                "verified": False,
            },
        }, upsert=True, full_response=True, new=True, safe=True)

    user_id = result.get("value", {}).get("_id")

    mailer.send(message)

    # XXX REMOVE THIS? otherwise users appear in eduid_am without 'passwords'
    # so they can't log in but exceptions are logged in the IdP when they try

    # Send the signal to the attribute manager so it can update
    # this user's attributes in the IdP
    update_attributes.delay('eduid_signup', str(user_id))
    # Development
    if request.registry.settings.get("development", '') == 'true':
        for mail in mailer.outbox:
            print mail.body


def send_credentials(request, email, password):
    _ = request.translate
    mailer = get_mailer(request)
    context = {
        "email": email,
        "password": password,
        "site_url": request.route_url("home"),
        "site_name": request.registry.settings.get("site.name", "eduid_signup"),
    }
    message = Message(
        subject=_("eduid-signup credentials"),
        sender=request.registry.settings.get("mail.default_sender"),
        recipients=[email],
        body=render(
            "templates/credentials_email.txt.jinja2",
            context,
            request,
        ),
        html=render(
            "templates/credentials_email.html.jinja2",
            context,
            request,
        ),
    )
    mailer.send(message)
    # Development
    if request.registry.settings.get("development", '') == 'true':
        for mail in mailer.outbox:
            print mail.body

@@ -264,16 +264,69 @@
 e_eppn%0A%0A
+import logging%0Alogger = logging.getLogger(__name__)%0A%0A
 %0Adef sen
@@ -1806,35 +1806,314 @@
 %22)%0A%0A    
-mailer.send(message
+if request.registry.settings.get(%22development%22, '') != 'true':%0A        mailer.send(message)%0A    else:%0A        # Development%0A        logger.info(%22Confirmation e-mail:%5CnFrom: %7B!s%7D%5CnTo: %7B!s%7D%5CnSubject: %7B!s%7D%5Cn%5Cn%7B!s%7D%22.format(%0A            message.sender, message.recipients, message.subject, message.body)
 )%0A%0A    #
@@ -2430,156 +2430,8 @@
 d))%0A
-    # Development%0A    if request.registry.settings.get(%22development%22, '') == 'true':%0A        for mail in mailer.outbox:%0A            print mail.body%0A
 %0A%0Ade
@@ -3173,51 +3173,8 @@
   )%0A
-    mailer.send(message)%0A    # Development%0A
     
@@ -3225,17 +3225,17 @@
 t%22, '') 
-=
+!
 = 'true'
@@ -3248,59 +3248,232 @@
     
-for mail in mailer.outbox:%0A            print mail
+mailer.send(message)%0A    else:%0A        # Development%0A        logger.info(%22Credentials e-mail:%5CnFrom: %7B!s%7D%5CnTo: %7B!s%7D%5CnSubject: %7B!s%7D%5Cn%5Cn%7B!s%7D%22.format(%0A            message.sender, message.recipients, message.subject, message
 .body
+))
 %0A

Fix not actually sending e-mails when in development mode.