package UE04_270924_Map_Cocktails;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;

public class UE04_Cocktails {
    public static void main(String[] args) {
        System.out.println(readCocktails(Path.of("src/UE04_270924_Map_Cocktails/CocktailMix.txt")));
    }

    public static String readCocktails(Path path) {
        Map<String, Set<String>> cocktails = new TreeMap<>();
        List<String> lines;
        try {
            lines = Files.readAllLines(path);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        if (lines.isEmpty()) throw new RuntimeException();
        lines.removeIf(String::isEmpty);
        String existingIngredients = lines.getLast();
        lines.removeFirst();
        lines.removeLast();
        List<String> output = new ArrayList<>();
        List<String> ingredients = new ArrayList<>(List.of(existingIngredients.split(",")));
        ingredients.replaceAll(String::trim);
        //Set<String> values = cocktails.getOrDefault(key.trim(), new TreeSet<>());values.add(line.split(":")[0].trim());cocktails.put(key.trim(), values);
        for (String line : lines)
            for (String key : line.split(":")[1].split(","))
                cocktails.computeIfAbsent(key.trim(), _ -> new TreeSet<>()).add(line.split(":")[0].trim()); //Alternative from ChatGPT (original above)
        for (Map.Entry<String, Set<String>> entry : cocktails.entrySet())
            if (new HashSet<>(ingredients).containsAll(entry.getValue())) output.add(entry.getKey());
        return output.toString().substring(1, output.toString().length() - 1);
    }
}