"""
Helper class for loading generated file from webpack.
"""
import json
import logging
from urllib.parse import urlparse

from django.conf import settings
from xblock.utils.resources import ResourceLoader

logger = logging.getLogger(__name__)  # pylint: disable=invalid-name


def urljoin(*args):
    """
    Joining multiple url continuously. The default behavior from os.path.join
    would completely reset the path if the second path start with '/'.
    """
    beginning_slash = '/' if args[0].startswith('/') else ''
    trailing_slash = '/' if args[-1].endswith('/') else ''
    joined_path = '/'.join(map(lambda x: str(x).strip('/'), args))
    return beginning_slash + joined_path + trailing_slash


class LoadStatic:
    """
    Helper class for loading generated file from webpack.
    """

    _manifest = {}
    _base_url = ''
    _is_loaded = False

    resource_loader = ResourceLoader(__name__)

    @staticmethod
    def reload_manifest():
        """
        Reload from manifest file
        """
        root_url, base_url = '', '/static/dist/'
        base_url_override = ''

        if hasattr(settings, 'LMS_ROOT_URL'):
            root_url = settings.LMS_ROOT_URL
        else:
            logger.error('LMS_ROOT_URL is undefined')

        try:
            json_data = LoadStatic.resource_loader.load_unicode('static/dist/manifest.json')
            LoadStatic._manifest = json.loads(json_data)
            base_url_override = LoadStatic._manifest.get('base_url', None)
            LoadStatic._is_loaded = True
        except OSError:
            logger.error('Cannot find static/dist/manifest.json')
        finally:
            if base_url_override and urlparse(base_url_override).scheme:
                LoadStatic._base_url = base_url_override
            else:
                LoadStatic._base_url = urljoin(root_url, base_url)

    @staticmethod
    def get_url(key):
        """
        get url from key
        """
        if not LoadStatic._is_loaded:
            LoadStatic.reload_manifest()
        url = LoadStatic._manifest[key] if key in LoadStatic._manifest else key
        return urljoin(LoadStatic._base_url, url)
