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

from constants import label_names as lbl
from context_manager import components_context, incidents_context, resources_context
from translators import label_translator as lt
from constants import var_names


def get_conditional_routing_list_context(lang, allowed_components):
    '''
    Context for the conditional routing list page.
    '''
    top_bar_context = components_context.get_dashboard_topbar_context(lang, allowed_components)

    page_labels = [lbl.det_disabled, lbl.det_video_learn_conditional_routing,
                   lbl.ins_delete, lbl.ins_disable, lbl.ins_edit, lbl.ins_enable, lbl.ins_no, lbl.ins_yes,
                   lbl.ins_view, lbl.res_resources, lbl.qqs_are_you_sure_you_want_to_delete_this_routing_rule,
                   lbl.ttl_conditional_routing, lbl.ttl_new_rule, lbl.ttl_routing_rule]

    res_labels = [lbl.res_conditional_routing, lbl.res_how_to_trigger_an_incident, lbl.res_notifications_mechanism,
                  lbl.res_actions_you_can_perform]
    page_labels += res_labels

    page_descriptions = [lbl.desc_component_empty, lbl.desc_conditional_routing]
    final_context = {**top_bar_context, **lt.get_context(page_labels + page_descriptions, lang)}
    final_context[var_names.resource_items] = resources_context.get_resources_content(res_labels, final_context)
    return final_context


def get_conditional_routing_details_context(lang, allowed_components):
    '''
    Context for the conditional routing details page.
    '''
    top_bar_context = components_context.get_dashboard_topbar_context(lang, allowed_components)
    months_days_context = components_context.get_months_days_context(lang)

    page_labels = [lbl.det_add_a_note, lbl.det_add_tag, lbl.det_alert, lbl.det_alert_as_normal,
                   lbl.det_allow_rule_to_be_combined_with_others, lbl.det_always, lbl.det_change_urgency_level_to,
                   lbl.det_cond_route_view_past_payload, lbl.det_custom, lbl.det_description,
                   lbl.det_do_not_alert_at_all, lbl.det_group_alerts_as_one_for_occurrences_within, lbl.det_period,
                   lbl.det_resolve_alert, lbl.det_route_alert_to_another_policy, lbl.det_route_alert_to_another_service,
                   lbl.det_rule_name, lbl.det_service, lbl.det_set_dedup_key_to, lbl.det_suppress_alerts, lbl.det_time,
                   lbl.det_to, lbl.det_update_incident_description_to, lbl.det_update_incident_title_to,
                   lbl.det_urgency, lbl.dsm_after, lbl.dsm_contains, lbl.dsm_days, lbl.dsm_equals, lbl.dsm_exists,
                   lbl.dsm_field_name, lbl.dsm_hours, lbl.dsm_matches, lbl.dsm_minutes,
                   lbl.dsm_not_contains, lbl.dsm_not_equals, lbl.dsm_not_exists, lbl.dsm_not_matches,
                   lbl.dsm_occurrences_within, lbl.dsm_pending_for, lbl.dsm_up_to, lbl.dsm_value,
                   lbl.err_cond_route_actions, lbl.err_cond_route_days, lbl.err_cond_route_name,
                   lbl.err_cond_route_rule_application, lbl.err_cond_route_rule_min, lbl.err_incident_id_invalid,
                   lbl.err_time_end_before_start, lbl.ins_cancel, lbl.ins_delete, lbl.ins_disable, lbl.ins_enable,
                   lbl.ins_load, lbl.ins_save, lbl.qqs_how_many_of_these_conditions_must_apply,
                   lbl.qqs_what_actions_should_be_taken, lbl.qqs_when_should_the_rule_apply,
                   lbl.qqs_which_conditions_must_be_met, lbl.ttl_add_rule, lbl.ttl_conditional_routing,
                   lbl.ttl_delete_routing, lbl.ttl_disable_routing, lbl.ttl_edit_routing, lbl.ttl_email_from,
                   lbl.ttl_email_subject, lbl.ttl_email_to, lbl.ttl_enable_routing, lbl.ttl_incidents,
                   lbl.ttl_new_routing_rule, lbl.ttl_payload_field, lbl.ttl_resolve, lbl.ttl_timezone]

    option_labels = [lbl.opt_critical, lbl.opt_high, lbl.opt_medium, lbl.opt_low, lbl.opt_minor]
    page_labels = page_labels + option_labels

    final_context = {**top_bar_context, **months_days_context, **lt.get_context(page_labels, lang)}
    return final_context


