# By: Riasat Ullah
# This file contains function that generate content for business impact notices.

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


def business_impact_update_app_content(lang, business_service_name, bus_event, incidents, update_after_time=None):
    '''
    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 business_service_name: name of the business service that has been impacted
    :param bus_event: adhoc way to identify the status of the business service (not related to instance event)
    :param incidents: (list) of incidents
    :param update_after_time: (timestamp) if provided only the status updates after the given time will be sent
    :return: (tuple) -> title, notice
    '''
    notice_body = '{0}: {1}'.format(_lt.get_label(lbl.ttl_incidents, lang), str(len(incidents)))
    if bus_event == constants.trigger_event:
        bus_status = _lt.get_label(lbl.det_impacted, lang)
        if len(incidents) == 1:
            notice_body = incidents[0][var_names.task_title]
    elif bus_event == constants.resolve_event:
        bus_status = _lt.get_label(lbl.sts_operational, lang)
        notice_body = ''
    else:
        bus_status = _lt.get_label(lbl.det_status_update, lang)

    upd_list = []
    for inc in incidents:
        inst_updates = inc[var_names.status_update]
        if inst_updates is not None:
            for upd in inst_updates:
                if upd[var_names.timestamp] > update_after_time:
                    upd_list.append(upd[var_names.status_update])

    if len(upd_list) > 0:
        notice_body = '; '.join(upd_list)[:100]

    notice_title = '{0} {1}'.format(business_service_name, bus_status)
    return notice_title, notice_body


def business_impact_update_email_content(lang, business_service_name, bus_event, impact_start, impact_end,
                                         timezone, incidents):
    '''
    Gets the content of the email that should be sent out to let
    status dashboard subscribers know about impacted business services.
    :param lang: content language
    :param business_service_name: name of the business service that has been impacted
    :param bus_event: adhoc way to identify the status of the business service (not related to instance event)
    :param impact_start: time when the impact started
    :param impact_end: time when the impact ended
    :param timezone: timezone the timestamps should be converted to
    :param incidents: (list) of incidents
    :return: (tuple) -> email subject, email body
    '''
    if bus_event == constants.trigger_event:
        bus_status = _lt.get_label(lbl.det_impacted, lang)
    elif bus_event == constants.resolve_event:
        bus_status = _lt.get_label(lbl.sts_operational, lang)
    else:
        bus_status = _lt.get_label(lbl.det_status_update, lang)

    imp_time = times.utc_to_region_time(impact_start, timezone).strftime(constants.timestamp_words_format)
    imp_end = times.utc_to_region_time(impact_end, timezone).strftime(constants.timestamp_words_format)
    inc_body = ''
    for inc in incidents:
        org_inst_id = inc[var_names.organization_instance_id]
        inst_title = inc[var_names.task_title]
        inst_time = times.utc_to_region_time(inc[var_names.instance_timestamp], timezone).strftime(
            constants.timestamp_words_format)
        inst_service = inc[var_names.service_name]
        urgency = helpers.get_urgency_map(inc[var_names.urgency_level], lang)
        inst_updates = inc[var_names.status_update]

        # Put the status updates together.
        upd_body = ''
        if inst_updates is not None:
            upd_body += '''<p style="text-decoration: underline;">{0}</p>'''.format(
                _lt.get_label(lbl.ttl_status_updates, lang))
            for upd in inst_updates:
                upd_body += "<p> {0} <br/> {1} <br/><br/> </p>".format(
                    times.utc_to_region_time(upd[var_names.timestamp], timezone).strftime(
                        constants.timestamp_words_format),
                    upd[var_names.status_update]
                )

        # Prepare the content of each incident.
        inc_body += '''
                   <p style="padding-bottom: 5px;">
                    {0} #{1}: {2}
                    <br/>

                    {3}: {4} <br/>
                    {5}: {6} <br/>
                    {7}: {8} <br/>

                    <br/> {9}
                   </p>
                   '''.format(_lt.get_label(lbl.ttl_incident, lang), org_inst_id, inst_title,
                              _lt.get_label(lbl.ttl_created_on, lang), inst_time,
                              _lt.get_label(lbl.det_service, lang), inst_service,
                              _lt.get_label(lbl.det_urgency, lang), urgency,
                              upd_body)

    subject = '{0}: {1}'.format(business_service_name, bus_status)
    notice = '''
             <p> <strong>{0}: {1}</strong> </p>
             <p> {2}: {0} </p>
             <p> {3}: {4} </p>
             <p> {5}: {6} </p>

             <p style="font-weight: 600;"> {7} </p>
             {8}

             <p>
                {9}: <a href="{10}">{10}</a>
             </p>
             '''.format(business_service_name, bus_status, _lt.get_label(lbl.ttl_business_service, lang),
                        _lt.get_label(lbl.det_impacted, lang), imp_time,
                        _lt.get_label(lbl.ttl_timezone, lang), timezone,
                        _lt.get_label(lbl.ttl_incidents, lang),
                        inc_body,
                        _lt.get_label(mcl.mcl_body_you_can_find_more_details_at, lang),
                        url_paths.web_status_pages_internal)
    return subject, notice
