# By: Riasat Ullah
# This file contains notices for status pages.

from translators import label_translator as _lt
from utils import constants, label_names as lbl, url_paths, var_names


def status_page_pending_post_approval_email_content(lang, page_ref_id, page_name, post_title, post_message,
                                                    event_type, event_ref_id):
    '''
    Gets the content for a pending post approval email.
    :param lang: content language
    :param page_ref_id: (concealed) reference ID of the page
    :param page_name: name of the status page
    :param post_title: title of the post
    :param post_message: message of the post
    :param event_type: type of event (MAINTENANCE, NEW_INCIDENT, STATUS_UPDATE)
    :param event_ref_id: (concealed) reference ID of the pending event
    :return: (tuple) -> email subject, email body
    '''
    if event_type == constants.maintenance:
        post_url = url_paths.web_status_pages_external_pending_maintenance_format.format(page_ref_id, event_ref_id)
    else:
        post_url = url_paths.web_status_pages_external_pending_incident_format.format(page_ref_id, event_ref_id)

    subject = '{0}: {1}'.format(_lt.get_label(lbl.det_pending_post_approval, lang),
                                post_title[:30] + '...' if len(post_title) > 30 else post_title)
    notice = '''
             <p><strong>{0}</strong></p>
             <p>
                {1}: {2} <br/>
                {3}: {4} <br/>
                {5}: {6} <br/>
             </p>
             <p><a href="{7}">{8}</a></p>
             '''.format(_lt.get_label(lbl.det_pending_post_approval, lang),
                        _lt.get_label(lbl.ttl_page_name, lang), page_name,
                        _lt.get_label(lbl.det_title, lang), post_title,
                        _lt.get_label(lbl.det_message, lang), post_message,
                        post_url, _lt.get_label(lbl.ttl_view_post, lang))
    return subject, notice


def status_page_pending_post_approval_notice(lang, page_name, post_title, post_message):
    '''
    Gets the content for a pending post approval push notification.
    :param lang: content language
    :param page_name: name of the status page
    :param post_title: title of the post
    :param post_message: message of the post
    :return: (tuple) -> notification title, notification body
    '''
    notice_title = '{0} - {1}'.format(page_name, _lt.get_label(lbl.det_pending_post_approval, lang))
    body = '{0}: {1}, {2}: {3}'.format(_lt.get_label(lbl.det_title, lang), post_title,
                                       _lt.get_label(lbl.det_message, lang), post_message)
    return notice_title, body


def status_page_subscriber_confirmation_email_content(lang, logo_url, page_name, confirmation_url, cancellation_url):
    '''
    Get the subject and content of the subscription confirmation email.
    :param lang: content language
    :param logo_url: URL of the status page's logo
    :param page_name: name of the status page
    :param confirmation_url: URL from where the subscription can be confirmed
    :param cancellation_url: URL from where the subscription can be cancelled
    :return: (tuple) -> email subject, email body
    '''
    subject = '{0}: {1}'.format(page_name, _lt.get_label(lbl.det_subscription_for_status_updates, lang))
    notice = '''
            <html>
                <body style="background-color: #EDEDED; padding: 5%;">
                    <div style="margin: 20px auto; min-width: 200px; max-width: 500px; border: 1px solid #E0E0E0;
                            border-radius: 5px; display: block; padding: 5% 5%; background-color: #FFFFFF;
                            font-family: sans-serif; font-weight: 500; font-size: 13px; color: #404040;">
                        <div style="clear: right; vertical-align: middle; padding-top: 5px; padding-bottom: 8px;
                            text-align: center;">
                            <img src="{0}" style="max-width: 100px;">
                        </div>
                        <div style="font-size: 13px;">
                            <h1 style="font-size: 16px;"> {1} </h1>
                            <p>{2}.</p>
                            <p>{3}.</p>
                            <br/>
                            <a style="text-decoration: none; background-color: #0D6EFD; padding: 10px 15px; color: white;
                                border-radius: 5px; font-size: 13px;" href="{4}">
                                {5}
                            </a>
                            <br/><br/>
                        </div>
                    </div>
                    <div style="margin: 20px auto; min-width: 200px; max-width: 500px; display: block;
                        font-family: sans-serif; font-weight: 500; font-size: 13px; color: #404040;">
                        <div style="text-align: center;">
                            <p style="color: #9A9A9A; font-size: 11px;"> Powered by TaskCall </p>
                        </div>
                        <p style="color: #9A9A9A; font-size: 10px;">
                            {6}: <a style="text-decoration: none; color: #8A8A8A;" href="{7}">{8}</a>
                        </p>
                    </div>
                </body>
            </html>
             '''.format(logo_url, _lt.get_label(lbl.det_confirm_subscription, lang),
                        _lt.get_label(lbl.plc_subscribed_to_receive_status_updates_from,
                                      lang).replace('{{ page_name }}', page_name),
                        _lt.get_label(lbl.det_subscription_click_to_confirm, lang), confirmation_url,
                        _lt.get_label(lbl.det_confirm_subscription, lang),
                        _lt.get_label(lbl.plc_status_page_notice_unsubscribe_footer, lang).replace(
                            '{{ page_name }}', page_name), cancellation_url, _lt.get_label(lbl.ttl_unsubscribe, lang))

    return subject, notice