def get_incident_workflow_list_context(lang, allowed_components):
    '''
    Context for the workflow list page.
    '''
    top_bar_context = components_context.get_dashboard_topbar_context(lang, allowed_components)
    months_days_context = components_context.get_months_days_context(lang)

    page_labels = [lbl.det_add_new_responders_to_incident, lbl.det_after_incident_is_acknowledged,
                   lbl.det_after_incident_is_created, lbl.det_after_note_is_added, lbl.det_after_urgency_is_amended,
                   lbl.det_all_admins, lbl.det_all_incident_responders, lbl.det_automatically, lbl.det_conditional,
                   lbl.det_disabled, lbl.det_edit_permission, lbl.det_enabled, lbl.det_last_run_on, lbl.det_manually,
                   lbl.det_possible_actions, lbl.det_post_incident_status_update, lbl.det_run_permission,
                   lbl.det_subscribe_users_and_teams_to_incident, lbl.det_set_conference_bridge_details_for_incident,
                   lbl.det_triggers, lbl.det_when_incident_is_resolved, lbl.ins_edit, lbl.ins_delete, lbl.ins_no,
                   lbl.ins_yes, lbl.qqs_are_you_sure_you_want_to_delete_this_incident_workflow, lbl.res_resources,
                   lbl.ttl_actions, lbl.ttl_incident_workflow, lbl.ttl_incident_workflows, lbl.ttl_more,
                   lbl.ttl_new_incident_workflow, lbl.ttl_permissions]

    action_type_labels = [lbl.det_add_business_impact, lbl.det_reassign_to, lbl.det_resolve_incident,
                          lbl.det_run_custom_action, lbl.det_send_chat_message, lbl.det_send_email, lbl.det_send_sms,
                          lbl.det_subscribe_teams, lbl.det_subscribe_users, lbl.ttl_add_conference_bridge,
                          lbl.ttl_add_note, lbl.ttl_add_responders, lbl.ttl_add_subscribers, lbl.ttl_post_status_update]

    res_labels = [lbl.res_incident_workflows, lbl.res_mobilize_response, lbl.res_stakeholder_communication,
                  lbl.res_setting_up_a_conference_bridge, lbl.res_actions_you_can_perform]

    page_labels += res_labels
    page_descriptions = [lbl.desc_component_empty, lbl.desc_incident_workflows]

    final_context = {**top_bar_context, **months_days_context,
                     **lt.get_context(page_labels + page_descriptions + action_type_labels, lang)}
    final_context[var_names.resource_items] = resources_context.get_resources_content(res_labels, final_context)
    return final_context


