# By: Riasat Ullah
# This file contains functions that will generate the context of components.

from constants import component_names as cnm, label_names as lbl, var_names, static_vars
from translators import label_translator as lt


component_maps = {
    cnm.dis_com_incidents: (lbl.ttl_incidents, '/incidents'),
    cnm.dis_com_alerts: (lbl.ttl_alerts, '/alerts'),
    cnm.dis_com_routines: (lbl.ttl_routines, '/configurations/routines'),
    cnm.dis_com_escalation_policies: (lbl.ttl_escalation_policies, '/configurations/escalation-policies'),
    cnm.dis_com_services: (lbl.ttl_services, '/configurations/services'),
    cnm.dis_com_business_services: (lbl.ttl_business_services, '/configurations/business-services'),
    cnm.dis_com_dependency_graph: (lbl.ttl_dependency_graph, '/configurations/services/dependencies'),
    cnm.dis_com_recent_changes: (lbl.ttl_recent_changes, '/configurations/recent-changes'),
    cnm.dis_com_conference_bridges: (lbl.ttl_conference_bridges, '/configurations/conference-bridges'),
    cnm.dis_com_users: (lbl.ttl_users, '/configurations/users'),
    cnm.dis_com_teams: (lbl.ttl_teams, '/configurations/teams'),
    cnm.dis_com_api_access: (lbl.ttl_api_access, '/configurations/api-access'),
    cnm.dis_com_tags: (lbl.ttl_tags, '/configurations/tags'),
    cnm.dis_com_live_call_routing: (lbl.ttl_live_call_routing, '/configurations/live-call-routing'),
    cnm.dis_com_organization: (lbl.ttl_organization, '/organization'),
    cnm.dis_com_conditional_routing: (lbl.ttl_conditional_routing, '/automation/conditional-routing'),
    cnm.dis_com_workflows: (lbl.ttl_incident_workflows, '/automation/incident-workflows'),
    cnm.dis_com_heartbeats: (lbl.ttl_heartbeats, '/automation/heartbeats'),
    cnm.dis_com_notification_analytics: (lbl.ttl_notifications, '/analytics/notifications'),
    cnm.dis_com_incident_analytics: (lbl.ttl_incidents, '/analytics/incidents'),
    cnm.dis_com_user_analytics: (lbl.ttl_users, '/analytics/users'),
    cnm.dis_com_team_analytics: (lbl.ttl_teams, '/analytics/teams'),
    cnm.dis_com_service_analytics: (lbl.ttl_services, '/analytics/services'),
    cnm.dis_com_business_impact_analytics: (lbl.ttl_business_impact, '/analytics/business-impact'),
    cnm.dis_com_live_call_analytics: (lbl.ttl_live_calls, '/analytics/live-calls'),
    cnm.dis_com_postmortem: (lbl.ttl_postmortem, '/analytics/postmortem'),
    cnm.dis_com_operations_console: (lbl.ttl_visibility_console, '/advanced/visibility-console'),
    cnm.dis_com_status_dashboard: (lbl.ttl_internal_status_pages, '/itsm/internal-status-pages'),
    cnm.dis_com_status_pages: (lbl.ttl_external_status_pages, '/itsm/external-status-pages'),
    cnm.dis_com_itsm_groups: (lbl.ttl_groups, '/itsm/groups'),
    cnm.dis_com_itsm_people: (lbl.ttl_people, '/itsm/people'),
    cnm.dis_com_itsm_sso: (lbl.ttl_external_sso, '/itsm/external-sso')
}

configuration_components = [cnm.dis_com_routines, cnm.dis_com_escalation_policies,
                            cnm.dis_com_services, cnm.dis_com_business_services,
                            cnm.dis_com_dependency_graph, cnm.dis_com_recent_changes,
                            cnm.dis_com_conference_bridges, cnm.dis_com_users,
                            cnm.dis_com_teams, cnm.dis_com_api_access,
                            cnm.dis_com_tags, cnm.dis_com_live_call_routing]

automation_components = [cnm.dis_com_conditional_routing, cnm.dis_com_workflows, cnm.dis_com_heartbeats]

