# By: Riasat Ullah
# This file handles translation of labels.

from taskcallrest import settings
from utils import s3
import pandas


univ_bucket = 'taskcall-prod-data'
univ_path = 'string_universes/label_universe.txt'


label_store = dict()
if settings.TEST_MODE:
    pd = pandas.read_csv(r'C:\Users\MSI\OneDrive\TaskCallDocs\StringUniverses\label_universe.txt',
                         index_col='label', skipinitialspace=True, quotechar='"', keep_default_na=False)
    label_store = pd.to_dict()
else:
    pd = pandas.read_csv(s3.read_bytes_io(univ_bucket, univ_path), index_col='label',
                         skipinitialspace=True, quotechar='"', keep_default_na=False)
    label_store = pd.to_dict()


def get_label(label, lang):
    try:
        return label_store[lang][label]
    except KeyError:
        # KeyErrors add extra quotes on the side causing the get_label requests making labels unidentifiable.
        # Hence, we are removing them here.
        label = label.strip("'").strip('"')
        if label in label_store[lang]:
            return label_store[lang][label]
        return label_store[settings.LANGUAGE_CODE][label]


def get_context(labels: list, lang: str):
    if lang not in label_store:
        lang = settings.LANGUAGE_CODE
    context = dict()
    for item in labels:
        context[item] = label_store[lang][item]
    return context
