from core.explanations.factories.BaseExplanationFactory import BaseExplanationFactory
from core.explanations.factories.IndividualExplanationFactory import IndividualExplanationFactory


class AggregatorExplanationFactory(BaseExplanationFactory):
    individual_factory: IndividualExplanationFactory

    def __init__(self, model, label):
        super().__init__(model, label)
        self.individual_factory = IndividualExplanationFactory(model, label)

    def get_explanations(self, samples, epsilon, ordering, num_explanations, num_consumers, use_progress_bar):
        explanations = []

        for sample in samples:
            try:
                explanations_for_sample = self.individual_factory.get_explanations(
                    sample=sample,
                    epsilon=epsilon,
                    ordering=ordering,
                    num_explanations=num_explanations,
                    num_consumers=num_consumers,
                    use_progress_bar=use_progress_bar
                )
                explanations.append(explanations_for_sample)
            except ValueError:
                # If the sample is not valid, skip it
                explanations.append([])
                continue

        return explanations
