#
# Copyright 2015 Rackspace, Inc
# All Rights Reserved
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from __future__ import annotations

import dbm
import logging
import os
from typing import cast

import dogpile.cache
import platformdirs


LOG = logging.getLogger(__name__)

AUTHOR: str = 'openstack'
PROGNAME: str = 'python-ironicclient'

CACHE: dogpile.cache.CacheRegion | None = None
CACHE_DIR: str = platformdirs.user_cache_dir(PROGNAME, AUTHOR)
CACHE_EXPIRY_ENV_VAR: str = 'IRONICCLIENT_CACHE_EXPIRY'  # environment variable
CACHE_FILENAME: str = os.path.join(CACHE_DIR, 'ironic-api-version.dbm')
DEFAULT_EXPIRY: int = 300  # seconds


def _get_cache() -> dogpile.cache.CacheRegion:
    """Configure file caching."""
    global CACHE
    if CACHE is None:

        # Ensure cache directory present; exist_ok tolerates a concurrent
        # create by another client process.
        if not os.path.exists(CACHE_DIR):
            os.makedirs(CACHE_DIR, exist_ok=True)

        # Use the cache expiry if specified in an env var
        expiry_time = os.environ.get(CACHE_EXPIRY_ENV_VAR, DEFAULT_EXPIRY)
        try:
            expiry_time = int(expiry_time)
        except ValueError:
            LOG.warning("Environment variable %(env_var)s should be an "
                        "integer (not '%(curr_val)s'). Using default "
                        "expiry of %(default)s seconds instead.",
                        {'env_var': CACHE_EXPIRY_ENV_VAR,
                         'curr_val': expiry_time,
                         'default': DEFAULT_EXPIRY})
            expiry_time = DEFAULT_EXPIRY

        CACHE = dogpile.cache.make_region(key_mangler=str).configure(
            'dogpile.cache.dbm',
            expiration_time=expiry_time,
            arguments={
                "filename": CACHE_FILENAME,
            }
        )
    return CACHE


def _build_key(host: str, port: str | int) -> str:
    """Build a key based upon the hostname or address supplied."""
    return "%s:%s" % (host, port)


def save_data(host: str, port: str | int, data: str) -> None:
    """Save 'data' for a particular 'host' in the appropriate cache dir.

    param host: The host that we need to save data for
    param port: The port on the host that we need to save data for
    param data: The data we want saved
    """
    key = _build_key(host, port)
    try:
        _get_cache().set(key, data)
    except dbm.error as e:
        LOG.debug('Could not write API version to cache file %(cache)s: '
                  '%(error)s', {'cache': CACHE_FILENAME, 'error': e})


def retrieve_data(
    host: str, port: str | int, expiry: int | None = None
) -> str | None:
    """Retrieve the version stored for an ironic 'host', if it's not stale.

    Check to see if there is valid cached data for the host/port
    combination and return that if it isn't stale.

    param host: The host that we need to retrieve data for
    param port: The port on the host that we need to retrieve data for
    param expiry: The age in seconds before cached data is deemed invalid
    """
    # Ensure that a cache file exists first
    if not os.path.isfile(CACHE_FILENAME):
        return None

    key = _build_key(host, port)
    try:
        data = _get_cache().get(key, expiration_time=expiry)
    except dbm.error as e:
        LOG.debug('Could not read API version from cache file %(cache)s: '
                  '%(error)s', {'cache': CACHE_FILENAME, 'error': e})
        return None

    if data == dogpile.cache.api.NO_VALUE:
        return None
    return cast(str, data)
