/*******************************************************************************
 * Caleydo - Visualization for Molecular Biology - http://caleydo.org
 * Copyright (c) The Caleydo Team. All rights reserved.
 * Licensed under the new BSD license, available at http://caleydo.org/license
 *******************************************************************************/
package v2.data;

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

import org.caleydo.core.data.collection.EDimension;
import org.caleydo.core.data.perspective.variable.Perspective;
import org.caleydo.core.util.color.Color;
import org.caleydo.core.util.function.Function2;
import org.caleydo.core.view.opengl.layout2.manage.GLElementFactoryContext.Builder;
import org.caleydo.view.domino.api.model.typed.TypedCollections;
import org.caleydo.view.domino.api.model.typed.TypedGroupList;
import org.caleydo.view.domino.api.model.typed.TypedGroupSet;
import org.caleydo.view.domino.api.model.typed.TypedList;
import org.caleydo.view.domino.api.model.typed.TypedListGroup;
import org.caleydo.view.domino.api.model.typed.TypedSet;
import org.caleydo.view.domino.api.model.typed.TypedSetGroup;
import org.caleydo.view.domino.internal.util.Utils;

import com.google.common.collect.ImmutableList;

/**
 * @author Samuel Gratzl
 *
 */
public class StratificationDataValue implements IDataValues, Function2<Integer, Integer, Color> {
	private final String label;
	private final EDimension main;

	private final TypedGroupSet singleGroup;
	private final TypedGroupSet groups;


	public StratificationDataValue(Perspective data, EDimension dim, Integer referenceId) {
		this.main = dim;
		this.label = data.getLabel();
		this.singleGroup = new TypedGroupSet(TypedGroupList.createUngroupedGroup(new TypedSet(Collections
				.singleton(TypedCollections.INVALID_ID), TypedCollections.INVALID_IDTYPE)));
		this.groups = new TypedGroupSet(Utils.extractSetGroups(data, referenceId, dim));
	}

	public StratificationDataValue(String label, TypedSet data, EDimension main) {
		this.main = main;
		this.label = label;
		this.singleGroup = new TypedGroupSet(TypedGroupList.createUngroupedGroup(new TypedSet(Collections
				.singleton(TypedCollections.INVALID_ID), TypedCollections.INVALID_IDTYPE)));
		this.groups = new TypedGroupSet(TypedGroupList.createUngroupedGroup(data));
	}


	@Override
	public String getExtensionID() {
		return "stratification";
	}

	@Override
	public void fill(Builder b, TypedListGroup dimData, TypedListGroup recData) {
		final EDimension dimension = main;
		b.put(EDimension.class, dimension);
		final TypedList data = dimension.select(dimData, recData);
		final List<Integer> op = ImmutableList.of(0);
		String prim = dimension.select("dimensions", "records");
		String sec = dimension.select("records", "dimensions");
		b.put("heatmap." + prim, data);
		b.put("heatmap." + prim + ".idType", groups.getIdType());
		b.put("heatmap." + sec, op);
		b.put("heatmap." + sec + ".idType", TypedCollections.INVALID_IDTYPE);
		b.put(Function2.class, this);
	}

	@Override
	public Color apply(Integer record, Integer dimension) {
		Integer id = main.select(dimension, record);
		for (TypedSetGroup g : groups.getGroups()) {
			if (g.contains(id))
				return g.getColor();
		}
		return Color.NOT_A_NUMBER_COLOR;
	}

	@Override
	public TypedGroupSet getDefaultGroups(EDimension dim) {
		return dim == main ? groups : singleGroup;
	}

	@Override
	public String getLabel() {
		return label;
	}

	@Override
	public int compare(EDimension dim, int a, int b, TypedSet otherData) {
		return a - b;
	}

}

@@ -412,31 +412,8 @@
 ons;
-%0Aimport java.util.List;
 %0A%0Aim
@@ -894,66 +894,8 @@
 et;%0A
-import org.caleydo.view.domino.api.model.typed.TypedList;%0A
 impo
@@ -1129,57 +1129,8 @@
 s;%0A%0A
-import com.google.common.collect.ImmutableList;%0A%0A
 /**%0A
@@ -2368,304 +2368,102 @@
 %7B%0A%09%09
-final EDimension dimension = main;%0A%09%09b.put(EDimension.class, dimension);%0A%09%09final TypedList data = dimension.select(dimData, recData);%0A%09%09final List%3CInteger%3E op = ImmutableList.of(0);%0A%09%09String prim = dimension.select(%22dimensions%22, %22records%22);%0A%09%09String sec = dimension.select(%22records%22, %22dimensions%22
+b.put(%22heatmap.dimensions%22, dimData);%0A%09%09b.put(%22heatmap.dimensions.idType%22, dimData.getIdType()
 );%0A%09
@@ -2482,19 +2482,22 @@
 map.
-%22 + prim, d
+records%22, recD
 ata)
@@ -2519,20 +2519,15 @@
 map.
-%22 + prim + %22
+records
 .idT
@@ -2532,22 +2532,23 @@
 dType%22, 
-groups
+recData
 .getIdTy
@@ -2560,109 +2560,161 @@
 ;%0A%09%09
-b.put(%22heatmap.%22 + sec, op);%0A%09%09b.put(%22heatmap.%22 + sec + %22.idType%22, TypedCollections.INVALID_IDTYPE);%0A
+if (dimData.getIdType() != getDefaultGroups(EDimension.DIMENSION).getIdType()) %7B // swapped%0A%09%09%09b.put(Function2.class, Functions2s.swap(this));%0A%09%09%7D else%0A%09
 %09%09b.

fix transpose of stratification