from __future__ import absolute_import

import traceback

from celery import shared_task

from django.conf import settings

from takeyourmeds.utils.dt import local_time
from takeyourmeds.utils.twilio import get_twilio_client

from .enums import TypeEnum, SourceEnum
from .models import Reminder, Time

@shared_task()
def schedule_reminders():
    for x in Time.objects.filter(time='%02d:00' % local_time().hour):
        trigger_reminder.delay(x.reminder_id, SourceEnum.cron.value)

@shared_task()
def trigger_reminder(reminder_id, source=SourceEnum.manual.value):
    reminder = Reminder.objects.get(pk=reminder_id)

    instance = reminder.instances.create(source=source)

    notification = getattr(
        instance,
        '%ss' % reminder.get_type_enum().name,
    ).create()

    # We won't get a callback if Twilio is disabled, so let's set a special
    # state to not have confusing "in progress" messages outside of a live
    # environment.
    if not settings.TWILIO_ENABLED:
        notification.state = 10 # twilio_disabled

    try:
        resource = notify(notification)
    except:
        notification.state = 20 # failed
        notification.traceback = traceback.format_exc()
        raise
    else:
        notification.twilio_sid = resource.sid
    finally:
        notification.save()

    return repr((reminder, instance, notification))

def notify(notification):
    reminder = notification.instance.reminder

    if reminder.type == TypeEnum.message:
        return get_twilio_client().messages.create(
            to=reminder.get_phone_number(),
            body=reminder.message,
            from_=settings.TWILIO_MESSAGE_FROM,
            status_callback=notification.get_status_callback_url(),
        )

    if reminder.type == TypeEnum.call:
        return get_twilio_client().calls.create(
            to=reminder.get_phone_number(),
            from_=settings.TWILIO_CALL_FROM,
            url=notification.get_twiml_callback_url(),
            status_callback=notification.get_status_callback_url(),
        )

    raise NotImplementedError("Unhandled reminder type")

@@ -807,16 +807,28 @@
 allback 
+from Twilio 
 if Twili
@@ -860,16 +860,22 @@
 et a
+%0A    #
  special
 %0A   
@@ -870,22 +870,16 @@
  special
-%0A    #
  state t
@@ -938,19 +938,19 @@
 of a
- live
 %0A    #
+ live
  env

Clarify language.