package net.ranides.assira.ui.parsers;

import net.ranides.assira.events.EventLock;
import net.ranides.assira.events.EventReactor;
import net.ranides.assira.events.EventRouter;
import net.ranides.assira.observable.BeanEvent;
import net.ranides.assira.ui.helpers.WindowUtils;
import org.junit.Ignore;
import org.junit.Test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

public class ParseCssFontTest {

    @Test
    public void swing() {

        assertEquals(new Font("Arial Strange", Font.PLAIN, 20), ParseCssFont.font("Arial Strange-PLAIN-20") );

        assertEquals(new Font("Arial", Font.PLAIN, 20), ParseCssFont.font("Arial-20") );

        assertEquals(new Font("Verdana", Font.BOLD, 15), ParseCssFont.font("Verdana BOLD 15") );

        assertThrows(IllegalArgumentException.class, () -> {
            ParseCssFont.font("Verdana Strange BOLD 15");
        });

        assertEquals(new Font("Verdana", Font.PLAIN, 15), ParseCssFont.font("Verdana 15") );

        assertEquals(new Font("Verdana", Font.ITALIC, 18), ParseCssFont.font("Verdana ITALIC", ExampleComponents.PANEL) );
    }

    @Test
    public void css() {
        assertEquals(new Font("Arial", Font.ITALIC | Font.BOLD, 16), ParseCssFont.font("bold italic 12pt Arial"));

        assertEquals(new Font("Arial Black", Font.ITALIC | Font.BOLD, 24), ParseCssFont.font("italic bold 18pt \"Arial Black\""));
        assertEquals(new Font("Arial Black", Font.ITALIC, 24), ParseCssFont.font("oblique 30deg 18pt \"Arial Black\""));
        assertEquals(new Font("Arial Black", Font.ITALIC, 12), ParseCssFont.font("oblique 30deg 300 9pt \"Arial Black\""));
        assertEquals(new Font("Arial Black", Font.BOLD, 12), ParseCssFont.font("oblique 0deg 800 9pt \"Arial Black\""));
        assertEquals(new Font("Arial Black", Font.PLAIN, 12), ParseCssFont.font("oblique 0deg 100 9pt \"Arial Black\""));

        assertThrows(IllegalArgumentException.class, () -> {
            ParseCssFont.font("italic oblique 30deg bold 12pt Arial");
        });

        assertThrows(IllegalArgumentException.class, () -> {
            ParseCssFont.font("italic oblique bold 12pt \"Arial Black\"");
        });

        assertEquals(new Font("Verdana", Font.ITALIC, 16), ParseCssFont.font("oblique 12pt Verdana"));
        assertEquals(new Font("Verdana", Font.PLAIN, 16), ParseCssFont.font("12pt Verdana"));
        assertEquals(new Font("Arial Black", Font.PLAIN, 16), ParseCssFont.font("12pt \"Arial Black\""));
        assertEquals(new Font("Arial Black", Font.PLAIN, 16), ParseCssFont.font("12pt 'Arial Black'"));

        assertEquals(new Font("Arial Black", Font.PLAIN, 32), ParseCssFont.font("24pt \"Arial Black\", sans-serif"));

        assertEquals(new Font(Font.SANS_SERIF, Font.PLAIN, 32), ParseCssFont.font("24pt sans-serif"));
        assertEquals(new Font(Font.SERIF, Font.PLAIN, 32), ParseCssFont.font("24pt serif"));

        assertEquals(new Font(Font.SERIF, Font.PLAIN, 32), ParseCssFont.font("2em serif", ExampleComponents.BUTTON));
        assertEquals(new Font(Font.SERIF, Font.PLAIN, 48), ParseCssFont.font("2rem serif", ExampleComponents.BUTTON));
    }

    @Ignore("manual")
    @Test
    public void preview() {
        showPreview(ParseCssFont.font("bold 24pt 'comic sans ms'"));
        showPreview(ParseCssFont.font("bold italic 1em 'arial black'", ExampleComponents.PANEL));
    }

    private void showPreview(Font font) {
        JFrame frame = new JFrame();
        frame.setSize(600, 200);

        JLabel label = new JLabel();
        label.setText("Hello world. Thys ys font.");
        label.setForeground(ParseCssColor.color("#fff"));
        label.setBackground(ParseCssColor.color("#0088"));
        label.setOpaque(true);
        label.setFont(font);
        label.setHorizontalAlignment(SwingConstants.CENTER);
        label.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                WindowUtils.close(frame);
            }
        });
        frame.add(label);

        WindowUtils.waitForFrame(frame);
    }
}