def get_incident_workflow_details_context(lang, allowed_components):
    '''
    Context for the incident workflow details page.
    '''
    top_bar_context = components_context.get_dashboard_topbar_context(lang, allowed_components)
    months_days_context = components_context.get_months_days_context(lang)

    page_labels = [lbl.det_add_action, lbl.det_add_condition, lbl.det_add_group_condition,
                   lbl.det_add_if_else_statement, lbl.det_after_incident_is_acknowledged,
                   lbl.det_after_incident_is_created, lbl.det_after_note_is_added, lbl.det_after_urgency_is_amended,
                   lbl.det_all_admins, lbl.det_all_incident_responders, lbl.det_automatically, lbl.det_description,
                   lbl.det_disabled, lbl.det_enabled, lbl.det_last_run_on, lbl.det_leave_empty_if_nothing_otherwise,
                   lbl.det_managers_from_a_specific_team, lbl.det_manually, lbl.det_name,
                   lbl.det_responders_from_a_specific_team, lbl.det_subject, lbl.det_text, lbl.det_to,
                   lbl.det_when_incident_is_resolved, lbl.err_invalid_email, lbl.err_workflow_actions,
                   lbl.err_workflow_conditions, lbl.err_workflow_missing_action, lbl.err_workflow_name,
                   lbl.err_workflow_trigger, lbl.ins_add_workflow, lbl.ins_cancel, lbl.ins_no, lbl.ins_save,
                   lbl.ins_yes, lbl.msg_incident_workflow_apply_when_manual,
                   lbl.qqs_are_you_sure_you_want_to_delete_this_incident_workflow,
                   lbl.qqs_how_should_workflow_trigger, lbl.qqs_who_can_edit_this_workflow,
                   lbl.qqs_who_can_run_this_workflow, lbl.ttl_configurations, lbl.ttl_delete_workflow,
                   lbl.ttl_disable_workflow, lbl.ttl_edit_workflow, lbl.ttl_enable_workflow, lbl.ttl_incident_workflows,
                   lbl.ttl_new_incident_workflow, lbl.ttl_permissions, lbl.ttl_workflow]

    field_type_labels = [lbl.det_previous_action_http_status, lbl.det_previous_action_json,
                         lbl.det_service, lbl.det_status, lbl.det_urgency, lbl.det_workflow_not_run_already,
                         lbl.dsm_field_name, lbl.dsm_value, lbl.ttl_email_from, lbl.ttl_email_to,
                         lbl.ttl_payload_field, lbl.ttl_tags]

    comparator_labels = [lbl.dsm_contains, lbl.dsm_equals, lbl.dsm_exists, lbl.dsm_matches,
                         lbl.dsm_not_contains, lbl.dsm_not_equals, lbl.dsm_not_exists, lbl.dsm_not_matches]

    field_value_labels = [lbl.opt_critical, lbl.opt_high, lbl.opt_medium, lbl.opt_low, lbl.opt_minor,
                          lbl.sts_acknowledged, lbl.sts_open, lbl.sts_resolved]

    action_type_labels = [lbl.det_add_business_impact, lbl.det_reassign_to, lbl.det_resolve_incident,
                          lbl.det_run_custom_action, lbl.det_send_chat_message, lbl.det_send_email, lbl.det_send_sms,
                          lbl.det_subscribe_teams, lbl.det_subscribe_users, lbl.ttl_add_conference_bridge,
                          lbl.ttl_add_note, lbl.ttl_add_responders, lbl.ttl_add_subscribers, lbl.ttl_post_status_update]

    all_labels = page_labels + field_type_labels + comparator_labels + field_value_labels + action_type_labels
    final_context = {**top_bar_context, **months_days_context, **lt.get_context(all_labels, lang)}
    return final_context


def get_heartbeat_list_context(lang, allowed_components):
    '''
    Context for the heartbeats list page.
    '''
    top_bar_context = components_context.get_dashboard_topbar_context(lang, allowed_components)

    page_labels = [lbl.desc_heartbeats, lbl.desc_monitor_empty, lbl.det_all, lbl.det_failing, lbl.det_heartbeat,
                   lbl.det_interval, lbl.det_keywords, lbl.det_passing, lbl.det_service, lbl.det_status,
                   lbl.dsm_last_30_days, lbl.dsm_search, lbl.ins_delete, lbl.ins_disable, lbl.ins_edit,
                   lbl.ins_enable, lbl.ins_no, lbl.ins_yes, lbl.msg_check_deleted, lbl.plc_ago,
                   lbl.qqs_are_you_sure_you_want_to_delete_this_heartbeat, lbl.res_resources, lbl.ttl_health_check,
                   lbl.ttl_copy_ping_endpoint, lbl.ttl_new_heartbeat, lbl.ttl_heartbeats, lbl.ttl_services,
                   lbl.ttl_team]

    res_labels = [lbl.res_heartbeat_monitoring, lbl.res_incident_workflows, lbl.res_mobilize_response,
                  lbl.res_stakeholder_communication, lbl.res_actions_you_can_perform]
    page_labels += res_labels

    page_descriptions = [lbl.desc_heartbeats]
    final_context = {**top_bar_context, **lt.get_context(page_labels + page_descriptions, lang)}
    final_context[var_names.resource_items] = resources_context.get_resources_content(res_labels, final_context)
    return final_context


