# By: Riasat Ullah
# This file contains content for incident related notifications.

from translators import label_translator as _lt
from utils import constants, helpers, label_names as lbl, mail_content_labels as mcl, url_paths, var_names
import datetime


########################
# Incident Trigger
########################

def dispatch_email_content(lang, org_instance_id, instance_open_time, assignees_str, service_name, title, details,
                           urgency, impacted_business_service_names, timezone=None, conference_bridge=None):
    '''
    The email content of a dispatch event notice.
    :param lang: content language
    :param org_instance_id: (int) organization instance ID
    :param instance_open_time: (datetime.datetime) instance open time
    :param assignees_str: (str) assignees separated by comma
    :param service_name: name of the service that triggered the instance
    :param title: title of the instance
    :param details: details of the instance
    :param urgency: (int) urgency level of the task
    :param impacted_business_service_names: (str) names of all the business services that have been impacted
    :param timezone: (optional) timezone the instance time is in
    :param conference_bridge: (dict) -> {conference phone: ..., conference url: ...}
    :return: (tuple) -> email subject, email body
    '''
    assert isinstance(instance_open_time, datetime.datetime)
    assert isinstance(urgency, int)
    str_urgency = helpers.get_urgency_map(urgency, lang)
    inst_time_words = instance_open_time.strftime(constants.timestamp_words_format)
    if timezone is not None:
        inst_time_words += ' (' + timezone + ')'

    conf_details = conference_bridge_content_for_email(lang, conference_bridge)

    subject = 'TaskCall - {0} - {1}'.format(_lt.get_label(lbl.ttl_incident, lang), title)
    notice = '''
             <p> {0} </p>
             <p>
                {1}: {2} <br/>
                {3}: {4} <br/>
                {5}: {6} <br/>
                {7}: {8} <br/>
                {9}: {10} <br/>
                {11}: {12} <br/>
                {18}
                {13}: <a href="{17}/{2}"> {14} </a> <br/>
                {15}:
             </p>
             <pre style="font-family: sans-serif;"> {16} </pre>
             '''.format(_lt.get_label(mcl.mcl_body_open_incident_please_attend, lang),
                        _lt.get_label(lbl.ttl_incident_id, lang), str(org_instance_id),
                        _lt.get_label(lbl.ttl_created_on, lang), inst_time_words,
                        _lt.get_label(lbl.det_assigned_to, lang), assignees_str,
                        _lt.get_label(lbl.det_service, lang),  '' if service_name is None else service_name,
                        _lt.get_label(lbl.ttl_impacted_business_services, lang), impacted_business_service_names,
                        _lt.get_label(lbl.det_urgency, lang), str_urgency,
                        _lt.get_label(lbl.ttl_incident_title, lang), title,
                        _lt.get_label(lbl.ttl_details, lang), details,
                        url_paths.web_incidents_details, conf_details)
    return subject, notice


def dispatch_push_notice_content(lang, org_instance_id, instance_open_time, title):
    '''
    The content of an app push notification of a dispatch event.
    :param lang: content language
    :param org_instance_id: (int) organization instance ID
    :param instance_open_time: (datetime.datetime) time when instance was opened
    :param title: title of the instance
    :return: (tuple) -> notice title, instance title
    '''
    assert isinstance(instance_open_time, datetime.datetime)
    notice_title = '{0} - {1}'.format(_lt.get_label(lbl.ttl_open_incident, lang),
                                      instance_open_time.strftime(constants.timestamp_words_format))
    body = '#' + str(org_instance_id) + ' - ' + title
    return notice_title, body


