# Copyright (C) 2015 Google Inc., authors, and contributors <see AUTHORS file>
# Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
# Created By: dan@reciprocitylabs.com
# Maintained By: miha@reciprocitylabs.com

from ggrc import db
from ggrc_workflows import start_recurring_cycles
from ggrc import notification
from ggrc.login import login_required
from ggrc.notification import email
from jinja2 import Environment, PackageLoader
from datetime import date, datetime
from ggrc.rbac import permissions
from werkzeug.exceptions import Forbidden


env = Environment(loader=PackageLoader('ggrc_workflows', 'templates'))

# TODO: move these views to ggrc_views and all the functions to notifications
# module.


def do_start_recurring_cycles():
  start_recurring_cycles()
  return 'Ok'


def show_pending_notifications():
  if not permissions.is_admin():
    raise Forbidden()
  _, notif_data = notification.get_pending_notifications()
  pending = env.get_template("notifications/view_pending_digest.html")
  return pending.render(data=sorted(notif_data.iteritems()))


def show_todays_digest_notifications():
  if not permissions.is_admin():
    raise Forbidden()
  _, notif_data = notification.get_todays_notifications()
  todays = env.get_template("notifications/view_todays_digest.html")
  return todays.render(data=notif_data)


def set_notification_sent_time(notifications):
  for notif in notifications:
    notif.sent_at = datetime.now()
  db.session.commit()


def send_pending_notifications():
  digest_template = env.get_template("notifications/email_digest.html")
  notifications, notif_data = notification.get_pending_notifications()
  sent_emails = []
  for day, day_notif in notif_data.iteritems():
    subject = "gGRC daily digest for {}".format(day.strftime("%b %d"))
    for user_email, data in day_notif.iteritems():
      email_body = digest_template.render(digest=data)
      email.send_email(user_email, subject, email_body)
      sent_emails.append(user_email)
  set_notification_sent_time(notifications)
  return "emails sent to: <br> {}".format("", "<br>".join(sent_emails))


def send_todays_digest_notifications():
  digest_template = env.get_template("notifications/email_digest.html")
  notifications, notif_data = notification.get_todays_notifications()
  sent_emails = []
  subject = "gGRC daily digest for {}".format(date.today().strftime("%b %d"))
  for user_email, data in notif_data.iteritems():
    email_body = digest_template.render(digest=data)
    email.send_email(user_email, subject, email_body)
    sent_emails.append(user_email)
  set_notification_sent_time(notifications)
  return "emails sent to: <br> {}".format("", "<br>".join(sent_emails))


def init_extra_views(app):
  app.add_url_rule(
      "/start_recurring_cycles", "start_recurring_cycles",
      view_func=do_start_recurring_cycles)

  app.add_url_rule(
      "/_notifications/show_pending", "show_pending_notifications",
      view_func=login_required(show_pending_notifications))

  app.add_url_rule(
      "/_notifications/show_todays_digest", "show_todays_digest_notifications",
      view_func=login_required(show_todays_digest_notifications))

  app.add_url_rule(
      "/_notifications/send_pending", "send_pending_notifications",
      view_func=login_required(send_pending_notifications))

  app.add_url_rule(
      "/_notifications/send_todays_digest", "send_todays_digest_notifications",
      view_func=send_todays_digest_notifications)

@@ -728,16 +728,314 @@
 dule.%0A%0A%0A
+def modify_data(data):%0A  %22%22%22%0A  for easyer use in templates, it joins the due_in and due today fields%0A  together%0A  %22%22%22%0A%0A  data%5B%22due_soon%22%5D = %7B%7D%0A  if %22due_in%22 in data:%0A    data%5B%22due_soon%22%5D.update(data%5B%22due_in%22%5D)%0A  if %22due_today%22 in data:%0A    data%5B%22due_soon%22%5D.update(data%5B%22due_today%22%5D)%0A  return data%0A%0A
 def do_s
@@ -1059,16 +1059,16 @@
 cles():%0A
-
   start_
@@ -1249,16 +1249,147 @@
 ations()
+%0A%0A  for day, day_notif in notif_data.iteritems():%0A    for user_email, data in day_notif.iteritems():%0A      data = modify_data(data)
 %0A  pendi
@@ -1602,32 +1602,111 @@
 ise Forbidden()%0A
+  for user_email, data in notif_data.iteritems():%0A    data = modify_data(data)%0A
   _, notif_data 
@@ -2356,24 +2356,55 @@
 teritems():%0A
+      data = modify_data(data)%0A
       email_
@@ -2936,16 +2936,16 @@
 b %25d%22))%0A
-
   for us
@@ -2982,24 +2982,53 @@
 teritems():%0A
+    data = modify_data(data)%0A
     email_bo

Add extra data to notification object