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

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


# zoom s3 credential file variables
zoom_s3_bucket = 'taskcall-prod-data'
zoom_s3_key = 'credentials/zoom_credentials.json'

# zoom url paths
zoom_token_retrieval_path = 'https://zoom.us/oauth/token'
zoom_user_path = 'https://api.zoom.us/v2/users/me'


def get_zoom_credentials():
    '''
    Get the credentials needed for handling and making API calls to Zoom.
    :return: (dict) of credentials
    '''
    creds = s3.read_json(zoom_s3_bucket, zoom_s3_key)
    if settings.TEST_SERVER:
        return creds['test']
    else:
        return creds['prod']


def get_zoom_to_tc_redirect_uri():
    '''
    Get the TaskCall redirect uri to send to Zoom.
    :return: (str) redirect uri
    '''
    ref_dict = static_vars.regional_urls
    if settings.TEST_SERVER:
        ref_dict = static_vars.regional_test_server_urls
    path = ref_dict[settings.REGION][var_names.redirect_base] +\
        '/configurations/conference-bridges/integrations/zoom/authorize'
    return path


def get_zoom_oauth_path(state):
    '''
    Format the Zoom oauth path for a given account.
    :param state: unique code to send in the url for verification
    :return: (str) oauth path
    '''
    zoom_creds = get_zoom_credentials()
    oauth_root = 'https://zoom.us/oauth/authorize?'
    params = {
        'redirect_uri': get_zoom_to_tc_redirect_uri(),
        'client_id': zoom_creds[var_names.client_id],
        'response_type': 'code',
        'state': state
    }
    oauth_path = oauth_root + urlencode(params)
    return oauth_path
