import json

from datetime import datetime, timedelta, timezone

from laceworksdk import LaceworkClient
import demistomock as demisto
from CommonServerPython import *

# disable insecure warnings
requests.packages.urllib3.disable_warnings()

handle_proxy()

''' GLOBAL VARS '''
LACEWORK_ACCOUNT = demisto.params().get('lacework_account')
LACEWORK_SUBACCOUNT = demisto.params().get('lacework_subaccount', None)
LACEWORK_API_KEY = demisto.params()['lacework_api_key']
LACEWORK_API_SECRET = demisto.params()['lacework_api_secret']
LACEWORK_EVENT_SEVERITY = demisto.params()['lacework_event_severity']
LACEWORK_EVENT_HISTORY_DAYS = demisto.params()['lacework_event_history']

try:
    if LACEWORK_SUBACCOUNT:
        lacework_client = LaceworkClient(account=LACEWORK_ACCOUNT,
                                         subaccount=LACEWORK_SUBACCOUNT,
                                         api_key=LACEWORK_API_KEY,
                                         api_secret=LACEWORK_API_SECRET)
    else:
        lacework_client = LaceworkClient(account=LACEWORK_ACCOUNT,
                                         api_key=LACEWORK_API_KEY,
                                         api_secret=LACEWORK_API_SECRET)
except Exception as e:
    demisto.results("Lacework API authentication failed. Please validate Account, \
                    Sub-Account, API Key, and API Secret. Error: {}".format(e))

''' HELPER FUNCTIONS '''


def get_event_severity_threshold():
    """
    Convert the Event Severity string to the appropriate integer
    """

    if LACEWORK_EVENT_SEVERITY == 'critical':
        return 1
    elif LACEWORK_EVENT_SEVERITY == 'high':
        return 2
    elif LACEWORK_EVENT_SEVERITY == 'medium':
        return 3
    elif LACEWORK_EVENT_SEVERITY == 'low':
        return 4
    elif LACEWORK_EVENT_SEVERITY == 'informational':
        return 5
    else:
        raise Exception('Invalid Event Severity Threshold was defined.')


def create_entry(title, data, ec, human_readable=None):
    """
    Simplify the output/contents
    """

    if human_readable is None:
        human_readable = data

    return {
        'ContentsFormat': formats['json'],
        'Type': entryTypes['note'],
        'Contents': data,