package model;

import model.DCEL.DoublyConnectedEdgeList;
import model.DCEL.Edge;
import model.DCEL.Face;
import model.DCEL.Node;
import org.junit.Before;
import org.junit.Test;

import java.util.LinkedList;

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

/**
 * Implementierung verschiedener Algorithmen zur Berechnung von Ausgleichsgeraden.
 *
 * @Author: Armin Wolf
 * @Email: a_wolf28@uni-muenster.de
 * @Date: 30.05.2017.
 */
public class DoublyConnectedEdgeListTest {

    private static DoublyConnectedEdgeList dcel;
    private static Node v1, v2, v3, v4, v5;
    private static Edge e1, e2, e3, e4, e5;
    private static Face f1, f2;

    @Before
    public void setUp() throws Exception {
        dcel = new DoublyConnectedEdgeList();

        //initialisiere die TestKnoten im Graphen
        v1 = dcel.createNode(new Point(2.5, 7.5), "v1");
        v2 = dcel.createNode(new Point(2.5, 4.0), "v2");
        v3 = dcel.createNode(new Point(6.5, 3.5), "v3");
        v4 = dcel.createNode(new Point(8.5, 6.5), "v4");
        v5 = dcel.createNode(new Point(6.0, 8.0), "v5");

        //initialisere Kanten im Graph
        e1 = dcel.createEdge(v1, v5, "e1");
        e2 = dcel.createEdge(v5, v4, "e2");
        e3 = dcel.createEdge(v4, v3, "e3");
        e4 = dcel.createEdge(v3, v2, "e4");
        e5 = dcel.createEdge(v2, v1, "e5");

        dcel.createConnection(e1, e2);
        dcel.createConnection(e2, e3);
        dcel.createConnection(e3, e4);
        dcel.createConnection(e4, e5);
        dcel.createConnection(e5, e1);

        //intialisiere die Flaechen
        f1 = dcel.createFace(null, e1, "f1");
        f2 = dcel.createFace(e1.getTwin(), null, "f2");
    }


    @Test
    public void testInnerComponentsAccess() {
        System.out.println("Test: testInnerComponentAccess();");
        Edge[] expected = {e1, e2, e3, e4, e5};
        LinkedList<Edge> list = dcel.getEdgesOfInnerComponents(f1);

        assertArrayEquals(expected, list.toArray());
    }

    @Test
    public void testOuterComponentsAccess() {
        System.out.println("Test: testOuterComponentAccess();");
        String[] expected = {"#e1", "#e5", "#e4", "#e3", "#e2"};
        LinkedList<Edge> list = dcel.getEdgesOfOuterComponents(f2);

        for (int i = 0; i < list.size(); i++) {
            assertEquals(expected[i], list.get(i).getID());
        }

    }


    @Test
    public void testNodeEdgeAccess() {
        System.out.println("Test: testNodeEdgeAccess();");

        assertEquals(5, dcel.getConnectedEdges(v4).size());
    }

    @Test
    public void testDCELAccess() {
        System.out.println("Test: testDCELAccess();");

        assertEquals(e1, e1.getTwin().getTwin());
        assertEquals(e1, e1.getPrev().getNext());
        assertEquals(e2.getID(), e1.getTwin().getPrev().getTwin().getID());
        assertEquals(e1, e1.getNext().getNext().getNext().getNext().getNext());
        assertEquals(e1.getTwin(), e1.getTwin().getNext().getNext().getNext().getNext().getNext());
    }


}