"""Utils module"""

import logging

from cerberus import Validator

from secrets_safe_library import exceptions


def print_log(logger, log_message, level):
    """
    Print a log message
    Arguments:
        Logger
        Log level
        Log message
    Returns:
    """
    if logger:
        if level == logging.DEBUG:
            logger.debug(log_message)
        elif level == logging.INFO:
            logger.info(log_message)
        elif level == logging.ERROR:
            logger.error(log_message)
        elif level == logging.WARN:
            logger.warning(log_message)


def validate_inputs(inputs):
    """
    Validate inputs
    Arguments:
        inputs (dict)
    Returns:
        inputs (dict)
    """

    schema = {
        "api_url": {
            "type": "string",
            "regex": r"^(https?|http)://[^\s/$.?#].[^\s]*$",
            "required": True,
        }
    }

    if "api_version" in inputs:
        schema.update(
            {
                "api_version": {
                    "type": "string",
                    "minlength": 3,
                    "maxlength": 3,
                    "required": False,
                }
            }
        )

    if "api_key" in inputs:
        schema.update(
            {
                "api_key": {
                    "type": "string",
                    "minlength": 128,
                    "maxlength": 263,
                    "required": True,
                }
            }
        )
    else:
        schema.update(
            {
                "client_id": {
                    "type": "string",
                    "minlength": 36,
                    "maxlength": 40,
                    "required": True,
                },
                "client_secret": {
                    "type": "string",
                    "minlength": 36,
                    "maxlength": 64,
                    "required": True,
                },
            }
        )

    v = Validator(schema)

    if v.validate(inputs):
        if "/BeyondTrust/api/public/v" not in inputs["api_url"]:
            raise exceptions.OptionsError(
                "invalid API URL, it must contains /BeyondTrust/api/public/v as part of"
                " the path"
            )

        inputs["api_url"] = inputs["api_url"].strip()

    else:
        raise exceptions.OptionsError(f"Errors: {v.errors}")

    return inputs


def prepare_certificate_info(certificate, certificate_key):
    """
    Validate certificate and certificate key
    Arguments:
        certificate
        certificate_key
    Returns:
        certificate
        certificate_key

    """

    if certificate and certificate_key:

        certificate_length_in_bits = len(certificate.encode("utf-8")) * 8

        if certificate_length_in_bits > 32768:
            message = "Invalid length for certificate, the maximum size is 32768 bits"
            raise exceptions.OptionsError(message)

        certificate_key_length_in_bits = len(certificate_key.encode("utf-8")) * 8

        if certificate_key_length_in_bits > 32768:
            message = (
                "Invalid length for certificate key, the maximum size is 32768 bits"
            )
            raise exceptions.OptionsError(message)

        if "BEGIN CERTIFICATE" not in certificate:
            raise exceptions.OptionsError("Bad certificate content")

        if "BEGIN PRIVATE KEY" not in certificate_key:
            raise exceptions.OptionsError("Bad certificate key content")

        certificate = certificate.replace(r"\n", "\n")
        certificate_key = certificate_key.replace(r"\n", "\n")

        certificate = f"{certificate}\n"
        certificate_key = f"{certificate_key}\n"

        return certificate, certificate_key

    return "", ""
