# By: Riasat Ullah
# This file contains variables specifically for Zendesk.

from constants import api_paths, var_names
from taskcallweb import settings
from urllib.parse import urlencode
from utils import helpers, s3


# zendesk s3 credential file variables
zendesk_s3_bucket = 'taskcall-prod-data'
zendesk_s3_key = 'credentials/zendesk_credentials.json'

# url paths
zendesk_oauth_token_path = 'https://{0}.zendesk.com/oauth/tokens'
zendesk_tc_redirect_uri = settings.REDIRECT_BASE + '/configurations/services/integrations/zendesk/authorize'
zendesk_ticket_path_format = 'https://{0}.zendesk.com/agent/tickets/{1}'

# internal variables for Zendesk
str_undefined = 'undefined'

# Zendesk variables
str_zendesk_access_token = 'zendesk_access_token'
str_zendesk_display_name = 'zendesk_display_name'
str_zendesk_origin = 'zendesk_origin'
str_zendesk_refresh_token = 'zendesk_refresh_token'
str_zendesk_ticket_id = 'zendesk_ticket_id'

# Zendesk ticket types
incident_ticket_type = 'incident'
problem_ticket_type = 'problem'
question_ticket_type = 'question'
task_ticket_type = 'task'

# TaskCall allowed url calls
cmd_acknowledge = 'acknowledge'
cmd_add_responders = 'add-responders'
cmd_create_incident = 'create-incident'
cmd_link = 'link'
cmd_linked_incidents = 'linked-incidents'
cmd_list_policies = 'list-policies'
cmd_list_response_sets = 'list-response-sets'
cmd_list_services = 'list-services'
cmd_notate = 'notate'
cmd_reassign = 'reassign'
cmd_resolve = 'resolve'
cmd_run_response_set = 'run-response-set'
cmd_status_dashboard = 'status-dashboard'
cmd_subscribe = 'subscribe'
cmd_unacknowledge = 'unacknowledge'
cmd_unlink = 'unlink'
cmd_unsubscribe = 'unsubscribe'
cmd_update_status = 'update-status'
cmd_view_incidents = 'view-incidents'

# error messages
err_unknown_action = 'The requested action was either not found or is not allowed'


zendesk_allowed_actions = [cmd_acknowledge, cmd_add_responders, cmd_status_dashboard, cmd_link,
                           cmd_linked_incidents, cmd_list_policies, cmd_list_response_sets, cmd_list_services,
                           cmd_notate, cmd_reassign, cmd_resolve, cmd_run_response_set, cmd_subscribe,
                           cmd_unacknowledge, cmd_unlink, cmd_unsubscribe, cmd_update_status, cmd_view_incidents]


def get_zendesk_credentials():
    '''
    Get the credentials needed for handling and making API calls for Zendesk.
    :return: (dict) of credentials
    '''
    creds = s3.read_json(zendesk_s3_bucket, zendesk_s3_key)
    return creds


def get_zendesk_oauth_path(subdomain, state):
    '''
    Format the zendesk oauth path for a given account.
    :param subdomain: subdomain of the Zendesk account
    :param state: the service ref ID that will be sent with the request as a state to check back with on the response
    :return: (str) oauth path
    '''
    zd_creds = get_zendesk_credentials()
    oauth_root = 'https://{0}.zendesk.com/oauth/authorizations/new?'.format(subdomain)
    params = {
        'response_type': 'code',
        'redirect_uri': zendesk_tc_redirect_uri,
        'client_id': zd_creds[var_names.client_id],
        'scope': 'read write',
        'state': state
    }
    oauth_path = oauth_root + urlencode(params)
    return oauth_path


def get_token_details_for_zendesk(request):
    '''
    Get the details of a TaskCall issued token for Zendesk.
    :param request: Http request
    :return: JSON response -> (tuple --> access_token, expiry, display name)
    '''
    access_token, token_expiry, display_name = None, None, None
    if str_zendesk_access_token in request.COOKIES:
        access_token = request.COOKIES.get(str_zendesk_access_token)
    elif str_zendesk_refresh_token in request.COOKIES:
        refresh_token = request.COOKIES.get(str_zendesk_refresh_token)
        body = {var_names.refresh_token: refresh_token}

        status, output = helpers.send_post_request(api_paths.integrations_zendesk_token_refresh, body)
        if status == 200:
            access_token = output[var_names.access_token]
            token_expiry = output[var_names.expires_on]

    if str_zendesk_display_name in request.COOKIES:
        display_name = request.COOKIES.get(str_zendesk_display_name)

    return access_token, token_expiry, display_name
