package com.knecon.fforesight.service.layoutparser.server.model;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.List;

import org.junit.jupiter.api.Test;

import com.knecon.fforesight.service.layoutparser.processor.model.table.CleanRulings;
import com.knecon.fforesight.service.layoutparser.processor.model.table.Ruling;

public class RulingTest {

    @Test
    public void testLineBetween() {

        List<Ruling> verticals = List.of(new Ruling(new Point2D.Double(10, 0), new Point2D.Double(10, 10)), new Ruling(new Point2D.Double(5, 0), new Point2D.Double(5, 5)));
        List<Ruling> horizontals = List.of(new Ruling(new Point2D.Double(0, 5), new Point2D.Double(10, 5)));

        CleanRulings cleanRulings = new CleanRulings(horizontals, verticals);

        Rectangle2D a = new Rectangle2D.Double(1, 6, 3, 3);
        Rectangle2D b = new Rectangle2D.Double(5, 6, 3, 3);
        Rectangle2D c = new Rectangle2D.Double(11, 6, 3, 3);
        Rectangle2D d = new Rectangle2D.Double(1, 1, 3, 3);
        Rectangle2D e = new Rectangle2D.Double(5, 1, 3, 3);
        Rectangle2D f = new Rectangle2D.Double(11, 1, 3, 3);

        assertFalse(cleanRulings.lineBetween(a, a));
        assertFalse(cleanRulings.lineBetween(a, b));
        assertTrue(cleanRulings.lineBetween(a, c));
        assertTrue(cleanRulings.lineBetween(a, d));
        assertTrue(cleanRulings.lineBetween(a, e));
        assertTrue(cleanRulings.lineBetween(a, f));

        assertFalse(cleanRulings.lineBetween(d, d));
        assertTrue(cleanRulings.lineBetween(d, b));
        assertTrue(cleanRulings.lineBetween(d, c));
        assertTrue(cleanRulings.lineBetween(d, a));
        assertTrue(cleanRulings.lineBetween(d, e));
        assertTrue(cleanRulings.lineBetween(d, f));

        assertFalse(cleanRulings.lineBetween(c, c));
        assertTrue(cleanRulings.lineBetween(c, b));
        assertTrue(cleanRulings.lineBetween(c, d));
        assertTrue(cleanRulings.lineBetween(c, a));
        assertTrue(cleanRulings.lineBetween(c, e));
        assertFalse(cleanRulings.lineBetween(c, f));

        var all = List.of(a, b, c, d, e, f);
        for (Rectangle2D r1 : all) {
            for (Rectangle2D r2 : all) {
                assertEquals(cleanRulings.lineBetween(r1, r2), cleanRulings.lineBetween(r2, r1));
            }
        }
    }

}
