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

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

import org.opencv.core.Point;



public class DistanceMatcher implements IMatcher{
	private double multiplicator=1;	//1
	@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) {
			int IdgoodMatch = 200;
			double nearestDistance=1000;			
			Point detectionPoint= new Point();
			detectionPoint.x= (detection.getRect().x+detection.getRect().width)/2;
			detectionPoint.y=(detection.getRect().y+detection.getRect().height)/2;
			for (Template template : listOfTemplates) {
				Point templatePoint=new Point();
				templatePoint.x= (template.getRect().x+template.getRect().width)/2;
				templatePoint.y=(template.getRect().y+template.getRect().height)/2;
				double distance=Math.sqrt(Math.pow(templatePoint.x-detectionPoint.x, 2)+Math.pow(templatePoint.y-detectionPoint.y, 2));
				if (distance<nearestDistance) {
					nearestDistance=distance;
					IdgoodMatch=listOfTemplates.indexOf(template);
				}
			}
				//detection.setIdOfDistanceMatch(IdgoodMatch);
				assignmentArray[listOfDetections.indexOf(detection)][IdgoodMatch]+=1*multiplicator;
				detection.setNearestSizeDistance(nearestDistance);

		}
		/*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.getIdOfDistanceMatch() > rhs.getIdOfDistanceMatch() ? -1 : (lhs.getIdOfDistanceMatch() < rhs.getIdOfDistanceMatch()) ? 1 : 0;
		    }
		});*/
		
	}

}
