# -*- coding: utf-8; -*-
################################################################################
#
#  wuttaweb -- Web App for Wutta Framework
#  Copyright © 2024 Lance Edgar
#
#  This file is part of Wutta Framework.
#
#  Wutta Framework is free software: you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the Free
#  Software Foundation, either version 3 of the License, or (at your option) any
#  later version.
#
#  Wutta Framework is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
#  more details.
#
#  You should have received a copy of the GNU General Public License along with
#  Wutta Framework.  If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Utilities
"""

import importlib


def get_libver(
        request,
        key,
        default_only=False,
        prefix='wuttaweb',
):
    """
    Return the appropriate version string for the web resource library
    identified by ``key``.

    WuttaWeb makes certain assumptions about which libraries would be
    used on the frontend, and which versions for each would be used by
    default.  But it should also be possible to customize which
    versions are used, hence this function.

    Each library has a built-in default version but your config can
    override them, e.g.:

    .. code-block:: ini

       [wuttaweb]
       libver.bb_vue = 3.4.29

    :param request: Current request.

    :param key: Unique key for the library, as string.  Possibilities
       are the same as for :func:`get_liburl()`.

    :param default_only: If this flag is ``True``, the logic will
       *not* look for a "configured" version but rather will *only*
       return the "default" version regardless of config.

       If the flag is ``False`` (which it is by default) then the
       config value will be used if present, and a default version is
       used only if the config does not have a value.

    :param prefix: If specified, will override the prefix used for
       config lookups.

       .. warning::

          This ``prefix`` param is for backward compatibility and may
          be removed in the future.

    :returns: The appropriate version string, e.g. ``'1.2.3'`` or
       ``'latest'`` etc.
    """
    config = request.wutta_config

    # nb. we prefer a setting to be named like:  wuttaweb.libver.vue
    # but for back-compat this also can work:    wuttaweb.vue_version
    # however that compat only works for some of the settings...

    if not default_only:
        # nb. new/preferred setting
        version = config.get(f'{prefix}.libver.{key}')
        if version:
            return version

    if key == 'buefy':
        if not default_only:
            # nb. old/legacy setting
            version = config.get(f'{prefix}.buefy_version')
            if version:
                return version
        return 'latest'

    elif key == 'buefy.css':
        # nb. this always returns something
        return get_libver(request, 'buefy', default_only=default_only)

    elif key == 'vue':
        if not default_only:
            # nb. old/legacy setting
            version = config.get(f'{prefix}.vue_version')
            if version:
                return version
        return '2.6.14'

    elif key == 'vue_resource':
        return 'latest'

    elif key == 'fontawesome':
        return '5.3.1'

    elif key == 'bb_vue':
        return '3.4.31'

    elif key == 'bb_oruga':
        return '0.8.12'

    elif key in ('bb_oruga_bulma', 'bb_oruga_bulma_css'):
        return '0.3.0'

    elif key == 'bb_fontawesome_svg_core':
        return '6.5.2'

    elif key == 'bb_free_solid_svg_icons':
        return '6.5.2'

    elif key == 'bb_vue_fontawesome':
        return '3.0.6'


def get_liburl(
        request,
        key,
        prefix='wuttaweb',
):
    """
    Return the appropriate URL for the web resource library identified
    by ``key``.

    WuttaWeb makes certain assumptions about which libraries would be
    used on the frontend, and which versions for each would be used by
    default.  But ultimately a URL must be determined for each, hence
    this function.

    Each library has a built-in default URL which references a public
    Internet (i.e. CDN) resource, but your config can override the
    final URL in two ways:

    The simplest way is to just override the *version* but otherwise
    let the default logic construct the URL.  See :func:`get_libver()`
    for more on that approach.

    The most flexible way is to override the URL explicitly, e.g.:

    .. code-block:: ini

       [wuttaweb]
       liburl.bb_vue = https://example.com/cache/vue-3.4.31.js

    :param request: Current request.

    :param key: Unique key for the library, as string.  Possibilities
       are:

       Vue 2 + Buefy

       * ``vue``
       * ``vue_resource``
       * ``buefy``
       * ``buefy.css``
       * ``fontawesome``

       Vue 3 + Oruga

       * ``bb_vue``
       * ``bb_oruga``
       * ``bb_oruga_bulma``
       * ``bb_oruga_bulma_css``
       * ``bb_fontawesome_svg_core``
       * ``bb_free_solid_svg_icons``
       * ``bb_vue_fontawesome``

    :param prefix: If specified, will override the prefix used for
       config lookups.

       .. warning::

          This ``prefix`` param is for backward compatibility and may
          be removed in the future.

    :returns: The appropriate URL as string.
    """
    config = request.wutta_config

    url = config.get(f'{prefix}.liburl.{key}')
    if url:
        return url

    version = get_libver(request, key, prefix=prefix)

    if key == 'buefy':
        return f'https://unpkg.com/buefy@{version}/dist/buefy.min.js'

    elif key == 'buefy.css':
        return f'https://unpkg.com/buefy@{version}/dist/buefy.min.css'

    elif key == 'vue':
        return f'https://unpkg.com/vue@{version}/dist/vue.min.js'

    elif key == 'vue_resource':
        return f'https://cdn.jsdelivr.net/npm/vue-resource@{version}'

    elif key == 'fontawesome':
        return f'https://use.fontawesome.com/releases/v{version}/js/all.js'

    elif key == 'bb_vue':
        return f'https://unpkg.com/vue@{version}/dist/vue.esm-browser.prod.js'

    elif key == 'bb_oruga':
        return f'https://unpkg.com/@oruga-ui/oruga-next@{version}/dist/oruga.mjs'

    elif key == 'bb_oruga_bulma':
        return f'https://unpkg.com/@oruga-ui/theme-bulma@{version}/dist/bulma.mjs'

    elif key == 'bb_oruga_bulma_css':
        return f'https://unpkg.com/@oruga-ui/theme-bulma@{version}/dist/bulma.css'

    elif key == 'bb_fontawesome_svg_core':
        return f'https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-svg-core@{version}/+esm'

    elif key == 'bb_free_solid_svg_icons':
        return f'https://cdn.jsdelivr.net/npm/@fortawesome/free-solid-svg-icons@{version}/+esm'

    elif key == 'bb_vue_fontawesome':
        return f'https://cdn.jsdelivr.net/npm/@fortawesome/vue-fontawesome@{version}/+esm'
