package cz.senslog.analytics.utils;

import cz.senslog.analytics.domain.AttributeType;
import org.junit.jupiter.api.Test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class AttributeRegexTest {

    @Test
    public void regex() {
        String attributes = Stream.of(AttributeType.values()).map(AttributeType::name).map(String::toLowerCase).collect(Collectors.joining("|"));
        String variables = "timestamp|module|datasource";

        final String attrRegex = "(?<attribute>\\$("+attributes+"))(\\((?<format>\\S*)\\))?";
        final String varRegex = "(?<variable>\\$("+variables+"))(\\((?<format>\\S*)\\))?";


        final String test1 = "Hodnota $val(%.2f) překročila práh $VAL >= 25 °C v čase $timestamp.";
        final String test2 = "$val(%.2f) >= 25 & 12h <= $timestamp(HH:mm) <= 15h v čase $timestamp.";
        final String test3 = "$timestamp | $val";

        final String[] test = {test1, test2, test3};

        Pattern attrPattern = Pattern.compile(attrRegex, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL );
        Pattern varPattern = Pattern.compile(varRegex, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL );

        for (String s : test) {
            System.out.println(s);
            Matcher m1 = attrPattern.matcher(s);
            while (m1.find()) {
                System.out.format("\tAttr: %s\tFormat: %s\n", m1.group("attribute"), m1.group("format"));
            }
            Matcher m2 = varPattern.matcher(s);
            while (m2.find()) {
                System.out.format("\tVar: %s\tFormat: %s\n", m2.group("variable"), m2.group("format"));
            }
        }
    }
}
