// 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 General Public License (GPL): http://www.gnu.org/licenses/
package graphtea.plugins.main.select;

import graphtea.graph.atributeset.GraphAttrSet;
import graphtea.graph.graph.*;
import graphtea.platform.core.BlackBoard;
import graphtea.plugins.main.GraphData;
import graphtea.plugins.main.core.actions.vertex.DeleteVertex;
import graphtea.plugins.main.extension.GraphActionExtension;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * User: root
 */
public class DeleteSelected implements GraphActionExtension {
    public static final String SELECTION_DELETED = "selection deleted";
    public BlackBoard blackboard;

    /**
     * constructor
     *
     * @param bb the blackboard of the action
     */
    public DeleteSelected(BlackBoard bb) {
        this.blackboard = bb;
        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventPostProcessor(e -> {
            AbstractGraphRenderer gv = blackboard.getData(AbstractGraphRenderer.EVENT_KEY);
            Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
            if (gv == focusOwner) {
                if (e.getKeyCode() == KeyEvent.VK_DELETE) {
                    action(new GraphData(blackboard));
                    return true;
                }
            }
            return false;
        });

    }

    @Override
    public String getName() {
        return "Delete Selection";
    }

    @Override
    public String getDescription() {
        return "Deletes the selected vertices and edges";
    }

    @Override
    public void action(GraphData graphData) {
        SubGraph selection = Select.getSelection(blackboard);
        GraphModel g = blackboard.getData(GraphAttrSet.name);
        if (selection.edges.isEmpty() && selection.vertices.isEmpty())
            return;

        HashSet<Edge> edges = new HashSet<>(selection.edges);
        HashSet<Vertex> vertices = new HashSet<>(selection.vertices);
        selection.edges.forEach(g::removeEdge);
        List<Edge> ed = new ArrayList<>();
        for (Vertex v : selection.vertices) {
            for (Edge e : g.edges(v)) {
                ed.add(e);
            }
//            for (Edge e : v.control)
//                ed.add(e);
            DeleteVertex.doJob(g, v);
        }
        blackboard.setData(Select.EVENT_KEY, new SubGraph());

        ClearSelection.clearSelected(blackboard);

    }

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