# By: Riasat Ullah
# This file contains all constants and functions related to the PRTG integration.

from utils import constants

# PRTG variables
var_device = 'device'
var_device_id = 'deviceid'
var_group = 'group'
var_history = 'history'
var_host = 'host'
var_last_status = 'laststatus'
var_message = 'message'
var_priority = 'priority'
var_probe_id = 'probeid'
var_program_name = 'programname'
var_sensor = 'sensor'
var_sensor_id = 'sensorid'
var_server = 'server'
var_short_name = 'shortname'
var_site_name = 'sitename'
var_status = 'status'

# possible status text words - in small caps
status_word_change = 'change'
status_word_changed = 'changed'
status_word_down = 'down'
status_word_ended = 'ended'
status_word_not = 'not'
status_word_reached = 'reached'
status_word_unusual = 'unusual'
status_word_up = 'up'
status_word_warning = 'warning'

# urgency mapping
priority_mapping = {
    '*': constants.minor_urgency,
    '**': constants.low_urgency,
    '***': constants.medium_urgency,
    '****': constants.high_urgency,
    '*****': constants.critical_urgency
}


# internal event type of PRTG alerts
prtg_event_resolve = 'Resolve'
prtg_event_trigger = 'Trigger'
prtg_event_unknown = 'Unknown'


def get_alert_event(prtg_status):
    '''
    From the description provided in the PRTG alert status identify whether it is a trigger or resolution event.
    :param prtg_status: (str) text provided in the status of the alert
    :return: (str) Trigger | Resolve
    '''
    prtg_status = prtg_status.lower().split()

    if (status_word_down in prtg_status and status_word_ended not in prtg_status) or\
        (status_word_reached in prtg_status and status_word_not not in prtg_status) or \
        status_word_warning in prtg_status or status_word_change in prtg_status or\
            status_word_changed in prtg_status:
        return prtg_event_trigger
    elif status_word_up in prtg_status or (status_word_down in prtg_status and status_word_ended in prtg_status) or\
            (status_word_reached in prtg_status and status_word_not in prtg_status):
        return prtg_event_resolve
    else:
        return prtg_event_unknown