analytics_components = [cnm.dis_com_notification_analytics, cnm.dis_com_incident_analytics, cnm.dis_com_user_analytics,
                        cnm.dis_com_team_analytics, cnm.dis_com_service_analytics,
                        cnm.dis_com_business_impact_analytics, cnm.dis_com_live_call_analytics,
                        cnm.dis_com_postmortem]

itsm_components = [cnm.dis_com_status_dashboard, cnm.dis_com_status_pages, cnm.dis_com_itsm_sso,
                   cnm.dis_com_itsm_people, cnm.dis_com_itsm_groups]

owner_components = [cnm.dis_com_organization]

language_maps = {
    static_vars.lang_en: lbl.opt_english,
    static_vars.lang_es: lbl.opt_spanish
}


def get_nav_bar_content(allowed_components, context):
    '''
    Gets the content to show on the flexible part of the dashboard navigation bar.
    :param allowed_components: (list) of names of the components that can be displayed (as received from the rest API)
    :param context: (dict) translated context
    :return: (dict) navigation bar components context
    '''
    nav_content = dict()
    conf_content = []
    atma_content = []
    anlt_content = []
    itsm_content = []
    owner_content = []

    for comp_name in allowed_components:
        if comp_name in component_maps:
            this_map = component_maps[comp_name]
            tmpl_info_to_send = {var_names.name: context[this_map[0]], var_names.url: this_map[1]}

            if comp_name == cnm.dis_com_incidents:
                nav_content[cnm.nav_incidents] = [tmpl_info_to_send]
            elif comp_name == cnm.dis_com_alerts:
                nav_content[cnm.nav_alerts] = [tmpl_info_to_send]
            elif comp_name in configuration_components:
                conf_content.append(tmpl_info_to_send)
            elif comp_name in automation_components:
                atma_content.append(tmpl_info_to_send)
            elif comp_name in analytics_components:
                anlt_content.append(tmpl_info_to_send)
            elif comp_name in itsm_components:
                itsm_content.append(tmpl_info_to_send)
            elif comp_name in owner_components:
                owner_content.append(tmpl_info_to_send)

    if len(conf_content) > 0:
        nav_content[cnm.nav_configurations] = conf_content
    if len(atma_content) > 0:
        nav_content[cnm.nav_automation] = atma_content
    if len(anlt_content) > 0:
        nav_content[cnm.nav_analytics] = anlt_content
    if len(itsm_content) > 0:
        nav_content[cnm.nav_itsm] = itsm_content
    if len(owner_content) > 0:
        nav_content[cnm.nav_owner] = owner_content

    return nav_content


def get_dashboard_topbar_context(lang, allowed_components):
    '''
    Gets the template context needed to show the dashboard navigation bar correctly. Some labels that are generally
    used across many pages are also included in the list.
    :param lang: code of the language the context should be produced in
    :param allowed_components: (list) of names of the components that can be displayed (as received from the rest API)
    :return: (dict) dashboard top bar context
    '''
    page_labels = [lbl.ttl_advanced, lbl.ttl_alerts, lbl.ttl_analytics, lbl.ttl_api_access, lbl.ttl_automation,
                   lbl.ttl_business_impact, lbl.ttl_business_services, lbl.ttl_conditional_routing,
                   lbl.ttl_conference_bridges, lbl.ttl_configurations, lbl.ttl_dependency_graph,
                   lbl.ttl_escalation_policies, lbl.ttl_external_sso, lbl.ttl_external_status_pages,
                   lbl.ttl_groups, lbl.ttl_heartbeats, lbl.ttl_incidents, lbl.ttl_incident_workflows,
                   lbl.ttl_internal_status_pages, lbl.ttl_itsm, lbl.ttl_live_calls, lbl.ttl_live_call_routing,
                   lbl.ttl_log_out, lbl.ttl_my_on_call_shifts, lbl.ttl_my_profile, lbl.ttl_notifications,
                   lbl.ttl_people, lbl.ttl_postmortem, lbl.ttl_organization, lbl.ttl_recent_changes,
                   lbl.ttl_routines, lbl.ttl_services, lbl.ttl_support, lbl.ttl_tags, lbl.ttl_teams,
                   lbl.ttl_users, lbl.ttl_visibility_console]
    common_labels = [lbl.det_items_per_page, lbl.det_showing]
    page_context = lt.get_context(page_labels + common_labels, lang)
    page_context[cnm.nav_content] = get_nav_bar_content(allowed_components, page_context)
    return page_context