def dispatch_sms_content(lang, org_instance_id, task_title, ack_code, com_code, esc_code):
    '''
    The phone text and voice content of a dispatch event notice.
    :param lang: content language
    :param org_instance_id: (int) organization instance ID
    :param task_title: (str) title of the task
    :param ack_code: acknowledgement code
    :param com_code: completion code
    :param esc_code: escalation code
    :return: (str) message
    '''
    escalation_cond = ''
    if esc_code is not None:
        escalation_cond = ', {0} {1}'.format(str(esc_code), _lt.get_label(lbl.dsm_to_escalate, lang))

    notice = "{0} - #{1} {2}. {3} {4} {5}, {6} {7}{8}".format(
        _lt.get_label(lbl.ttl_incident, lang), str(org_instance_id),
        task_title if len(task_title) <= 60 else task_title[:57] + '...',
        _lt.get_label(lbl.ins_press, lang),
        str(ack_code), _lt.get_label(lbl.dsm_to_acknowledge, lang),
        str(com_code), _lt.get_label(lbl.dsm_to_resolve, lang),
        escalation_cond
    )
    return notice


def dispatch_voice_content(lang, org_instance_id, task_title, ack_code, com_code, esc_code):
    '''
    The phone text and voice content of a dispatch event notice.
    :param lang: content language
    :param org_instance_id: (int) organization instance ID
    :param task_title: (str) title of the task
    :param ack_code: acknowledgement code
    :param com_code: completion code
    :param esc_code: escalation code
    :return: (str) message
    '''
    escalation_cond = ''
    if esc_code is not None:
        escalation_cond = ', {0} {1}'.format(str(esc_code), _lt.get_label(lbl.dsm_to_escalate, lang))

    notice = "{0} {1} - {2} {3}. {4} {5} {6}, {7} {8}{9}".format(
        _lt.get_label(mcl.mcl_body_you_have_an_open_incident, lang),
        _lt.get_label(lbl.ttl_incident_id, lang), str(org_instance_id),
        task_title if len(task_title) <= 80 else task_title[:77] + '...',
        _lt.get_label(lbl.ins_press, lang),
        str(ack_code), _lt.get_label(lbl.dsm_to_acknowledge, lang),
        str(com_code), _lt.get_label(lbl.dsm_to_resolve, lang),
        escalation_cond
    )
    return notice


###########################
# Conference Bridges
###########################

def conference_bridge_content_for_email(lang, conference_bridge):
    '''
    Get the conference bridge part of the email content.
    :param lang: language the card should be in
    :param conference_bridge: (dict) -> {conference_url: .., conference_phone: ..}
    :return: (str) html for the email content
    '''
    conf_details = ''
    if conference_bridge is not None:
        if var_names.conference_url in conference_bridge:
            conf_url = conference_bridge[var_names.conference_url]
            if conf_url is not None:
                conf_details += '{0}: <a href="{1}"> {1} </a> <br/>'.format(
                    _lt.get_label(lbl.ttl_conference_url, lang), conf_url)
        if var_names.conference_phone in conference_bridge:
            conf_phone = conference_bridge[var_names.conference_phone]
            if conf_phone:
                conf_details += '{0}: <a href="tel:{1}"> {2} </a> <br/>'.format(
                    _lt.get_label(lbl.ttl_dial_in_number, lang), conf_phone.replace('-', ''),
                    conf_phone.split(',')[0].split('#')[0]
                )
    return conf_details


def conference_bridge_added_email_content(lang, org_instance_id, incident_title, conference_bridge):
    '''
    Gets the email content that should be sent out when a new conference bridge is added.
    :param lang: content language
    :param org_instance_id: (int) organization instance ID
    :param incident_title: (str) title of the incident
    :param conference_bridge: (dict) -> {conference phone: ..., conference url: ...}
    :return: (tuple) -> email subject, email body
    '''
    subject = 'TaskCall - {0} - {1} #{2}'.format(_lt.get_label(mcl.mcl_sbj_conference_bridge_join, lang),
                                                 _lt.get_label(lbl.ttl_incident_id, lang), str(org_instance_id))
    conf_details = conference_bridge_content_for_email(lang, conference_bridge)
    notice = '''
             <p> {0} </p>
             <p>
                {1}: {2} <br/>
                {3}: {4} <br/>
                {5}
             </p>
             '''.format(_lt.get_label(mcl.mcl_body_conference_bridge_invited, lang),
                        _lt.get_label(lbl.ttl_incident_id, lang), str(org_instance_id),
                        _lt.get_label(lbl.ttl_incident_title, lang), incident_title,
                        conf_details)
    return subject, notice


