package com.elphel.imagej.orthomosaic;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

public class ItemMatch {
	public double [] combo_pxy; // combo image {pixel_x, pixel_y}
	public double [] lla; // add later
	public HashMap<String, ItemPatternMatch> pattern_matches = new HashMap<String, ItemPatternMatch>();
	
	public ItemMatch (double [] combo_pxy) {
		this.combo_pxy = combo_pxy;
	}
	
	public double [] getXY() {
		return combo_pxy;
	}
	public int [] getIntXY() {
		return new int [] {(int) Math.round(combo_pxy[0]), (int) Math.round(combo_pxy[1])};
	}
	
	public void addPatternMatches(
			GroundObjectPattern groundObjectPattern,
			double [] matches,
			int      best_sub) {
		String pattern_path = groundObjectPattern.getPatternPath();
		ItemPatternMatch match = pattern_matches.get(pattern_path);
		if (match == null) {
			match = new ItemPatternMatch(groundObjectPattern);
			pattern_matches.put(pattern_path, match);
		}
		match.setMatches(matches);
		match.setBestSub(best_sub);
	}
	
	public ItemPatternMatch getPatternMatch(GroundObjectPattern groundObjectPattern) {
		return getPatternMatch(groundObjectPattern.getPatternPath());
	}
	
	public ItemPatternMatch getPatternMatch(String pattern_path) {
		return pattern_matches.get(pattern_path);
	}
	
	public double [] getMatchValues(String pattern_path) {
		ItemPatternMatch match = pattern_matches.get(pattern_path);
		if (match == null) {
			System.out.println("No matches found for pattern "+pattern_path);
			return null;
		}
		return match.getMatches();
	}
	public double [] getMatchValues(GroundObjectPattern groundObjectPattern) {
		return getMatchValues(groundObjectPattern.getPatternPath());
	}
	
	public double getMatchValue(String pattern_path, int indx) {
		ItemPatternMatch match = pattern_matches.get(pattern_path);
		if (match == null) {
			System.out.println("No matches found for pattern "+pattern_path);
			return Double.NaN;
		}
		return match.getMatch(indx);
	}
	
	public double getMatchValue(GroundObjectPattern groundObjectPattern, int indx) {
		return getMatchValue(groundObjectPattern.getPatternPath(), indx);
	}
	
	public static ArrayList<Integer> sortByMatch(
			ArrayList<ItemMatch> match_list,
			GroundObjectPattern groundObjectPattern,
			int indx){
		String pattern_path = groundObjectPattern.getPatternPath();
		return sortByMatch(match_list, pattern_path, indx);
	}
	
	public static ArrayList<Integer> sortByMatch(
			ArrayList<ItemMatch> match_list,
			String pattern_path,
			int indx){
		ArrayList<Integer> result_list= new ArrayList<Integer>(match_list.size());
		for (int i = 0; i < match_list.size(); i++) {
			result_list.add(i);
		}
		Collections.sort(result_list, new Comparator<Integer>() {
			@Override
			public int compare(Integer lhs, Integer rhs) {
				double rhsd = match_list.get(rhs).getMatchValue(pattern_path, indx);
				double lhsd = match_list.get(lhs).getMatchValue(pattern_path, indx);
				return (rhsd > lhsd) ? 1 : (rhsd < lhsd) ? -1 : 0; // recreasing
			}
		});
		return result_list;
	}
	
}