def get_heartbeat_details_context(lang, allowed_components):
    '''
    Context for the heartbeat details page.
    '''
    top_bar_context = components_context.get_dashboard_topbar_context(lang, allowed_components)
    months_days_context = components_context.get_months_days_context(lang)

    page_labels = [lbl.desc_checks_grace_period, lbl.desc_checks_incident_configure, lbl.desc_checks_time_interval,
                   lbl.desc_heartbeat_ping_email, lbl.desc_heartbeat_ping_url, lbl.det_accept_email_pings_from_senders,
                   lbl.det_accept_http_pings_from_ip_addresses, lbl.det_description, lbl.det_incident_description,
                   lbl.det_interval, lbl.det_name, lbl.det_urgency, lbl.det_service, lbl.det_service_check_for,
                   lbl.dsm_days, lbl.dsm_hours, lbl.dsm_minutes, lbl.dsm_seconds, lbl.err_check_grace_period,
                   lbl.err_check_interval, lbl.err_check_invalid, lbl.err_invalid_email, lbl.err_invalid_ip_address,
                   lbl.ins_cancel, lbl.ins_save, lbl.msg_check_created, lbl.msg_check_edited, lbl.res_resources,
                   lbl.ttl_check_details, lbl.ttl_email, lbl.ttl_email_from, lbl.ttl_grace_period,
                   lbl.ttl_incident_details, lbl.ttl_incident_title, lbl.ttl_ip_allowlist, lbl.ttl_new_heartbeat,
                   lbl.ttl_permissions, lbl.ttl_ping_email, lbl.ttl_ping_type, lbl.ttl_ping_url, lbl.ttl_tags]

    option_labels = [lbl.opt_critical, lbl.opt_high, lbl.opt_medium, lbl.opt_low, lbl.opt_minor]
    res_labels = [lbl.res_heartbeat_monitoring, lbl.res_incident_workflows, lbl.res_mobilize_response,
                  lbl.res_stakeholder_communication, lbl.res_actions_you_can_perform]
    page_labels = page_labels + option_labels + res_labels

    final_context = {**top_bar_context, **months_days_context, **lt.get_context(page_labels, lang)}
    final_context[var_names.resource_items] = resources_context.get_resources_content(res_labels, final_context)
    return final_context


def get_heartbeat_metrics_context(lang, allowed_components):
    '''
    Context for the heartbeat metrics page.
    '''
    top_bar_context = components_context.get_dashboard_topbar_context(lang, allowed_components)
    months_days_context = components_context.get_months_days_context(lang)

    page_labels = [lbl.desc_no_events_recorded, lbl.desc_no_incidents_here, lbl.det_availability, lbl.det_check_failing,
                   lbl.det_check_passing, lbl.det_custom, lbl.det_failed, lbl.det_fails, lbl.det_passed,
                   lbl.det_resolved_on, lbl.det_service, lbl.det_status, lbl.dsm_grace_period, lbl.dsm_last_hour,
                   lbl.dsm_last_3_hours, lbl.dsm_last_24_hours, lbl.err_check_metrics_period, lbl.ins_edit,
                   lbl.plc_check_last_and_next_run, lbl.plc_check_next_run, lbl.ttl_created_on,
                   lbl.ttl_heartbeat_events, lbl.ttl_heartbeats, lbl.ttl_incident, lbl.ttl_incidents,
                   lbl.ttl_ping_email, lbl.ttl_ping_url]

    incident_status_labels = list(incidents_context.incident_alert_status_maps.values())

    final_context = {**top_bar_context, **months_days_context,
                     **lt.get_context(page_labels + incident_status_labels, lang)}
    return final_context
