import os
from pathlib import Path

import pandas as pd

from src.data_loading_and_saving.print_and_save_results import print_and_save_result


class ComparisonVariance:
    """
    A class used to calculate the Pearson's correlation coefficient between two variables.

    Attributes:
    ----------
        print_result: bool
            A flag to determine if the result should be printed.
        save_result: bool
            A flag to determine if the result should be saved.
        filepath: str
            The directory where the result should be saved.

    Methods
    -------
        calculate_correlation(data: pd.DataFrame, first_group_name: str, second_group_name: str, name_save_file: Path):
            This method calculates Pearson's correlation coefficient for the given columns
    """

    def __init__(
        self,
        print_result: bool = True,
        save_result: bool = True,
        filepath: str = "results/",
    ):
        """
        Constructs all the necessary attributes for the PearsonCorrelation object.

        Parameters
        ----------
            print_result: bool
                A flag to determine if the result should be printed.
            save_result: bool
                A flag to determine if the result should be saved.
            filepath: str
                The directory where the result should be saved.
        """
        self.print_result: bool = print_result
        self.save_result: bool = save_result
        self.filepath: str = filepath
        if not os.path.isdir(filepath):
            os.makedirs(filepath)

    def compare_ingroup_intergroup_variance(
        self, data: pd.DataFrame, variable: str, group: str, name_save_file: Path
    ) -> None:
        """
        Compares the variance within a group to the variance between groups for a specified variable.

        This method calculates the total variance of the variable, the average variance of the variable
        within each group, and the variance of the variable between groups. It then prints and/or saves
        these results based on the object's attributes.

        Parameters
        ----------
        data : pd.DataFrame
            The dataset containing the variable and group columns.
        variable : str
            The name of the column representing the variable for which variance is calculated.
        group : str
            The name of the column representing the groups.
        name_save_file : Path
            The name of the file to which the result will be saved (if save_result is True).
        """
        total_var: float = data[variable].var()

        between_group_var: float = data.groupby(group)[variable].var().mean()

        within_group_var: float = total_var - between_group_var

        results: str = f"""
        Total variance: {total_var}
        Between-group variance: {between_group_var}
        Within-group variance: {within_group_var}
        """

        print_and_save_result(
            self.print_result,
            self.save_result,
            self.filepath,
            results,
            name_save_file,
        )
