package com.keba.tracecompass.currentctx.ui;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;

public class TaskPropertiesProvider {
	
	private HashMap<String, ArrayList<TaskProperty>> properties;
	private TreeMap<String, Integer> catPrecedences;
	
	public TaskPropertiesProvider() {
		properties = new HashMap<>();
		catPrecedences = new TreeMap<>();
	}
	
	public void addCategory(String category, int precedence) {
		Integer isCat = catPrecedences.get(category);
		if (isCat == null) {
			catPrecedences.put(category, precedence);
			ArrayList<TaskProperty> ltp = new ArrayList<>();
			properties.put(category, ltp);
		}
	}
	
	public void clearCategories() {
		catPrecedences.clear();
		properties.clear();
	}
	
	public void clearProperties() {
		properties.clear();
		
		for (Map.Entry<String, Integer> entry : catPrecedences.entrySet()) {
			ArrayList<TaskProperty> ltp = new ArrayList<>();
			properties.put(entry.getKey(), ltp);
		}
	}
	
	public void clearPropertiesOfCategory(String category) {
		properties.remove(category);
		ArrayList<TaskProperty> ltp = new ArrayList<>();
		properties.put(category, ltp);
	}
	
	public Object [] getPrecedenceContent () {
		ArrayList<TaskProperty> ltp = new ArrayList<>();
		
		//
		SortedSet<Map.Entry<String,Integer>> sortedEntries = new TreeSet<Map.Entry<String,Integer>>(
		        new Comparator<Map.Entry<String,Integer>>() {
					@Override
					public int compare(Entry<String, Integer> arg0, Entry<String, Integer> arg1) {
						return arg0.getValue().compareTo(arg1.getValue());
					}
		}); 
		sortedEntries.addAll(catPrecedences.entrySet());
		
		for (Map.Entry<String, Integer> entry : sortedEntries) {
			ArrayList<TaskProperty> ll = properties.get(entry.getKey());
			if (ll != null) {
				ltp.addAll(ll);
			}
		}
		
		return ltp.toArray();
	}
	
	public Object [] getContent () {
		ArrayList<TaskProperty> ltp = new ArrayList<>();
		for (Map.Entry<String, ArrayList<TaskProperty>> entry : properties.entrySet()) {
			ltp.addAll(entry.getValue());
		}
		return ltp.toArray();
	}
	
	public void addProperty(String category, TaskProperty tp) {
		ArrayList<TaskProperty> ltp = properties.get(category);
		if (ltp != null) {
			ltp.add(tp);
		}
	}
}

@@ -528,32 +528,108 @@
 ap%3C%3E();%0D%0A%09%7D%0D%0A%09%0D%0A
+%09/* %0D%0A%09 * Adds a Category. Necessary prerequisite to add a property %0D%0A%09 */%0D%0A
 %09public void add
@@ -673,16 +673,16 @@
 nce) %7B%0D%0A
-
 %09%09Intege
@@ -884,32 +884,85 @@
 );%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%09%0D%0A
+%09/*%0D%0A%09 * Clear all categories and properties.%0D%0A%09 */%0D%0A
 %09public void cle
@@ -1028,32 +1028,102 @@
 lear();%0D%0A%09%7D%0D%0A%09%0D%0A
+%09/* %0D%0A%09 * Clear all Properties, but leave categories in place %0D%0A%09 */%0D%0A
 %09public void cle
@@ -1337,32 +1337,93 @@
 );%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%09%0D%0A
+%09/* %0D%0A%09 * Clear all Properties of a certain category.%0D%0A%09 */%0D%0A
 %09public void cle
@@ -1582,32 +1582,106 @@
 , ltp);%0D%0A%09%7D%0D%0A%09%0D%0A
+%09/*%0D%0A%09 * Get all properties sorted by their categories precedence %0D%0A%09 */%0D%0A
 %09public Object %5B
@@ -2362,32 +2362,78 @@
 rray();%0D%0A%09%7D%0D%0A%09%0D%0A
+%09/* %0D%0A%09 * Get all properties unsorted %0D%0A%09 */%0D%0A
 %09public Object %5B
@@ -2655,19 +2655,198 @@
 );%0D%0A%09%7D%0D%0A
-
 %09%0D%0A
+%09/*%0D%0A%09 * Add a property. Every Property is part of a category. A category must be%0D%0A%09 * created first with addCategory. Otherwise this method fails without further notice. %0D%0A%09 */%0D%0A
 %09public 

Add some comments