package UE13_110225_Lambdas;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.UnaryOperator;

public class UE13_Lambdas {
    public static void main(String[] args) {
        List<Double> list = Arrays.asList(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 20.0, 30.0);
        print(hauptworte(new ArrayList<>(Arrays.asList(null, "Hugo", "HTL", "HTL3R", "", "Haus-Boot", "SEW", "Softwareentwicklung", "a"))));
        System.out.println("\n");
        print(hauptworte(new ArrayList<>(Arrays.asList("Hallo", "du", "Keiko", "DU", "MensH", ".", "async1w19", "Aast1w19"))));
        System.out.println(mult(list, 5));
        System.out.println(mult(list, -1));
        System.out.println(mult(list, -2));
        System.out.println(func(list, a -> a + 1));
        System.out.println(numerisch("12", "Wappler", "8", "-5", "8", "Hugo", "-9", "-10", "Hugo", "-10", "7"));
        System.out.println(numerisch("12", "Wappler", "8", "-5", "8", "Hugo", "-9", "-10", "Hugo", "-10", "7", "10.5", "10,5"));
    }

    public static void print(Collection<?> collection) {
        collection.forEach(System.out::println);
    }

    public static Collection<String> hauptworte(Collection<String> worte) {
        return worte.stream().filter(w -> w != null && !w.isEmpty() && w.matches("[A-Z][A-Za-z]*")).toList();
    }

    public static List<Double> mult(List<Double> zahlen, double faktor) {
        return zahlen.stream().map(z -> z * faktor).toList();
    }

    public static List<Double> func(List<Double> zahlen, UnaryOperator<Double> op) {
        return zahlen.stream().map(op).toList();
    }

    public static List<String> numerisch(String... elements) {
        String p = "^-?\\d+$";
//        Methode 1:
//        Arrays.sort(elements, (o1, o2) -> {
//            if (o1.matches(p) && o2.matches(p)) return Integer.compare(Integer.parseInt(o1), Integer.parseInt(o2));
//            else if (o1.matches(p)) return -1;
//            else if (o2.matches(p)) return 1;
//            else return o1.compareTo(o2);
//        });
//        return List.of(elements);

//        Methode 2
//        Arrays.sort(elements, (o1, o2) -> o1.matches(p) && o2.matches(p) ? Integer.compare(Integer.parseInt(o1), Integer.parseInt(o2)) : o1.matches(p) ? -1 : o2.matches(p) ? 1 : o1.compareTo(o2));
//        return List.of(elements);

//        Methode 3
        return Arrays.stream(elements).sorted((o1, o2) -> o1.matches(p) && o2.matches(p) ? Integer.compare(Integer.parseInt(o1), Integer.parseInt(o2)) : o1.matches(p) ? -1 : o2.matches(p) ? 1 : o1.compareTo(o2)).toList();
    }
}