package de.hhu.ba.yoshikoWrapper.gui;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.border.Border;

import org.cytoscape.model.CyNetwork;
import org.cytoscape.model.CyNode;
import org.cytoscape.model.CyRow;
import org.cytoscape.view.model.CyNetworkView;

import de.hhu.ba.yoshikoWrapper.core.CyCore;
import de.hhu.ba.yoshikoWrapper.core.LocalizationManager;

@SuppressWarnings("serial")
public class ClusterView extends ComfortPanel {
	
	private final JLabel title;
	private final JLabel clusterSize;
	
	private final CyNetworkView clusterView;
	
	private CyNetwork clusterSubNet;
	
	private final Border regularBorder = BorderFactory.createLineBorder(Color.GRAY);
	private final Border highlightBorder = BorderFactory.createLineBorder(Color.RED);
	
	//SYMBOLIC LINKS
	
	/**Simple array list containing references to the nodes that are represented by this cluster view
	 * 
	 */
	private ArrayList<CyNode> nodes;
	
	public ClusterView() {
		//Swing init
		this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		
		title = new JLabel("CLUSTERTITLE");
		clusterSize = new JLabel("CLUSTERLABEL");
		
		this.clusterSubNet = CyCore.networkFactory.createNetwork();
		this.clusterView = CyCore.networkViewFactory.createNetworkView(this.clusterSubNet); 
		
		this.addAll(title,clusterSize);
		this.add(Box.createVerticalStrut(4));
		
		this.addMouseListener(mouseListener);
		
		this.setBorder(regularBorder);
		//Other init
		nodes = new ArrayList<CyNode>();
	}
	
	public void addNode(CyNode node) {
		this.add(
			new JLabel(
				CyCore.cy.getCurrentNetwork().getRow(node).get("name", String.class)
			)
		);
		//Add symbolic link
		nodes.add(node);
	}
	
	public void setTitle(long x) {
		this.title.setText(LocalizationManager.get("cluster")+" "+x);
	}

	public void setClusterSize(long x) {
		this.clusterSize.setText(LocalizationManager.get("clusterSize")+" "+x);
	}


	
	private MouseListener mouseListener = new MouseListener() {

		@Override
		public void mouseClicked(MouseEvent e) {}

		@Override
		public void mousePressed(MouseEvent e) {
			//Undo previous selection
			
			List<CyRow> allRows = CyCore.cy.getCurrentNetwork().getDefaultNodeTable().getAllRows();
			for (CyRow r: allRows) {
				r.set("selected", false);
			}
			//Select nodes corresponding to the cluster
			for (CyNode n : nodes) {
				CyCore.cy.getCurrentNetwork().getRow(n).set("selected", true);
			}
		}
		
		@Override
		public void mouseReleased(MouseEvent e) {}

		@Override
		public void mouseEntered(MouseEvent e) {
			setBorder(highlightBorder);
		}

		@Override
		public void mouseExited(MouseEvent e) {
			setBorder(regularBorder);
		}


	};
}