package net.ranides.assira.ui.parsers;

import lombok.experimental.UtilityClass;
import net.ranides.assira.grammar.G4Selector;
import net.ranides.assira.grammar.G4SelectorBuilder;
import net.ranides.grammars.generated.UiGrammarParser;

import java.awt.*;
import java.util.List;

@UtilityClass
public class ParseSwingGeometry {

    public static final G4Selector<List<Integer>> COORDS = new G4SelectorBuilder<List<Integer>>()
        .withName("ParseSwingCoords:COORDS")
        .withParser(GrammarBase.PARSER)
        .mapRule(UiGrammarParser.RULE_length2, node -> {
            return ParseCssUnit.LENGTH.list(node);
        })
        .mapRule(UiGrammarParser.RULE_length4, node -> {
            return ParseCssUnit.LENGTH.list(node);
        })
        .mapRule(UiGrammarParser.RULE_lengths, node -> {
            return ParseCssUnit.LENGTH.list(node);
        })
        .mapRule(UiGrammarParser.RULE_numberInt2, node -> {
            return ParsePrimitives.INT.list(node);
        })
        .mapRule(UiGrammarParser.RULE_numberInt4, node -> {
            return ParsePrimitives.INT.list(node);
        })
        .mapRule(UiGrammarParser.RULE_numberInts, node -> {
            return ParsePrimitives.INT.list(node);
        })
        .prepare();

    public static Point parsePoint(String value) {
        List<Integer> ints = GrammarBase.parseSingle(COORDS, "root_coords2", value);
        return new Point(ints.get(0), ints.get(1));
    }

    public static Dimension parseDimension(String value) {
        List<Integer> ints = GrammarBase.parseSingle(COORDS, "root_coords2", value);
        return new Dimension(ints.get(0), ints.get(1));
    }

    public static Rectangle parseRectangle(String value) {
        List<Integer> ints = GrammarBase.parseSingle(COORDS, "root_coords4", value);
        return new Rectangle(ints.get(0), ints.get(1), ints.get(2), ints.get(3));
    }

    public static Insets parseInsets(String value) {
        List<Integer> ints = GrammarBase.parseSingle(COORDS, "root_coords4", value);
        return new Insets(ints.get(0), ints.get(1), ints.get(2), ints.get(3));
    }

}
