# -*- coding: utf-8 -*-
from django.conf import settings
from django.contrib.sites.models import Site
from django.core.mail import EmailMultiAlternatives
from django.template import Context
from django.template.loader import render_to_string
from django.utils.translation import ugettext as _

import lfs.core.utils


def notify_payment_cleared(order):
    shop = lfs.core.utils.get_default_shop()
    ctx = Context({
        'order': order,
        'user': order.user,
        'shop': shop,
        'product': order.product,
        'site_url': Site.objects.get(id=settings.SITE_ID).domain,
    })

    EmailMultiAlternatives(
        subject=_(u"Payment received!"),
        body=render_to_string(
            'lfs_downloads/mail/payment_cleared.txt',
            ctx
        ),
        from_email=shop.from_email,
        to=[order.user.email, ],
        bcc=shop.get_notification_emails(),
        alternatives=[
            (render_to_string('lfs_downloads/mail/payment_cleared.html', ctx), "text/html")
        ]
    ).send(fail_silently=True)


def notify_payment_failed(order):
    shop = lfs.core.utils.get_default_shop()
    ctx = Context({
        'order': order,
        'user': order.user,
        'shop': shop,
        'product': order.product,
        'site_url': Site.objects.get(id=settings.SITE_ID).domain,
    })

    EmailMultiAlternatives(
        subject=_(u"Something went wrong with your payment"),
        body=render_to_string(
            'lfs_downloads/mail/payment_failed.txt',
            ctx
        ),
        from_email=shop.from_email,
        to=[order.user.email, ],
        bcc=shop.get_notification_emails(),
        alternatives=[
            (render_to_string('lfs_downloads/mail/payment_failed.html', ctx), "text/html")
        ]
    ).send(fail_silently=True)


def notify_order_delivered(order):
    shop = lfs.core.utils.get_default_shop()
    ctx = Context({
        'order': order,
        'user': order.user,
        'shop': shop,
        'product': order.product,
        'site_url': Site.objects.get(id=settings.SITE_ID).domain,
    })

    EmailMultiAlternatives(
        subject=_(u"Your product is ready to download!"),
        body=render_to_string(
            'lfs_downloads/mail/order_delivered.txt',
            ctx
        ),
        from_email=shop.from_email,
        to=[order.user.email, ],
        bcc=shop.get_notification_emails(),
        alternatives=[
            (render_to_string('lfs_downloads/mail/order_delivered.html', ctx), "text/html")
        ]
    ).send(fail_silently=True)
