package cz.vutbr.feec.utko.objectTracking.example;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Vector;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfFloat;
import org.opencv.core.MatOfInt;
import org.opencv.imgproc.Imgproc;

public class ColorMatcher implements IMatcher {	
	private double multiplicator=0.9;	//0.9
	@Override
	public void setMultiplicator(double multiplicator) {
		this.multiplicator = multiplicator;
	}
	public double getMultiplicator() {
		return multiplicator;
	}
	public void match(List<Template> listOfTemplates, List<Template> listOfDetections,double[][] assignmentArray) {
		// TODO Auto-generated method stub
		for (Template detection : listOfDetections) {
//			Mat detectionHistogram=detection.histogram();
			Mat detectionHistogram=detection.getColorHistogram();
			int IdGoodMatch=200;
			double nearestComparator=200;
			for (Template template : listOfTemplates) {
				Mat templateHistogram= template.histogram();
				//Mat templateHistogram= template.getColorHistogram();
				/*System.out.println(detectionHistogram.cols()+" "+detectionHistogram.rows());
				System.out.println(templateHistogram.cols()+" "+templateHistogram.rows());*/
				double comparator=Imgproc.compareHist(templateHistogram, detectionHistogram, Imgproc.CV_COMP_BHATTACHARYYA);
				
				//System.out.println(comparator);
				if (comparator<nearestComparator) {
					nearestComparator=comparator;
					IdGoodMatch=listOfTemplates.indexOf(template);
				}
				
			}
			//	detection.setIdOfColorMatcher(IdGoodMatch);
			assignmentArray[listOfDetections.indexOf(detection)][IdGoodMatch]+=1*multiplicator;
			detection.setBhattacharyyaCoeficient(nearestComparator);
		}
		/*Collections.sort(listOfDetections, new Comparator<Template>() {
		    @Override
		    public int compare(Template lhs, Template rhs) {
		        // -1 - less than, 1 - greater than, 0 - equal, all inversed for descending
		        return lhs.getIdOfColorMatcher() > rhs.getIdOfColorMatcher() ? -1 : (lhs.getIdOfColorMatcher() < rhs.getIdOfColorMatcher()) ? 1 : 0;
		    }
		});*/
		
	}

}
