""" ConditionalDecorator module. """

#  ISC License
#
#  Copyright (c) 2020–2022, Paul Wilhelm, M. Sc. <anfrage@paulwilhelm.de>
#
#  Permission to use, copy, modify, and/or distribute this software for any
#  purpose with or without fee is hereby granted, provided that the above
#  copyright notice and this permission notice appear in all copies.
#
#  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
#  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
#  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
#  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
#  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
#  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

from typing import Callable, Any


class ConditionalDecorator(object):
    """ ConditionalDecorator class. """

    def __init__(self, condition: bool, decorating_func: Callable, **decorating_args: Any) -> None:
        """
        Initializes a conditional decorator.

        @param condition: Condition
        @param decorating_func: Decorating function
        @param decorating_args: Arguments to decorating function
        """
        self.condition = condition
        self.decorating_func = decorating_func
        self.decorating_args = decorating_args

    def __call__(self, func: Callable) -> Callable:
        """
        Gets called when a function is to be decorated.

        @param func: Function to be decorated
        @return: Decorated function
        """
        return self.decorating_func(func, **self.decorating_args) if self.condition else func
