from amnesia.ts.models import Ticket
from django.contrib.auth.models import User
from amnesia.settings import AMNESIA_FROM

def gen_comment(request, ticket, comment):
    subject = '%s: #%d [comment] %s' %(ticket.queue.name, ticket.id, ticket.s_desc)
    text = '''This is a comment by %s.  It is not sent to the Requestor(s):
%s''' %(request.user.username, comment)
    return (subject, text)

def gen_status(ticket):
    subject = '%s: #%d [status] %s' %(ticket.queue.name, ticket.id, ticket.s_desc)
    text = '''The status of ticket #%d changed, the new status is: %s''' %(ticket.id, ticket.get_status())
    return (subject, text)

def gen_take(request, ticket):
    subject = '%s: #%d [responsible] %s' %(ticket.queue.name, ticket.id, ticket.s_desc)
    text = '''Ticket #%d has been taken by %s''' %(ticket.id, request.user.username)
    return (subject, text)

def gen_untake(request, ticket):
    subject = '%s: #%d [no owner] %s' %(ticket.queue.name, ticket.id, ticket.s_desc)
    text = '''The ticket #%d has no owner anymore, %s dropped it''' %(ticket.id, request.user.username)
    return (subject, text)

def gen_new(request, ticket):
    subject = '%s: #%d [new] %s' %(ticket.queue.name, ticket.id, ticket.s_desc)
    resp = None
    if ticket.resp:
        resp = ticket.resp.username

    text = '''%s opened by %s
Queue       : %s
Subject     : %s
Responsible : %s
Requestor   : %s
Status      : new
Content     :
%s''' %(ticket.s_date, request.user.username, ticket.queue.name, ticket.s_desc, resp, ticket.creator, ticket.l_desc)
    return (subject, text)

def gen_edit(request, ticket):
    subject = '%s: #%d [edit] %s' %(ticket.queue.name, ticket.id, ticket.s_desc)
    resp = None
    if ticket.resp:
        resp = ticket.resp.username

    text = '''%s has been edited by %s, new ticket information:
Queue       : %s
Subject     : %s
Responsible : %s
Requestor   : %s
Status      : %s
Content     :
%s''' %(ticket.s_date, request.user.username, ticket.queue.name, ticket.s_desc, resp, ticket.creator, ticket.get_status(), ticket.l_desc)
    return (subject, text)


def generate_body(type, request, ticket, comment=None):
    options = {
        'comment' : gen_comment(request, ticket, comment),
        'status'  : gen_status(ticket),
        'take'    : gen_take(request, ticket),
        'untake'  : gen_untake(request, ticket),
        'new'     : gen_new(request, ticket),
        'edit'    : gen_edit(request, ticket)
    }
    mail    = options[type]
    subject = mail[0]
    text    = mail[1]
    text    = '<URL: http://' + request.get_host() + '/ts/detail/' + str(ticket.id) +' />\n\n' + text
    return (subject, text)


def email_notify(type, ticket, request, comment=None):
    from django.core.mail import send_mail
    recipients = []
    if type == 'new' or ticket.resp == None:
        foo = User.objects.all()
    else:
        foo = ticket.subscr.all()

    for i in foo:
        recipients.append(i.email)

    import email_notification
    (subject, text) = email_notification.generate_body(type, request, ticket, comment)

    send_mail(subject, text, AMNESIA_FROM, recipients, fail_silently=False, auth_user=None, auth_password=None)

