# By: Riasat Ullah
# This file contains all variables and integrations for the Slack integration.

from constants import static_vars, var_names
from taskcallweb import settings
from urllib.parse import urlencode


# slack s3 credential file variables
slack_s3_bucket = 'taskcall-prod-data'
slack_s3_key = 'credentials/slack_credentials.json'

# slack url paths
slack_access_token_path = 'https://slack.com/api/oauth.v2.access'

# string variable names
str_slack_client_id = 'client_id'
str_slack_client_secret = 'client_secret'


def get_slack_tc_redirect_path():
    '''
    Get the path in TaskCall a user should be redirected to from TaskCall.
    :return: (str) url path
    '''
    rdr_path = settings.REDIRECT_BASE + '/configurations/services/integrations/slack/authorize'
    return rdr_path


def get_slack_oauth_path(req_state):
    '''
    Get the path in Slack to send a user to from TaskCall to initiate the OAuth process.
    Future perm: incoming-webhook,commands,chat:write,channels:manage,channels:read,groups:write,im:write,mpim:write
    :params req_state: service ref ID to use as the request state
    :return: (str) url path
    '''
    oauth_base = 'https://slack.com/oauth/v2/authorize?'
    parameters = {
        'client_id': '1835325659411.1828359674758',
        'scope': 'incoming-webhook,commands',
        'redirect_uri': get_slack_tc_redirect_path(),
        'state': req_state,
        'user_scope': ''
    }
    return oauth_base + urlencode(parameters)


def get_web_path_us_tc_slack_authorization(slack_code, taskcall_state):
    '''
    Get the web url path in the US region to redirect a slack authorization request to
    from the EU region when the account is situated in the US.
    :return: (str) url path
    '''
    ref_dict = static_vars.regional_urls
    if settings.TEST_SERVER:
        ref_dict = static_vars.regional_test_server_urls
    path = ref_dict[static_vars.aws_us_ohio][var_names.redirect_base] + \
        '/configurations/services/integrations/slack/authorize?code={0}&state={1}'.format(slack_code, taskcall_state)
    return path