def status_page_subscriber_confirmation_sms_content(lang, page_name, confirmation_url):
    '''
    Get the subscription confirmation sms message.
    :param lang: content language
    :param page_name: name of the status page
    :param confirmation_url: URL where the subscription can be confirmed
    :return: (tuple) -> email subject, email body
    '''
    notice = '{0}. {1}: {2}'.format(_lt.get_label(lbl.plc_subscribed_to_receive_status_updates_from,
                                                  lang).replace('{{ page_name }}', page_name),
                                    _lt.get_label(lbl.det_subscription_click_to_confirm, lang),
                                    confirmation_url)
    return notice


def status_page_post_email_content(lang, page_name, page_url, logo_url, subscriber_ref_id, timestamp,
                                   event_type, status, post_title, post_message, impacted_business_services):
    '''
    Get the subject and content of status page post to be emailed to a subscriber.
    :param lang: content language
    :param page_name: name of the status page
    :param page_url: URL of the status page
    :param logo_url: URL of the status page's logo
    :param subscriber_ref_id: reference ID of the subscriber
    :param timestamp: timestamp when the post was made
    :param event_type: type of post
    :param status: status of the post/incident
    :param post_title: title of the post/incident
    :param post_message: details of the post/incident
    :param impacted_business_services: (list of dict) of impacted business services and their corresponding state
    :return: (tuple) -> email subject, email body
    '''
    # Get the label for the type of event.
    evn_type_label = _lt.get_label(lbl.ttl_new_incident, lang)
    if event_type == constants.status_update:
        evn_type_label = _lt.get_label(lbl.det_status_update, lang)
    elif event_type == constants.maintenance:
        evn_type_label = _lt.get_label(lbl.det_maintenance, lang)

    # Get the label for status.
    sts_label = None
    sts_style = 'padding: 4px 8px; border-radius: 4px;'
    if status == constants.completed_state:
        sts_label = lbl.sts_completed
        sts_style += 'background-color: #4CBB17; color: #FFFFFF;'
    elif status == constants.identified_state:
        sts_label = lbl.sts_identified
        sts_style += 'background-color: #FF0000; color: #FFFFFF;'
    elif status == constants.investigating_state:
        sts_label = lbl.sts_investigating
        sts_style += 'background-color: #FCE205; color: #000000;'
    elif status == constants.in_progress_state:
        sts_label = lbl.sts_in_progress
        sts_style += 'background-color: #007D8C; color: #FFFFFF;'
    elif status == constants.monitoring_state:
        sts_label = lbl.sts_monitoring
        sts_style += 'background-color: #FCE205; color: #000000;'
    elif status == constants.resolved_state:
        sts_label = lbl.sts_resolved
        sts_style += 'background-color: #4CBB17; color: #FFFFFF;'
    elif status == constants.scheduled_state:
        sts_label = lbl.sts_scheduled
        sts_style += 'background-color: #007D8C; color: #FFFFFF;'
    elif status == constants.verifying_state:
        sts_label = lbl.sts_verifying
        sts_style += 'background-color: #007D8C; color: #FFFFFF;'

    sts_label = _lt.get_label(sts_label, lang) if sts_label is not None else ''

    # Get the content for impacted services
    imp_div_contents = []
    for item in impacted_business_services:
        bus_sts = item[var_names.status]
        if bus_sts is None or bus_sts == constants.partial_outage_state:
            imp_div_contents.append('<span style="padding: 3px; text-align: center; color: #4CBB17; ' +
                                    'font-size: 20px; font-weight: 700;">&#9888;</span>' +
                                    '<span>' + _lt.get_label(lbl.sts_partial_outage, lang) + '</span>')
        elif item[var_names.status] == constants.outage_state:
            imp_div_contents.append('<span style="padding: 3px; text-align: center; color: #4CBB17; ' +
                                    'font-size: 15px; font-weight: 700;">&#10060;</span>' +
                                    '<span>' + _lt.get_label(lbl.sts_outage, lang) + '</span>')
        else:
            imp_div_contents.append('<span style="padding: 3px; text-align: center; color: #4CBB17; ' +
                                    'font-size: 20px; font-weight: 700;">&#10003;</span>' +
                                    '<span>' + _lt.get_label(lbl.sts_resolved, lang) + '</span>')

    subject = '{0}: {1}'.format(page_name, evn_type_label)
    content = '''
    <html>
        <body style="background-color: #EDEDED; padding: 5%;">
            <div style="margin: 20px auto; min-width: 200px; max-width: 500px; border: 1px solid #E0E0E0;
                    border-radius: 5px; display: block; padding: 3%; background-color: #FFFFFF;
                    font-family: sans-serif; font-weight: 500; font-size: 13px; color: #404040;">
                <div style="clear: right; vertical-align: middle; padding-top: 15px; padding-bottom: 8px;
                    text-align: center;">
                    <img src="{0}" style="max-width: 100px;">
                </div>
                <div style="padding: 4%;">
                    <h1 style="font-size: 16px; text-align: center;"> {1} </h1>
                    <br/>
                    <span style="color: #808080;">{2} <span style="padding: 0px 5px;">|</span> {3}</span>
                    <br/><br/>
                    <span style="{4}"> {5} </span>

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

                    <div>
                        <label style="font-weight: 600;"> {8} </label>
                        <br/>
                        <div style="padding-top: 5px;"> {9} </div>
                    </div>
                    <br/><br/>
                    <a style="text-decoration: none; padding: 8px 0px;" href="{10}"> {11} </a>
                    <br/><br/>
                </div>
            </div>
            <div style="margin: 20px auto; min-width: 200px; max-width: 500px;
                display: block; font-family: sans-serif; font-weight: 500; font-size: 13px; color: #404040;">
                <div style="text-align: center;">
                    <p style="color: #9A9A9A; font-size: 11px;"> Powered by TaskCall </p>
                </div>
                <p style="color: #9A9A9A; font-size: 10px;">
                    {12}: <a style="text-decoration: none; color: #8A8A8A;" href="{13}">{14}</a>
                </p>
            </div>
        </body>
    </html>
             '''.format(logo_url, page_name, evn_type_label, timestamp.strftime(constants.timestamp_words_format),
                        sts_style, sts_label, post_title, post_message, _lt.get_label(lbl.ttl_impacted_services, lang),
                        '<br/>'.join(imp_div_contents), page_url, _lt.get_label(lbl.ttl_view_incidents, lang),
                        _lt.get_label(lbl.plc_status_page_notice_unsubscribe_footer, lang).replace(
                            '{{ page_name }}', page_name),
                        url_paths.live_status_pages_subscription_cancel_format.format(page_url, subscriber_ref_id),
                        _lt.get_label(lbl.ttl_unsubscribe, lang))
    return subject, content
