package UE15_030325_Streams2;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class UE15_Streams2 {
    public static void main(String[] args) {
//        System.out.println(lottoziehung());
//        System.out.println(berechnePI(-5));
//        System.out.println(berechnePI(1));
//        System.out.println(berechnePI(2));
//        System.out.println(berechnePI(3));
//        System.out.println(berechnePI(1001));
//        System.out.println(zaehleZeichenAusVorrat("Hallo 1AI", "aeiou"));
//        System.out.println(zaehleZeichenAusVorrat("*code*123#", "?%*!#$"));
//        System.out.println(createRandoString("1234", 5));
//        System.out.println(Arrays.toString(deleteEquals(new int[]{1, 3, 3, 1, 2, 1, 5})));
//        System.out.println(zeichenStatistik("Hallo Welt wie geht es heute"));

//        System.out.println("hallo du 2kek!".chars().mapToObj(c -> (char) c).filter(Character::isAlphabetic).map(Object::toString).collect(Collectors.joining()));

//        System.out.println(Arrays.stream("hallo du du 2kek!".split(" ")).distinct().toList());

        System.out.printf("Filtered and Mapped: %s\n", filterMap(Arrays.asList("hell", "highway", "figure.09", "in the end", "busfahrt")));
        System.out.printf("Sum of the even Elements: %d\n", sumOfEven(Arrays.asList(1, 5, 22, 4, 6, 44, 7, 45)));
        System.out.printf("Mapping between word and its length: %s\n", mapColl(Arrays.asList("word", "and", "another", "one", "one")));
        System.out.printf("Maximum: %d%n", getMax(Arrays.asList(1, 5, 2, 3, 5, 7, 44, 3, 6, 2)));
        System.out.printf("Reverse sorted List: %s%n", reverseLengthSorting(Arrays.asList("a", "aa", "bbb", "ejcjiqejifoew")));
        System.out.printf("Flattened List: %s%n", flatten(Arrays.asList(Arrays.asList(1, 2, 3), Arrays.asList(4, 5, 6))));
        System.out.printf("Count of Elements > 10: %d%n", countAbvTen(Arrays.asList(1,55,3,788,4,654,3,664,55,78,5)));

    }

    static List<String> filterMap(List<String> input) {
        return input.stream().filter(c -> !c.contains("a"))
                .map(String::toUpperCase).collect(Collectors.toList());
    }

    static int sumOfEven(List<Integer> input) {
        return input.stream().filter(i -> i % 2 == 0).mapToInt(i -> i).sum();
    }

    static Map<String, Integer> mapColl(List<String> words) {
        return words.stream().collect(Collectors.toMap(s -> s, String::length, (_, y) -> y));
    }

    static Integer getMax(List<Integer> input) {
        return input.stream().mapToInt(i -> i).max().orElse(-1);
    }

    static List<String> reverseLengthSorting(List<String> input) {
        return input.stream().sorted((x, y) -> y.length() - x.length()).collect(Collectors.toList());
    }

    static int countAbvTen(List<Integer> ints) {
        return (int) ints.stream().filter(i -> i > 10).count();
    }


    static List<Integer> flatten(List<List<Integer>> nested) {
        return nested.stream().flatMap(List::stream).toList();
    }

    public static Set<Integer> lottoziehung() {
        return new Random().ints(1, 46).distinct().limit(6).boxed().collect(Collectors.toSet());
    }

    public static double berechnePI(int anzGlieder) {
        return anzGlieder < 1 ? -1 : 4 * IntStream.range(0, anzGlieder).mapToDouble(n -> Math.pow(-1, n) / (2 * n + 1)).sum();
    }

    public static int zaehleZeichenAusVorrat(String s, String vorrat) {
//        Set<Character> chars = vorrat.chars().mapToObj(c -> (char) c).collect(Collectors.toSet());
        return (int) s.chars().filter(c -> vorrat.chars().mapToObj(n -> (char) n).collect(Collectors.toSet()).contains((char) c)).count();
    }

    public static String createRandoString(String vorrat, int len) {
        return IntStream.range(0, len).mapToObj(_ -> String.valueOf(vorrat.charAt(new Random().nextInt(vorrat.length())))).collect(Collectors.joining());
    }

    public static int[] deleteEquals(int[] ia) {
        return Arrays.stream(ia).distinct().toArray();
    }

    public static Map<Character, Integer> zeichenStatistik(String s) {
        return s.chars().mapToObj(c -> (char) c).collect(Collectors.toMap(Function.identity(), n -> 1, Integer::sum));
    }
}
