"""This file and its contents are licensed under the Apache License 2.0. Please see the included NOTICE for copyright information and LICENSE for a copy of the license.
"""
import redis
import logging

from django_rq import get_connection

logger = logging.getLogger(__name__)

try:
    _redis = get_connection()
    _redis.ping()
except:
    logger.warning("Redis is not connected.")
    _redis = None


def redis_healthcheck():
    if not _redis:
        return False
    try:
        _redis.ping()
    except redis.exceptions.ConnectionError as exc:
        logger.error(f"Redis healthcheck failed with ConnectionError: {exc}", exc_info=True)
        return False
    except redis.exceptions.TimeoutError as exc:
        logger.error(f"Redis healthcheck failed with TimeoutError: {exc}", exc_info=True)
        return False
    except redis.exceptions.RedisError as exc:
        logger.error(f"Redis healthcheck failed: {exc}", exc_info=True)
        return False
    else:
        logger.debug("Redis client is alive!")
        return True


def redis_connected():
    return redis_healthcheck()


def redis_get(key):
    if not redis_healthcheck():
        return
    return _redis.get(key)


def redis_hget(key1, key2):
    if not redis_healthcheck():
        return
    return _redis.hget(key1, key2)


def redis_set(key, value, ttl=None):
    if not redis_healthcheck():
        return
    return _redis.set(key, value, ex=ttl)


def redis_hset(key1, key2, value):
    if not redis_healthcheck():
        return
    return _redis.hset(key1, key2, value)


def redis_delete(key):
    if not redis_healthcheck():
        return
    return _redis.delete(key)
