package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.anomalydetectors;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map.Entry;

import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
import weka.classifiers.trees.J48;
import weka.clusterers.HierarchicalClusterer;
import weka.clusterers.SimpleKMeans;
import weka.core.EuclideanDistance;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.SelectedTag;

public class HierachicalClusterAD extends BaseAnomalyDetector {

	/**
	 * Classifier
	 */
	private HierarchicalClusterer clusterer;
	
	private int NUMBER_OF_CLUSTERS = 16;
	
	public HierachicalClusterAD() {
		super();
		clusterer = new HierarchicalClusterer();
		clusterer.setDistanceFunction(new EuclideanDistance());
		clusterer.setNumClusters(NUMBER_OF_CLUSTERS);
	}
	/**
	 * Try to classify the given packets and detect anomalies
	 * @param packets packets to be classified
	 */
	@Override
	public boolean classifyInstance(Instance instance) throws Exception {

		int x = clusterer.clusterInstance(instance);
		
		//Instance center = clusterer.get
		//Instance stdev = clusterer.getClusterStandardDevs().get(x);
		
		//double distCenterInstance = clusterer.getDistanceFunction().distance(center, instance);
		//double distCenterStdev = clusterer.getDistanceFunction().distance(center, stdev);
		
		//if(distCenterInstance > distCenterStdev) {
			//return true;
		//}
		
		return false;		
	}
	
	/**
	 * Train the model using the given instances
	 * @param instances training set, which should be learned
	 * 
	 */
	@Override
	public void trainModel(Instances instances) {
		try {
			//instances.setClassIndex(1);
			clusterer.buildClusterer(instances);
			
		} catch (Exception e) {
			System.out.println("HC failed");
			e.printStackTrace();
		}
	}
	

	@Override
	public void saveModel() throws Exception {
		weka.core.SerializationHelper.write("./data/models/HC", clusterer);
	}
}
