/**
 *  Licensed under GPL. For more information, see
 *    http://jaxodraw.sourceforge.net/license.html
 *  or the LICENSE file in the jaxodraw distribution.
 */
package net.sf.jaxodraw.object.vertex;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;

import net.sf.jaxodraw.JaxoTestUtils;
import net.sf.jaxodraw.io.imports.JaxoAxodraw;
import net.sf.jaxodraw.object.JaxoObject;
import net.sf.jaxodraw.object.JaxoObjectFactory;
import net.sf.jaxodraw.object.loop.JaxoFLoop;
import net.sf.jaxodraw.util.JaxoColor;
import net.sf.jaxodraw.util.JaxoConstants;
import net.sf.jaxodraw.util.JaxoPrefs;


/** Tests JaxoVertexT1. */
public class JaxoVertexT1Test extends AbstractJaxoVertexTest {
    private JaxoVertexT1 vertex = new JaxoVertexT1();

    /** Constructor: just calls super().
     * @param testName The name of this test.
     */
    public JaxoVertexT1Test(String testName) {
        super(testName);
    }

    /** JUnit setup: initialize the object. */
    @Override
    protected void setUp() {
        super.setUp();
        init();
    }

    private void init() {
        vertex.setLocation(100, 100);
        vertex.setRelWAndH(100, 100);
        vertex.setStrokeWidth(1.f);
        vertex.setColor(JaxoColor.GREEN);
    }

    /** {@inheritDoc} */
    public JaxoObject getInstance() {
        return vertex;
    }

    /** Tests serialization. */
    public void testSerialization() {
        vertex.setStrokeWidth(1.5f);
        vertex.setColor(JaxoColor.RED);
        JaxoTestUtils.checkSerializedEquality(vertex);
    }

    /** Tests that a copy of an object is still the same object. */
    public void testCopy() {
        vertex.setStrokeWidth(1.5f);
        vertex.setColor(JaxoColor.RED);
        JaxoTestUtils.checkCopyEquality(vertex);

        JaxoVertexT1 vertex2 = new JaxoVertexT1();
        vertex2.setLocation(100, 100);
        vertex2.setRelWAndH(100, 100);
        vertex2.setStrokeWidth(1.f);
        vertex2.setColor(JaxoColor.GREEN);
        assertFalse(vertex2.isCopy(vertex));

        vertex2.setStrokeWidth(1.5f);
        vertex2.setColor(JaxoColor.RED);
        assertTrue(vertex2.isCopy(vertex));

        JaxoFLoop loop = new JaxoFLoop();
        assertFalse(vertex.isCopy(loop));

    }

    /** Tests identity. */
    public void testIdentity() {
        JaxoVertexT1 vertex2 = vertex;
        assertEquals(vertex, vertex2);
    }

    /** Tests the latex command. */
    public void testLatexCommand() {
        Dimension dim = new Dimension(44, 44);

        vertex.setRelWAndH(15, 13);
        String expected = "\\Vertex(100,-56){19.849}";
        String actual = vertex.latexCommand(1.f, dim);
        assertEquals("Latex commands do not match!", expected, actual);

        expected = "%";
        actual = vertex.latexCommand(100.f, dim);
        assertEquals("Latex commands do not match!", expected, actual);

    }

    /** Tests axodraw. */
    public void testAxodraw() {
        JaxoAxodraw axo = new JaxoAxodraw();
        String command;

        command = "\\Vertex(100,-56){19.85}";
        vertex = (JaxoVertexT1) axo.getJaxoObject(command);
        checkVertexParameters();

    }

    private void checkVertexParameters() {
        assertEquals("VertexT1 doesn't have the right color!",
            JaxoColor.BLACK, vertex.getColor());
        assertEquals("VertexT1 doesn't have the right relw!", 14,
            vertex.getRelw());
        assertEquals("VertexT1 doesn't have the right relh!", 14,
            vertex.getRelh());
        assertEquals("VertexT1 doesn't have the right x!", 100, vertex.getX());
        assertEquals("VertexT1 doesn't have the right y!", 56, vertex.getY());
    }

    /** Tests the getBounds() method. */
    public void testGetBounds() {
        Rectangle bBox = vertex.getBounds();
        Rectangle expected = new Rectangle(-42, -42, 284, 284);
        assertEquals("Bounding box not correct!", expected, bBox);

        assertNotNull(vertex.getBounds2D());
    }

    /** Tests createNewObject in JaxoObjectFactory. */
    public void testCreateNewObject() {
        Point p = new Point(50, 50);
        JaxoObject ob =
            JaxoObjectFactory.newObject(JaxoConstants.VERTEXT1, new Point[] {p, p});

        assertTrue("createNewObject did not return a VertexT1!",
            (ob instanceof JaxoVertexT1));

        vertex = (JaxoVertexT1) ob;
        assertEquals("VertexT1 doesn't have the right x!", 50, vertex.getX());
        assertEquals("VertexT1 doesn't have the right y!", 50, vertex.getY());
        assertEquals("VertexT1 doesn't have the right color!",
            JaxoColor.getColor(JaxoPrefs.getStringPref(JaxoPrefs.PREF_COLOR)),
            vertex.getColor());

    }

    /** {@inheritDoc} */
    public String className() {
        return "JaxoVertexT1";
    }

    /** {@inheritDoc} */
    public void testJaxo20Compat() {
        JaxoVertexT1 object =
                (JaxoVertexT1) instanceFrom20OpenGraph(JaxoVertexT1.class);
        assertNotNull(object);
        check20Object("opened wrong object!", object);

        object =
                (JaxoVertexT1) instanceFrom20ImportGraph(JaxoVertexT1.class);
        assertNotNull(object);
        check20Object("imported wrong object!", object);
    }

    private void check20Object(String message, JaxoVertexT1 object) {
        assertEquals(message, 465, object.getX());
        assertEquals(message, 177, object.getY());
        assertEquals(message, 6, (int) Math.round(object.getRadius()));

        assertEquals(message, JaxoColor.PLUM, object.getColor());
    }
}
