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

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

//import org.opencv.core.DMatch;
import org.opencv.core.Mat;
import org.opencv.core.MatOfDMatch;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.core.Size;
import org.opencv.features2d.DMatch;
import org.opencv.features2d.DescriptorExtractor;
import org.opencv.features2d.DescriptorMatcher;
import org.opencv.features2d.FeatureDetector;
//import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class ContourMatcher implements IMatcher {
	private double multiplicator=0.4;	//0.4
	@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) {
		FeatureDetector Fdetector = FeatureDetector.create(FeatureDetector.ORB);
		DescriptorExtractor Fdescriptor = DescriptorExtractor.create(DescriptorExtractor.ORB);
		DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
		for (Template detection :listOfDetections) {
			MatOfKeyPoint detectionKeyPoints=new MatOfKeyPoint();
			Mat greyDetectionImage = new Mat();
			Imgproc.cvtColor(detection.getImage(), greyDetectionImage,Imgproc.COLOR_RGB2GRAY);			
			Mat detectionDescriptors=new Mat();
			Fdetector.detect(greyDetectionImage, detectionKeyPoints);
			Fdescriptor.compute(greyDetectionImage, detectionKeyPoints, detectionDescriptors);
			int numberOfGoodMatches=0;
			int idGoodMatch=200;
			for (Template template : listOfTemplates) {
				int matchesOfTemplate=0;
				MatOfKeyPoint templateKeyPoints=new MatOfKeyPoint();
				Mat templateDescriptors=new Mat();	
				Mat greyTemplateImage = new Mat();
				Imgproc.cvtColor(template.getImage(), greyTemplateImage,Imgproc.COLOR_RGB2GRAY);
				Fdetector.detect(greyTemplateImage, templateKeyPoints);
				Fdescriptor.compute(greyTemplateImage, templateKeyPoints, templateDescriptors);
				MatOfDMatch matches=new MatOfDMatch();

				if (detectionDescriptors.cols() <= templateDescriptors.cols()) {
					matcher.match(detectionDescriptors,templateDescriptors , matches);
				} else {
					matcher.match(templateDescriptors, detectionDescriptors, matches);
				}
				List<DMatch> matchlist=matches.toList();
				for (DMatch dMatch : matchlist) {
					if (dMatch.distance<64) {
							matchesOfTemplate++;
					}
						//System.out.println("distance of countour matcher: "+dMatch.distance);
				}
				if (matchesOfTemplate>numberOfGoodMatches) {
					numberOfGoodMatches=matchesOfTemplate;
					idGoodMatch=listOfTemplates.indexOf(template);
				}	
			}
			if (idGoodMatch!=200) {
				//detection.setIdOfContourMatcher(idGoodMatch);
				assignmentArray[listOfDetections.indexOf(detection)][idGoodMatch]+=1*multiplicator;
				detection.setNumberOfGoodContours(numberOfGoodMatches);
			}

			}
			
			
		}
		
	}


