package edu.claflin.finder.algo.clustering.struct.louvain_struct;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import edu.claflin.finder.io.graph.SimpleGraphIO;
import edu.claflin.finder.logic.Graph;
import edu.claflin.finder.logic.Node;

// Standalone tester class for the louvain algorithm

public class Tester {
	public static void main(String[] args) {
		String fname = "ClusterMedium.txt";
		SimpleGraphIO sgio = new SimpleGraphIO();
		Graph g = sgio.parseGraph(new File("./Test Graphs/" + fname), true);
		
		LouvainAlgorithm louvain = new LouvainAlgorithm(g);
		
		System.out.println(communitiesToString(louvain.getCommunities(), g));
	}
	
	public static String arrayToString(int[] array) {
		StringBuilder sb = new StringBuilder();
		sb.append("[");
		
		for(int i = 0; i < array.length; i++) {
			sb.append(String.format("%2d", array[i]) + ", ");
		}
		
		sb.delete(sb.length() - 2, sb.length());
		sb.append("]");
		
		return sb.toString();
	}
	
	public static String arrayToString(double[] array) {
		StringBuilder sb = new StringBuilder();
		sb.append("[");
		
		for(int i = 0; i < array.length; i++) {
			sb.append(String.format("%2.2f", array[i]) + ", ");
		}
		
		sb.delete(sb.length() - 2, sb.length());
		sb.append("]");
		
		return sb.toString();
	}
	
	public static String groupMapToString(HashMap<Integer, ArrayList<Integer>> groupMap, Graph graph) {
		StringBuilder sb = new StringBuilder();
		
		for(int key : groupMap.keySet()) {
			sb.append("Group " + key + ": {");
			
			for(int member : groupMap.get(key)) {
				sb.append(graph.getNodeList().get(member).getIdentifier() + ", ");
			}
			
			sb.delete(sb.length() - 2, sb.length());
			sb.append("}\n");
		}
		
		return sb.toString();
	}
	
	public static String communitiesToString(HashMap<Integer, List<Node>> communities, Graph graph) {
		StringBuilder sb = new StringBuilder();
		
		for(Integer key : communities.keySet()) {
			sb.append("Community " + key + ": {");
			
			for(Node node : communities.get(key)) {
				sb.append(node.getIdentifier() + ", ");
			}
			
			sb.delete(sb.length() - 2, sb.length());
			sb.append("}, Nodes: ");
			sb.append(communities.get(key).size());
			
			sb.append(", Edges: ");
			sb.append(graph.getEdgesBack(communities.get(key)).size() + "\n");
		}
		
		return sb.toString();
	}
	
	public static String labelToGroupArray(int groupLabel, int[] groupVector, Graph graph) {
		StringBuilder sb = new StringBuilder();
		sb.append("[");
		
		for(int i = 0; i < groupVector.length; i++) {
			if(groupVector[i] == groupLabel) {
				sb.append(graph.getNodeList() + ", ");
			}
		}
		
		sb.delete(sb.length() - 2, sb.length());
		sb.append("]\n");
		
		return sb.toString();
	}
	
	public static String groupToString(int[] groupVector, int groupLabel, Graph graph) {
		StringBuilder sb = new StringBuilder();
		sb.append("{");
		
		for(int i = 0; i < groupVector.length; i++) {
			if(groupVector[i] == groupLabel) {
				sb.append(graph.getNodeList().get(i).getIdentifier() + ", ");
			}
		}
		
		sb.delete(sb.length() - 2, sb.length());
		sb.append("}");
		
		return sb.toString();
	}
	
	public static String groupToString(HashMap<Integer, ArrayList<Integer>> groupMap, int groupLabel, Graph graph) {
		StringBuilder sb = new StringBuilder();
		sb.append("{");
		
		for(Integer member : groupMap.get(groupLabel)) {
			sb.append(graph.getNodeList().get(member).getIdentifier() + ", ");
		}
		
		sb.delete(sb.length() - 2, sb.length());
		sb.append("}");
		
		return sb.toString();
	}
}
