package net.ranides.assira.ui.parsers;

import org.junit.Test;

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.Arrays;

import static org.junit.Assert.*;

public class ParseCssUnitTest {

    @Test
    public void multipleLength() {
        assertEquals(Arrays.asList(15, 30), GrammarBase.parseStream(ParseCssUnit.LENGTH, "length2", "15px 30px").list());
        assertEquals(Arrays.asList(15, 30, 151, 27), GrammarBase.parseStream(ParseCssUnit.LENGTH, "length4", "15px 30px 4cm 20pt").list());

        assertArrayEquals(new Integer[]{15, 30}, GrammarBase.parseStream(ParseCssUnit.LENGTH, "length2", "15px 30px").array(Integer[]::new));
        assertArrayEquals(new Integer[]{15, 30, 151, 27}, GrammarBase.parseStream(ParseCssUnit.LENGTH, "length4", "15px 30px 4cm 20pt").array(Integer[]::new));
    }

    @Test
    public void absolute() {
        assertEquals(10, ParseCssUnit.length("10px"));
        assertEquals(28, ParseCssUnit.length("21pt"));
        assertEquals(189, ParseCssUnit.length("5cm"));
        assertEquals(204, ParseCssUnit.length("5.4cm"));
        assertEquals(189, ParseCssUnit.length("50mm"));
        assertEquals(204, ParseCssUnit.length("54mm"));
        assertEquals(57, ParseCssUnit.length("1.5cm"));
        assertEquals(57, ParseCssUnit.length("60q"));
    }

    @Test
    public void relativeFont() {
        JComponent button = ExampleComponents.BUTTON;

        int lineHeight1 = ExampleComponents.BUTTON.getFontMetrics(button.getFont()).getHeight();
        int lineHeight2 = ExampleComponents.FRAME.getFontMetrics(ExampleComponents.FRAME.getFont()).getHeight();

        // 12px
        assertEquals(32, ParseCssUnit.length(button, "2em"));
        assertEquals(28, ParseCssUnit.length(button, "2ex"));
        assertEquals(28, ParseCssUnit.length(button, "2cap"));
        assertEquals(14, ParseCssUnit.length(button, "2ch"));
        assertEquals(18, ParseCssUnit.length(button, "2ic"));
        assertEquals(2 * lineHeight1, ParseCssUnit.length(button, "2lh"));

        // 18px
        assertEquals(48, ParseCssUnit.length(button, "2rem"));
        assertEquals(41, ParseCssUnit.length(button, "2rex"));
        assertEquals(41, ParseCssUnit.length(button, "2rcap"));
        assertEquals(20, ParseCssUnit.length(button, "2rch"));
        assertEquals(28, ParseCssUnit.length(button, "2ric"));
        assertEquals(2 * lineHeight2, ParseCssUnit.length(button, "2rlh"));
    }

    @Test
    public void relativeView() {
        JComponent button = ExampleComponents.BUTTON;

        assertEquals(400, ParseCssUnit.length(button, "100vw"));
        assertEquals(300, ParseCssUnit.length(button, "100vh"));
        assertEquals(400, ParseCssUnit.length(button, "100vmax"));
        assertEquals(300, ParseCssUnit.length(button, "100vmin"));

        assertEquals(180, ParseCssUnit.length(button, "100vi"));
        assertEquals(120, ParseCssUnit.length(button, "100vb"));
    }

    @Test
    public void angle() {
        assertEquals(360.0, ParseCssUnit.angle("360").doubleValue(), 0.01);
        assertEquals(180.0, ParseCssUnit.angle("180deg").doubleValue(), 0.01);
        assertEquals(720.0, ParseCssUnit.angle("2turn").doubleValue(), 0.01);
        assertEquals(90.0, ParseCssUnit.angle("100grad").doubleValue(), 0.01);
        assertEquals(135.0, ParseCssUnit.angle("150grad").doubleValue(), 0.01);
        assertEquals(180.0, ParseCssUnit.angle("3.1415rad").doubleValue(), 0.01);
    }
}