// 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.ui;

import graphtea.graph.graph.IndSubGraphs;
import graphtea.graph.graph.Vertex;
import graphtea.platform.Application;
import graphtea.plugins.main.GraphData;
import graphtea.ui.components.gpropertyeditor.GBasicCellRenderer;
import graphtea.ui.components.gpropertyeditor.GCellRenderer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;

/**
 * @author Azin Azadi
 */
public class IndSetColoringRenderer implements GBasicCellRenderer<IndSubGraphs> {
//    public IndSetColoringRenderer() {
//        super();
//        backupColoring();
 //   }
    public Component getRendererComponent(final IndSubGraphs res) {
        String txt;
        GraphData gd = new GraphData(Application.getBlackBoard());
        boolean hasAllVSet = true;

        for(int i=0;i<gd.getGraph().getVerticesCount();i++) {
          if(!res.contains(i)) {
             hasAllVSet = false;
             break;
          }
        }

        StringBuilder sb = new StringBuilder("<HTML><BODY><B>V:</B>{");
        if (hasAllVSet) sb.append("<B>");
        for (int tmp : res) {
            if (tmp == -1) sb.append("},{");
            else sb.append(tmp).append(",");
        }
        if (hasAllVSet) sb.append("</B>");
        sb.append("}<BR></BODY></HTML>");
        txt = sb.toString();
        //System.out.println(txt);
        JLabel l = new JLabel(txt) {
            @Override
            public void setForeground(Color fg) {
                super.setForeground(fg);
                if (fg== GCellRenderer.SELECTED_COLOR)
                    showOnGraph(res);
            }
        };
        l.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                showOnGraph(res);
            }
        }

        );
        return l;
    }

    private void showOnGraph(IndSubGraphs res) {
   //     resetColoring();
        GraphData gd = new GraphData(Application.getBlackBoard());
        //gd.getGraph().
        for(Vertex v : gd.getGraph()) {
            v.setColor(0);
        }
        int color = 2;
        for (Integer re : res) {
            if (re != -1)
                gd.getGraph().getVertex(re).setColor(color);
            else color++;
        }
    }

    private HashMap<Vertex, Integer> vertexColors;

    /**
     * resets and stores all colorings of g
     */
    public void backupColoring() {
        GraphData gd = new GraphData(Application.getBlackBoard());
        vertexColors = new HashMap<>();
        for (Vertex v : gd.getGraph()) {
            vertexColors.put(v, v.getColor());
        }
    }

    public void resetColoring() {
        GraphData gd = new GraphData(Application.getBlackBoard());
        for(Vertex v:vertexColors.keySet()) {
            v.setColor(vertexColors.get(v));
        }

    }

}
