package net.ranides.assira.ui.parsers;

import net.ranides.assira.junit.NewAssert;
import net.ranides.assira.ui.helpers.ColorUtils;
import org.junit.Test;

import java.awt.*;
import java.util.Arrays;

import static org.junit.Assert.*;

public class ParseCssColorTest {

    @Test
    public void parseColorHex() {

        assertEquals(new Color(0xf0, 0x80, 0x40), ParseCssColor.color("#f08040"));
        assertEquals(new Color(0x10, 0x34, 0x72), ParseCssColor.color("#103472"));
        assertEquals(new Color(0x11, 0x33, 0x44), ParseCssColor.color("#134"));
        assertEquals(new Color(0xEE, 0xCC, 0x77), ParseCssColor.color("#eC7"));
        assertEquals(new Color(0x11, 0xCC, 0xAA, 0x55), ParseCssColor.color("#1Ca5"));
        assertEquals(new Color(0x10, 0x34, 0x72, 0x80), ParseCssColor.color("#10347280"));

        assertEquals(
            Arrays.asList("#5080a0", "#324668", "#778899"),
            ParseCssColor.colors("rgb ( 80, 128, 160 ) #324668 #789").map(ColorUtils::asCSS2).list()
        );

        NewAssert.assertThrows(IllegalArgumentException.class, ()->{
            ParseCssColor.color("#1C!");
        });
        NewAssert.assertThrows(IllegalArgumentException.class, ()->{
            ParseCssColor.color("#11");
        });

        NewAssert.assertThrows(IllegalArgumentException.class, ()->{
            ParseCssColor.color("#10345");
        });
        NewAssert.assertThrows(IllegalArgumentException.class, ()->{
            ParseCssColor.color("#1034728");
        });
        NewAssert.assertThrows(IllegalArgumentException.class, ()->{
            ParseCssColor.color("#103472800");
        });
    }

    @Test
    public void parseColorRGB() {
        assertEquals(new Color(15,20,30,191), ParseCssColor.color("rgba ( 15, 20, 30, 0.75 )"));
        assertEquals(new Color(15,20,30), ParseCssColor.color("rgb(15,20,30)"));

        assertEquals(new Color(10, 34, 72), ParseCssColor.color("rgb(10,34,72)"));
        assertEquals(new Color(10, 34, 72, 255), ParseCssColor.color("rgb(10,34,72)"));

        assertEquals(new Color(10, 34, 72, 0), ParseCssColor.color("rgba(10,34,72,0.00)"));
        assertEquals(new Color(10, 34, 72, 0), ParseCssColor.color("rgba (10, 34,  72, 0)"));

        assertEquals(new Color(10, 34, 72, 64), ParseCssColor.color("rgba(10,34,72,0.25)"));
        assertEquals(new Color(10, 34, 72, 191), ParseCssColor.color("rgba(10,34,72,0.75 )"));

        NewAssert.assertThrows(IllegalArgumentException.class, ()->{
            ParseCssColor.color("rgb(x");
        });
        NewAssert.assertThrows(IllegalArgumentException.class, ()->{
            ParseCssColor.color("rgba(x");
        });
    }

    @Test
    public void parseColorHSL() {

        assertEquals(new Color(0,128,0), ParseCssColor.color("hsl(120, 100%, 50%)"));
        assertEquals(new Color(120,191,48), ParseCssColor.color("hsl(100grad, 75%, 75%)"));
        assertEquals(new Color(191,191,48), ParseCssColor.color("hsl(60deg, 75%, 75%)"));

        assertEquals(new Color(0,128,0,0), ParseCssColor.color("hsl(120, 100%, 50%, 0)"));
        assertEquals(new Color(120,191,48,64), ParseCssColor.color("hsl(100grad, 75%, 75%, 0.25)"));
        assertEquals(new Color(191,191,48,191), ParseCssColor.color("hsl(60deg, 75%, 75%, 0.75)"));

        assertEquals(new Color(0,128,0,0), ParseCssColor.color("hsla(120, 100%, 50%, 0)"));
        assertEquals(new Color(120,191,48,64), ParseCssColor.color("hsla(0.25turn, 75%, 75%, 0.25)"));
        assertEquals(new Color(191,191,48,191), ParseCssColor.color("hsla(1.05rad, 75%, 75%, 0.75)"));

        NewAssert.assertThrows(IllegalArgumentException.class, ()->{
            ParseCssColor.color("hsl(x");
        });
        NewAssert.assertThrows(IllegalArgumentException.class, ()->{
            ParseCssColor.color("hsl(15,20)");
        });
        NewAssert.assertThrows(IllegalArgumentException.class, ()->{
            ParseCssColor.color("hsla(x");
        });
    }

    @Test
    public void parseInvalidColor() {
        NewAssert.assertThrows(IllegalArgumentException.class, ()->{
            ParseCssColor.color("nocolor");
        });
    }

    @Test
    public void parseNames() {
        assertEquals(ParseCssColor.color("#f0f8ff"), ParseCssColor.color("aliceblue"));
        assertEquals(ParseCssColor.color("#ff8c00"), ParseCssColor.color("darkorange"));
        assertEquals(ParseCssColor.color("#696969"), ParseCssColor.color("dimgray"));
        assertEquals(ParseCssColor.color("#778899"), ParseCssColor.color("lightslategray"));
        assertEquals(ParseCssColor.color("#da70d6"), ParseCssColor.color("orchid"));
        assertEquals(ParseCssColor.color("#ffc0cb"), ParseCssColor.color("pink"));
        assertEquals(ParseCssColor.color("#c0c0c0"), ParseCssColor.color("silver"));
        assertEquals(ParseCssColor.color("#008080"), ParseCssColor.color("teal"));
        assertEquals(ParseCssColor.color("#ff6347"), ParseCssColor.color("tomato"));
        assertEquals(ParseCssColor.color("#ffffff"), ParseCssColor.color("white"));
        assertEquals(ParseCssColor.color("#f5f5f5"), ParseCssColor.color("whitesmoke"));
        assertEquals(ParseCssColor.color("#ffff00"), ParseCssColor.color("yellow"));
        assertEquals(ParseCssColor.color("#9acd32"), ParseCssColor.color("yellowgreen"));
    }
}