"""Minor helper functions"""


class FunctionData:
    def __init__(self, func, params):
        self.func = func
        self.params = params

    def __call__(self, x, y):
        return self.func(x, y, self.params)


def transform_to_bayes_corrected_valence(
    upvotes: int, downvotes: int, average_valence: float, weight_factor_m: float
) -> float:
    """
    Transforms the upvotes and downvotes to its bayesian corrected value.

    Parameters
    ----------
    upvotes: int
        The number of upvotes.
    downvotes: int
        The number of downvotes.
    weight_factor_m: float
        The weight factor m.
    average_valence: float
        The average valence.

    Returns
    -------
    bayes_corrected_value: float
        The bayes_corrected_value between 0 and 1.
    """
    valence = -(downvotes / (downvotes + upvotes)) + 0.5
    totalvotes = upvotes + downvotes

    bayes_corrected_value = caluculate_bayes_correction(
        valence, totalvotes, weight_factor_m, average_valence
    )
    return bayes_corrected_value


def caluculate_bayes_correction(
    measure: float, volume: float, weight: float, average_measure: float
) -> float:
    """
    Calculates the bayes_corrected_value between 0 and 1 weighing in the volume.

    Parameters
    ----------
    measure: float
        The measure.
    volume: float
        The volume.
    weight: float
        The weight factor m. Weighting how strongly the volume is considered
    average_measure: float
        The average_measure.

    Returns
    -------
    bayes_corrected_measure: float
        The bayes_corrected_value between 0 and 1
    """
    bayes_corrected_measure = (
        volume / (volume + weight) * measure
        + weight / (volume + weight) * average_measure
    )

    return bayes_corrected_measure


def calculate_inverse_bayes_correction(
    bayes_corrected_value: float,
    volume: float,
    weight_factor_m: float,
    average_measure: float,
) -> float:
    """
    Calculates the inverse bayes_corrected_value between 0 and 1.

    Parameters
    ----------
    bayes_corrected_value: float
        The bayes_corrected_value between 0 and 1.
    volume: float
        The average_totalvotes.
    weight_factor_m: float
        The weight_factor.
    average_measure: float
        The average_measure.

    Returns
    -------
    downvotes: float
        The downvotes corresponding to the bayes_corrected_value for a given number of total votes.
    """
    inverse_bayes_corrected_value = (
        ((volume + weight_factor_m)/volume) * (bayes_corrected_value - weight_factor_m/(volume + weight_factor_m) * average_measure)
    )
    return inverse_bayes_corrected_value