def conference_bridge_added_app_content(lang, org_incident_id, incident_title, conference_bridge):
    '''
    Push notification content for informing responders when a new conference bridge is added.
    :param lang: content language
    :param org_incident_id: (int) ID of the instance
    :param incident_title: (str) the title of the incident
    :param conference_bridge: (dict) -> {conference phone: ..., conference url: ...}
    :return: (tuple) -> notification title, notification body
    '''
    inc_title = incident_title if len(incident_title) <= 40 else incident_title[:37] + '...'
    notice_title = _lt.get_label(mcl.mcl_sbj_conference_bridge_join, lang) +\
        ' | #' + str(org_incident_id) + ' ' + inc_title
    body = ''
    if var_names.conference_url in conference_bridge and conference_bridge[var_names.conference_url] is not None:
        body += '{0}: {1}'.format(_lt.get_label(lbl.ttl_conference_url, lang),
                                  conference_bridge[var_names.conference_url])
    if var_names.conference_phone in conference_bridge and conference_bridge[var_names.conference_phone] is not None:
        if len(body) > 0:
            body += ' | '
        body += '{0}: {1} '.format(_lt.get_label(lbl.ttl_dial_in_number, lang),
                                   conference_bridge[var_names.conference_phone])
    return notice_title, body


######################
# Status Updates
######################

def instance_status_update_email_content(lang, org_instance_id, incident_time, incident_title, status_update):
    '''
    Gets the email content for status updates provided on incidents.
    :param lang: content language
    :param org_instance_id: (int) organization instance ID
    :param incident_time: (datetime.datetime) time the incident was created on
    :param incident_title: (str) title of the incident
    :param status_update: (str) status update
    :return: (tuple) -> email subject, email body
    '''
    subject = 'TaskCall - {0} - {1} #{2}'.format(_lt.get_label(lbl.ttl_status_updates, lang),
                                                 _lt.get_label(lbl.ttl_incident_id, lang), str(org_instance_id))
    notice = '''
             <p> {0}... </p>
             <p>
                {1}: {2} <br/>
                {3}: {4} <br/>
                {5}: {6} <br/>
                <br/>

                <strong> {7} </strong> <br/>
                {8}
             </p>

             <p>
                {9}: <a href="{10}"> {10} </a>
             </p>
             '''.format(_lt.get_label(mcl.mcl_body_you_have_status_update, lang),
                        _lt.get_label(lbl.ttl_incident_id, lang), str(org_instance_id),
                        _lt.get_label(lbl.ttl_created_on, lang),
                        incident_time.strftime(constants.timestamp_words_format),
                        _lt.get_label(lbl.ttl_incident_title, lang), incident_title,
                        _lt.get_label(lbl.ttl_new_update, lang), status_update,
                        _lt.get_label(mcl.mcl_body_you_can_find_more_details_at, lang),
                        url_paths.web_status_pages_internal)
    return subject, notice


def instance_status_update_app_content(lang, org_incident_id, incident_title, status_update):
    '''
    Gets the content of the push notification that should be sent out to let
    status dashboard subscribers know about impacted business services.
    :param lang: content language
    :param org_incident_id: (int) ID of the instance
    :param incident_title: (str) the title of the incident
    :param status_update: (str) the status update
    :return: (tuple) -> email subject, email body
    '''
    inc_title = incident_title if len(incident_title) <= 40 else incident_title[:37] + '...'
    notice_title = _lt.get_label(lbl.ttl_new_update, lang) + ' | #' + str(org_incident_id) + ' ' + inc_title
    return notice_title, status_update
