package net.ranides.assira.ui.parsers;

import net.ranides.assira.junit.ImageAssert;
import org.junit.Test;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

import static org.junit.Assert.*;

public class ParseCssBorderTest {

    @Test
    public void testBorderSmoke() {
        assertNotNull( GrammarBase.PARSER.parse("root_border", "solid 1px #808080") );
        assertNotNull( GrammarBase.PARSER.parse("root_border", "#808080 1px solid") );
        assertNotNull( GrammarBase.PARSER.parse("root_border", "#808080 1px") );
        assertNotNull( GrammarBase.PARSER.parse("root_border", "solid") );
    }

    @Test
    public void testBorderCSS() throws IOException {
        JPanel panel = new JPanel();
        panel.setBackground(new Color(128,192,192));
        panel.setForeground(new Color(255,128,0));

        ImageAssert.assertEquals("borders/border1solid1.bmp", paintBorder(panel, ParseCssBorder.border(panel, "solid 1px #f00")));
        ImageAssert.assertEquals("borders/border1solid2.bmp", paintBorder(panel, ParseCssBorder.border(panel, "solid #800 1px 2px 3px 4px")));
        ImageAssert.assertEquals("borders/border1solid3.bmp", paintBorder(panel, ParseCssBorder.border(panel, "solid 1px")));
        ImageAssert.assertEquals("borders/border1solid4.bmp", paintBorder(panel, ParseCssBorder.border(panel, "solid 1px 2px 3px 4px")));

        ImageAssert.assertEquals("borders/border1outset1.bmp", paintBorder(panel, ParseCssBorder.border(panel, "outset 1px #f00")));
        ImageAssert.assertEquals("borders/border1outset2.bmp", paintBorder(panel, ParseCssBorder.border(panel, "outset 4px #800")));
        ImageAssert.assertEquals("borders/border1outset3.bmp", paintBorder(panel, ParseCssBorder.border(panel, "outset 4px")));
        ImageAssert.assertEquals("borders/border1outset4.bmp", paintBorder(panel, ParseCssBorder.border(panel, "outset #800")));
        ImageAssert.assertEquals("borders/border1outset5.bmp", paintBorder(panel, ParseCssBorder.border(panel, "outset")));


        ImageAssert.assertEquals("borders/border1dashed1.bmp", paintBorder(panel, ParseCssBorder.border(panel, "dashed #ff0 1px")));
        ImageAssert.assertEquals("borders/border1dashed2.bmp", paintBorder(panel, ParseCssBorder.border(panel, "dashed #ff0 4px")));
        ImageAssert.assertEquals("borders/border1dashed3.bmp", paintBorder(panel, ParseCssBorder.border(panel, "dashed 4px")));

        ImageAssert.assertEquals("borders/border1dotted1.bmp", paintBorder(panel, ParseCssBorder.border(panel, "dotted #ff0 1px")));
        ImageAssert.assertEquals("borders/border1dotted2.bmp", paintBorder(panel, ParseCssBorder.border(panel, "dotted #ff0 4px")));
        ImageAssert.assertEquals("borders/border1dotted3.bmp", paintBorder(panel, ParseCssBorder.border(panel, "dotted 4px")));

        ImageAssert.assertEquals("borders/border1double1.bmp", paintBorder(panel, ParseCssBorder.border(panel, "double #ff0 1px")));
        ImageAssert.assertEquals("borders/border1double2.bmp", paintBorder(panel, ParseCssBorder.border(panel, "double #ff0 4px")));
        ImageAssert.assertEquals("borders/border1double3.bmp", paintBorder(panel, ParseCssBorder.border(panel, "double 4px")));

        ImageAssert.assertEquals("borders/border1groove1.bmp", paintBorder(panel, ParseCssBorder.border(panel, "groove 1px")));
        ImageAssert.assertEquals("borders/border1groove2.bmp", paintBorder(panel, ParseCssBorder.border(panel, "groove 4px")));

        ImageAssert.assertEquals("borders/border1ridge1.bmp", paintBorder(panel, ParseCssBorder.border(panel, "ridge 1px")));
        ImageAssert.assertEquals("borders/border1ridge2.bmp", paintBorder(panel, ParseCssBorder.border(panel, "ridge 4px")));
    }

    @Test
    public void testBorderSwing() throws IOException {
        JPanel panel = new JPanel();
        panel.setBackground(new Color(128,192,192));
        panel.setForeground(Color.RED);

        ImageAssert.assertEquals("borders/border2etched1.bmp", paintBorder(panel, ParseCssBorder.border(panel, "etched raised #800 #080")));
        ImageAssert.assertEquals("borders/border2etched2.bmp", paintBorder(panel, ParseCssBorder.border(panel, "etched raised #080")));
        ImageAssert.assertEquals("borders/border2etched3.bmp", paintBorder(panel, ParseCssBorder.border(panel, "etched raised")));

        ImageAssert.assertEquals("borders/border2soft1.bmp", paintBorder(panel, ParseCssBorder.border(panel, "soft-bevel raised #0a8 #084")));
        ImageAssert.assertEquals("borders/border2soft2.bmp", paintBorder(panel, ParseCssBorder.border(panel, "soft-bevel raised #080")));
        ImageAssert.assertEquals("borders/border2soft3.bmp", paintBorder(panel, ParseCssBorder.border(panel, "soft-bevel raised")));
    }

    @Test
    public void testBorderSize() {
        assertEquals("2px", GrammarBase.PARSER.parse("borderSize", "2px").text() );
        assertEquals("2px 4px", GrammarBase.PARSER.parse("borderSize", "2px 4px").text() );
        assertEquals("2px 4px 8px 2px", GrammarBase.PARSER.parse("borderSize", "2px 4px 8px 2px").text() );

        assertEquals("2px", GrammarBase.PARSER.parse("borderSize", "2px4px8px2px").text() );
    }

    private BufferedImage paintBorder(JPanel panel, Border border) {
        BufferedImage output = new BufferedImage(200, 100, BufferedImage.TYPE_INT_RGB);
        Graphics graphics3 = output.getGraphics();
        graphics3.setColor(panel.getBackground());
        graphics3.fillRect(0,0,200,100);
        border.paintBorder(panel, graphics3, 1, 1, 198, 98);
        return output;
    }

}