def get_months_days_context(lang):
    '''
    Gets the context for the months and days variables.
    :param lang: code of the language the context should be provided in
    :return: (dict) months and days context
    '''
    month_labels = [lbl.mth_jan, lbl.mth_feb, lbl.mth_mar, lbl.mth_apr, lbl.mth_may, lbl.mth_jun, lbl.mth_jul,
                    lbl.mth_aug, lbl.mth_sep, lbl.mth_oct, lbl.mth_nov, lbl.mth_dec]
    day_labels = [lbl.day_mon, lbl.day_tue, lbl.day_wed, lbl.day_thu, lbl.day_fri, lbl.day_sat, lbl.day_sun]
    md_context = lt.get_context(month_labels + day_labels, lang)
    return md_context


def get_event_types_context(lang):
    '''
    Gets the context for the incident event type variables.
    :param lang: code of the language the context should be provided in
    :return: (dict) event types context
    '''
    event_labels = [lbl.emd_app, lbl.emd_call, lbl.emd_email, lbl.emd_incidents_api, lbl.emd_integrations_api,
                    lbl.emd_internal, lbl.emd_live_call_routing, lbl.emd_rundeck, lbl.emd_text, lbl.emd_web,
                    lbl.evn_acknowledge, lbl.evn_add_conference_bridge, lbl.evn_add_impacted_business_service,
                    lbl.evn_add_responders, lbl.evn_add_subscribers, lbl.evn_call_answered, lbl.evn_call_ended,
                    lbl.evn_call_forwarding, lbl.evn_call_outgoing, lbl.evn_call_voicemail_prompt,
                    lbl.evn_custom_action, lbl.evn_dispatch, lbl.evn_edit_title, lbl.evn_escalate, lbl.evn_merge,
                    lbl.evn_notate, lbl.evn_reassign, lbl.evn_resolve, lbl.evn_run_workflow, lbl.evn_send_chat_message,
                    lbl.evn_send_external_email, lbl.evn_send_external_sms, lbl.evn_snooze, lbl.evn_status_update,
                    lbl.evn_trigger, lbl.evn_un_acknowledge, lbl.evn_un_merge, lbl.evn_urgency_amendment]
    ev_context = lt.get_context(event_labels, lang)
    return ev_context


def get_sectors_context(lang):
    '''
    Gets the context for the sectors that an organization can be from.
    :param lang: code of the language the context should be provided in
    :return: (dict) sector names context
    '''
    sector_labels = [lbl.sct_agriculture, lbl.sct_banking, lbl.sct_consulting, lbl.sct_construction, lbl.sct_education,
                     lbl.sct_engineering, lbl.sct_entertainment, lbl.sct_financial_services, lbl.sct_food_processing,
                     lbl.sct_government, lbl.sct_health_care, lbl.sct_hospitality, lbl.sct_insurance,
                     lbl.sct_information_technology, lbl.sct_legal_services, lbl.sct_manufacturing,
                     lbl.sct_pharmaceuticals, lbl.sct_real_estate, lbl.sct_retails_sales, lbl.sct_telecommunication,
                     lbl.sct_transportation, lbl.sct_others]
    sc_context = lt.get_context(sector_labels, lang)
    return sc_context


def get_languages_content(context):
    '''
    Gets the user language options to show when selecting a language.
    :param context: (dict) translated context
    :return: (dict) languages dict content
    '''
    lang_content = []
    for item in sorted(list(language_maps.keys())):
        lang_content.append({var_names.name: context[language_maps[item]], var_names.field_value: item})
    return lang_content


def get_group_types_context(lang):
    '''
    Gets the context for the external group types.
    :param lang: code of the language the context should be provided in
    :return: (dict) group types context
    '''
    group_type_labels = [lbl.opt_board, lbl.opt_client, lbl.opt_committee, lbl.opt_constituents, lbl.opt_department,
                         lbl.opt_partner, lbl.opt_reseller, lbl.opt_supplier, lbl.opt_vendor]
    grp_context = lt.get_context(group_type_labels, lang)
    return grp_context
