# By: Riasat Ullah
# This file contains paths and functions to get the file paths of client data storage on s3.

from constants import var_names
from taskcallweb import settings
from utils import file_storage
import random
import re


# client data s3 bucket variables
client_files_url_prefix = 'client/files'
audio_folder = 'audio'
images_folder = 'images'

# file path formats
live_call_routing_ending_audio = 'audio/live-call-routing/{0}/ending'
live_call_routing_greeting_audio = 'audio/live-call-routing/{0}/greeting'
organization_profile_pictures = 'images/profile-pictures/{0}'
status_pages_images = 'images/status-pages/{0}'
user_profile_pictures = 'images/profile-pictures/{0}/{1}'
user_taskcall_avatars = 'images/avatars/{0}'

# TaskCall avatar index name maps. The indices are used on the html and javascript pages to refer to the actual images.
taskcall_avatar_maps = {
    0: 'BlueAvatar.png',
    1: 'GreenAvatar.png',
    2: 'PurpleAvatar.png',
    3: 'RedAvatar.png',
    4: 'YellowAvatar.png'
}


def get_client_data_s3_bucket():
    '''
    Get the regional client data bucket.
    '''
    return file_storage.S3_BUCKET_TASKCALL_CLIENT_DATA[settings.REGION]


def get_taskcall_avatar_url(avatar_index=None):
    '''
    Get the url of a TaskCall avatar.
    :param avatar_index: the index of th avatar (0 - 4)
    :return: (str) avatar's full url
    '''
    if avatar_index is None:
        avatar_index = random.choice(list(taskcall_avatar_maps.keys()))
    folder_path = user_taskcall_avatars.format(taskcall_avatar_maps[avatar_index])
    file_url = '/'.join([settings.REDIRECT_BASE, client_files_url_prefix, folder_path])
    return file_url


def get_user_profile_picture_path(account_id, preferred_username, filename):
    '''
    Get the s3 path details and url path for uploading a user profile picture.
    :param account_id: (str) account_id of the organization
    :param preferred_username: (str) preferred username of the user
    :param filename: name of the file
    :return: (tuple) -> s3 bucket, folder path, s3 key, full file fetch url
    '''
    folder_path = user_profile_pictures.format(account_id, preferred_username)
    s3_key = folder_path + '/' + filename
    file_url = '/'.join([settings.REDIRECT_BASE, client_files_url_prefix, folder_path])
    return get_client_data_s3_bucket(), folder_path, s3_key, file_url


def get_organization_profile_pictures_path(account_id):
    '''
    Get the s3 path details where all profile pictures .
    :param account_id: (str) account_id of the organization
    :return: (tuple) -> s3 bucket, folder path, s3 key, full file fetch url
    '''
    folder_path = organization_profile_pictures.format(account_id)
    return get_client_data_s3_bucket(), folder_path


def get_greeting_audio_path(call_routing_ref_id, filename):
    '''
    Get the s3 path details and url path for uploading a live call routing greeting audio.
    :param call_routing_ref_id: (str) concealed call routing ref ID
    :param filename: name of the file
    :return: (tuple) -> s3 bucket, folder path, s3 key, full file fetch url
    '''
    folder_path = live_call_routing_greeting_audio.format(call_routing_ref_id)
    s3_key = folder_path + '/' + filename
    file_url = '/'.join([settings.REDIRECT_BASE, client_files_url_prefix, folder_path])
    return get_client_data_s3_bucket(), folder_path, s3_key, file_url


def get_ending_audio_path(call_routing_ref_id, filename):
    '''
    Get the s3 path details and url path for uploading a live call routing ending note audio.
    :param call_routing_ref_id: (str) concealed call routing ref ID
    :param filename: name of the file
    :return: (tuple) -> s3 bucket, folder path, s3 key, full file fetch url
    '''
    folder_path = live_call_routing_ending_audio.format(call_routing_ref_id)
    s3_key = folder_path + '/' + filename
    file_url = '/'.join([settings.REDIRECT_BASE, client_files_url_prefix, folder_path])
    return get_client_data_s3_bucket(), folder_path, s3_key, file_url


def get_status_page_image_type_folder_path(page_ref_id, image_type):
    '''
    Get the folder path for a status page image type.
    :param page_ref_id: (str) concealed status page ref ID
    :param image_type: type of image
    :return: (tuple) -> s3 bucket, folder path, s3 key, full file fetch url
    '''
    img_type_path = {var_names.logo: 'logo', var_names.icon: 'favicon', var_names.cover_image: 'cover-image'}
    folder_path = '/'.join([status_pages_images.format(page_ref_id), img_type_path[image_type]])
    return folder_path


def get_status_page_image_path(page_domain, page_ref_id, filename, image_type):
    '''
    Get the s3 path details and url path for uploading a status page image.
    :param page_domain: (str) domain of the status page
    :param page_ref_id: (str) concealed status page ref ID
    :param filename: name of the file
    :param image_type: type of image
    :return: (tuple) -> s3 bucket, folder path, s3 key, full file fetch url
    '''
    folder_path = get_status_page_image_type_folder_path(page_ref_id, image_type)
    s3_key = '/'.join([folder_path, filename])
    file_url = 'https://' + '/'.join([page_domain, client_files_url_prefix, s3_key])
    return get_client_data_s3_bucket(), folder_path, s3_key, file_url


def get_status_page_image_s3_key_from_url(image_url):
    '''
    Extract the s3 key path of a status page image given the image url.
    :param image_url: URL of the image
    :return: (string) s3 key
    '''
    pattern = r'https://.*/client/files/(images/status-pages/.*)'
    matches = re.findall(pattern, image_url)
    if len(matches) > 0:
        return matches[0]
    return None
