// GraphTea Project: http://github.com/graphtheorysoftware/GraphTea
// Copyright (C) 2012 Graph Theory Software Foundation: http://GraphTheorySoftware.com
// Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
// Distributed under the terms of the GNU Lesser General Public License (LGPL): http://www.gnu.org/licenses/

package graphtea.extensions.actions;

import graphtea.graph.graph.Edge;
import graphtea.graph.graph.GraphModel;
import graphtea.graph.graph.Vertex;
import graphtea.plugins.main.GraphData;
import graphtea.plugins.main.extension.GraphActionExtension;

/**
 * Creates a line graph from the current graph and shows it in a new tab
 *
 * @author Mohammad Ali Rostami
 * @author Azin Azadi
 */
public class ComplementGraph implements GraphActionExtension {

    public void action(GraphData graphData) {

        GraphModel g1 = graphData.getGraph();
        GraphModel g2 = new GraphModel(false);//

        for(Vertex v : g1.getVertexArray()) {
            Vertex tmp = new Vertex();
            tmp.setLocation(v.getLocation());
            g2.addVertex(tmp);
        }


       Vertex[] verts = g1.getVertexArray();
       for (int i = 0; i < verts.length; i++) {
           for (int j = i + 1; j < verts.length; j++) {
               // Use i < j so each unordered pair is visited exactly once,
               // avoiding duplicate edges in the (always-undirected) complement.
               if (!g1.isEdge(verts[i], verts[j])) {
                   g2.addEdge(new Edge(g2.getVertex(verts[i].getId()),
                           g2.getVertex(verts[j].getId())));
               }
           }
       }

       graphData.core.showGraph(g2);
    }

    public String getName() {
        return "Complement Graph";
    }

    public String getDescription() {
        return "Makes a graph including those edges which" +
                "are not existent in the initial graph.";
    }

    @Override
    public String getCategory() {
        return "Transformations";
    }
}
