package projetMusee.backend;

import java.util.List;
import java.util.stream.Collectors;

import projetMusee.backend.enums.Granularite;
import projetMusee.facade.ApplicationFacade;

public class MuseumFilter {

    public static List<Museum> filterByGranularity(List<Museum> museumList, Granularite granularite, String... values) {
        validateMuseumList(museumList);
        if (granularite == null) {
            throw new IllegalArgumentException("La granularité ne peut pas être nulle.");
        }
        if (values == null || values.length == 0) {
            return List.copyOf(museumList); // Retourne une copie si pas de valeurs
        }

        List<String> encodedValues = List.of(values).stream()
                .map(ApplicationFacade::encode)
                .toList();

        return switch (granularite) {
            case REGION -> museumList.stream()
                    .filter(museum -> encodedValues.contains(ApplicationFacade.encode(museum.getRegion())))
                    .collect(Collectors.toList());
            case DEPARTEMENT -> museumList.stream()
                    .filter(museum -> encodedValues.contains(ApplicationFacade.encode(museum.getDepartment())))
                    .collect(Collectors.toList());
        };
    }

    public static List<Museum> filterByDepartement(List<Museum> museumList, String... departements) {
        validateMuseumList(museumList);
        if (departements == null || departements.length == 0) {
            return List.copyOf(museumList);
        }

        List<String> encoded = List.of(departements).stream()
                .map(ApplicationFacade::encode)
                .toList();

        return museumList.stream()
                .filter(museum -> encoded.contains(ApplicationFacade.encode(museum.getDepartment())))
                .collect(Collectors.toList());
    }

    public static List<Museum> filterByVille(List<Museum> museumList, String... villes) {
        validateMuseumList(museumList);
        if (villes == null || villes.length == 0) {
            return List.copyOf(museumList);
        }

        List<String> encoded = List.of(villes).stream()
                .map(ApplicationFacade::encode)
                .toList();

        return museumList.stream()
                .filter(museum -> encoded.contains(ApplicationFacade.encode(museum.getCity())))
                .collect(Collectors.toList());
    }

    public static List<Museum> filterByCreationYear(List<Museum> museumList, String... annees) {
        validateMuseumList(museumList);
        if (annees == null || annees.length == 0) {
            return List.copyOf(museumList);
        }

        List<String> encoded = List.of(annees).stream()
                .map(ApplicationFacade::encode)
                .toList();

        return museumList.stream()
                .filter(museum -> encoded.contains(ApplicationFacade.encode(String.valueOf(museum.getCreationYear()))))
                .collect(Collectors.toList());
    }

    public static List<Museum> filterByTheme(List<Museum> museumList, String... themes) {
        validateMuseumList(museumList);
        if (themes == null || themes.length == 0) {
            return List.copyOf(museumList);
        }

        List<String> encoded = List.of(themes).stream()
                .map(ApplicationFacade::encode)
                .toList();

        return museumList.stream()
                .filter(museum -> museum.getThematicDomain().stream()
                        .map(ApplicationFacade::encode)
                        .anyMatch(encoded::contains))
                .collect(Collectors.toList());
    }

    public static List<Museum> filterByRegions(List<Museum> museumList, String... regions) {
        validateMuseumList(museumList);
        if (regions == null || regions.length == 0) {
            return List.copyOf(museumList);
        }

        List<String> encoded = List.of(regions).stream()
                .map(ApplicationFacade::encode)
                .toList();

        return museumList.stream()
                .filter(museum -> encoded.contains(ApplicationFacade.encode(museum.getRegion())))
                .collect(Collectors.toList());
    }


  private static void validateMuseumList(List<Museum> museumList) {
    if (museumList == null) {
        throw new IllegalArgumentException("La liste de musées ne peut pas être nulle.");
    }
}

}