# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
from django.db import transaction
from django.core.mail import send_mail
from django.conf import settings
from django.template import Context, Template
from .models import Notification, NotifiedUser


@transaction.atomic
def feed_alert(source, subject, body, context):
    notification = Notification.objects.create(
        name=subject,
        description=body,
        source=source
    )
    NotifiedUser.objects.create(
        notification=notification,
        did_read=False,
        recipient_id=context.get('notified_user_id'),
    )


def send_notification(subject, body, cc=[]):
    send_mail(subject, body, settings.DEFAULT_FROM_EMAIL, cc)


def get_template_as_str(filename, context):
    main_dir_name = os.path.dirname(os.path.abspath(__file__))
    temp_file = open(os.path.join(main_dir_name, 'standard_emails', filename))
    template = Template(temp_file.read())
    return template.render(Context(context))


def send_account_approval_activated_create_profile(context, cc):
    source = 'account_approval_activated_create_profile'
    subject = "Title"
    body = get_template_as_str(source, context)
    send_notification(subject, body, cc)
    feed_alert(source, subject, body, context)


def send_account_approval_activated_sent_to_head_org(context, cc):
    source = 'account_approval_activated_sent_to_head_org'
    subject = "Title"
    body = get_template_as_str(source, context)
    send_notification(subject, body, cc)
    feed_alert(source, subject, body, context)


def send_account_approval_rejection_application_duplicate(context, cc):
    source = 'account_approval_rejection_application_duplicate'
    subject = "Title"
    body = get_template_as_str(source, context)
    send_notification(subject, body, cc)
    feed_alert(source, subject, body, context)


def send_account_approval_rejection_sanctions_list(context, cc):
    source = 'account_approval_rejection_application_duplicate'
    subject = "Title"
    body = get_template_as_str(source, context)
    send_notification(subject, body, cc)
    feed_alert(source, subject, body, context)


def send_account_creation_rejection(context, cc):
    source = 'account_approval_rejection_application_duplicate'
    subject = "Title"
    body = get_template_as_str(source, context)
    send_notification(subject, body, cc)
    feed_alert(source, subject, body, context)


def send_cancel_cfei(context, cc):
    source = 'cancel_CFEI'
    subject = "Title"
    body = get_template_as_str(source, context)
    send_notification(subject, body, cc)
    feed_alert(source, subject, body, context)


def send_cn_assessment(context, cc):
    source = 'CN_Assessment'
    subject = "Title"
    body = get_template_as_str(source, context)
    send_notification(subject, body, cc)
    feed_alert(source, subject, body, context)


def send_cn_assessment_not_successful(context, cc):
    source = 'cn_assessment_not_successful'
    subject = "Title"
    body = get_template_as_str(source, context)
    send_notification(subject, body, cc)
    feed_alert(source, subject, body, context)


def send_cn_assessment_successful(context, cc):
    source = 'CN_Assessment_successful'
    subject = "Title"
    body = get_template_as_str(source, context)
    send_notification(subject, body, cc)
    feed_alert(source, subject, body, context)


def send_cn_submission(context, cc):
    source = 'CN_Submission'
    subject = "Title"
    body = get_template_as_str(source, context)
    send_notification(subject, body, cc)
    feed_alert(source, subject, body, context)


def send_cn_unsolicited(context, cc):
    source = 'CN_Unsolicited'
    subject = "Title"
    body = get_template_as_str(source, context)
    send_notification(subject, body, cc)
    feed_alert(source, subject, body, context)


def send_direct_selection_un_initiated(context, cc):
    source = 'direct_selection_UN_initiated'
    subject = "Title"
    body = get_template_as_str(source, context)
    send_notification(subject, body, cc)
    feed_alert(source, subject, body, context)


def send_direct_selection_via_ucn(context, cc):
    source = 'direct_selection_via_UCN'
    subject = "Title"
    body = get_template_as_str(source, context)
    send_notification(subject, body, cc)
    feed_alert(source, subject, body, context)


def send_new_cfei_inviting(context, cc):
    source = 'New_CFEI_inviting'
    subject = "Title"
    body = get_template_as_str(source, context)
    send_notification(subject, body, cc)
    feed_alert(source, subject, body, context)


def send_update_cfei_prev_invited_submited_app(context, cc):
    source = 'Update_CFEI_prev_invited_submited_app'
    subject = "Title"
    body = get_template_as_str(source, context)
    send_notification(subject, body, cc)
    feed_alert(source, subject, body, context)

@@ -468,45 +468,130 @@
     
-N
+n
 otified
-User.objects.create(%0A        
+_users = %5B%5D%0A    for recipient_id in context.get('notified_users_pks'):%0A        notified_users.append(NotifiedUser(
 noti
@@ -612,24 +612,16 @@
 ication,
-%0A       
  did_rea
@@ -628,24 +628,16 @@
 d=False,
-%0A       
  recipie
@@ -642,29 +642,68 @@
 ient_id=
-context.get('
+recipient_id))%0A    NotifiedUser.objects.bulk_create(
 notified
@@ -711,19 +711,9 @@
 user
-_id'),%0A    
+s
 )%0A%0A%0A

make NotifiedUser with bulk_create function