/*******************************************************************************
 * 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 org.caleydo.view.domino.internal.tourguide;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.caleydo.core.data.collection.EDataClass;
import org.caleydo.core.data.datadomain.ATableBasedDataDomain;
import org.caleydo.core.data.datadomain.DataSupportDefinitions;
import org.caleydo.core.data.datadomain.IDataDomain;
import org.caleydo.core.id.IDCategory;
import org.caleydo.core.view.opengl.layout2.renderer.GLRenderers;
import org.caleydo.view.domino.internal.tourguide.ui.EntityTypeSelector;
import org.caleydo.view.tourguide.api.adapter.ATourGuideDataMode;
import org.caleydo.view.tourguide.api.model.ADataDomainQuery;
import org.caleydo.view.tourguide.api.model.AScoreRow;
import org.caleydo.view.tourguide.api.model.InhomogenousDataDomainQuery;
import org.caleydo.vis.lineup.model.CategoricalRankColumnModel;
import org.caleydo.vis.lineup.model.IRow;
import org.caleydo.vis.lineup.model.RankTableModel;
import org.caleydo.vis.lineup.model.StringRankColumnModel;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;

/**
 * @author Samuel Gratzl
 *
 */
public class DataTourGuideDataMode extends ATourGuideDataMode {

	@Override
	public Iterable<? extends ADataDomainQuery> createDataDomainQuery(IDataDomain dd) {
		return Collections.singleton(createFor(dd));
	}

	@Override
	public Iterable<? extends ADataDomainQuery> createDataDomainQueries() {
		return super.createDataDomainQueries(); // , createLabelQueries());
	}

	private Iterable<ADataDomainQuery> createLabelQueries() {
		List<ADataDomainQuery> r = new ArrayList<>();
		for (IDCategory cat : EntityTypeSelector.findAllUsedIDCategories()) {
			r.add(new LabelDataDomainQuery(cat));
		}
		return r;
	}

	protected ADataDomainQuery createFor(IDataDomain dd) {
		if (!DataSupportDefinitions.homogenousTables.apply(dd))
			return new InhomogenousDataDomainQuery((ATableBasedDataDomain) dd,
 Sets.immutableEnumSet(
					EDataClass.NATURAL_NUMBER, EDataClass.REAL_NUMBER,
							EDataClass.UNIQUE_OBJECT));
		return new DataDomainQuery((ATableBasedDataDomain) dd);
	}

	@Override
	public void addDefaultColumns(RankTableModel table) {
		addDataDomainRankColumn(table);
		final StringRankColumnModel base = new StringRankColumnModel(GLRenderers.drawText("Data Set"),
				StringRankColumnModel.DEFAULT);
		table.add(base);
		base.setWidth(150);
		base.orderByMe();
		table.add(CategoricalRankColumnModel.createSimple(GLRenderers.drawText("Type"), new Function<IRow, String>() {
			@Override
			public String apply(IRow input) {
				return input instanceof DataDomainScoreRow ? "2D" : "1D";
			}

		}, Arrays.asList("1D", "2D")));
		table.add(new SizeRankColumnModel("#Records", new Function<IRow, Integer>() {
			@Override
			public Integer apply(IRow in) {
				return ((AScoreRow) in).size();
			}
		}).setWidth(75));
		table.add(new SizeRankColumnModel("#Dimensions", new Function<IRow, Integer>() {
			@Override
			public Integer apply(IRow in) {
				return Iterables.size(((AScoreRow) in).getDimensionIDs());
			}
		}).setWidth(75));
	}

	@Override
	public boolean apply(IDataDomain input) {
		return input instanceof ATableBasedDataDomain;
	}

}

@@ -519,16 +519,38 @@
 il.List;
+%0Aimport java.util.Set;
 %0A%0Aimport
@@ -1311,24 +1311,93 @@
 model.IRow;%0A
+import org.caleydo.vis.lineup.model.MultiCategoricalRankColumnModel;%0A
 import org.c
@@ -1536,16 +1536,116 @@
 nction;%0A
+import com.google.common.collect.ImmutableSet;%0Aimport com.google.common.collect.ImmutableSortedSet;%0A
 import c
@@ -3252,16 +3252,16 @@
 ;%0A%09%09%09%7D%0A%0A
-
 %09%09%7D, Arr
@@ -3282,24 +3282,819 @@
 %22, %222D%22)));%0A
+%0A%09%09ImmutableSortedSet.Builder%3CString%3E s = ImmutableSortedSet.naturalOrder();%0A%09%09for (IDCategory cat : EntityTypeSelector.findAllUsedIDCategories())%0A%09%09%09s.add(cat.getCategoryName());%0A%09%09table.add(MultiCategoricalRankColumnModel.createSimple(GLRenderers.drawText(%22Entity Type%22), new Function%3CIRow, Set%3CString%3E%3E() %7B%0A%09%09%09@Override%0A%09%09%09public Set%3CString%3E apply(IRow input) %7B%0A%09%09%09%09if (input instanceof DataDomainScoreRow) %7B%0A%09%09%09%09%09IDCategory a = ((DataDomainScoreRow) input).getIdType().getIDCategory();%0A%09%09%09%09%09IDCategory b = ((DataDomainScoreRow) input).getDimensionIdType().getIDCategory();%0A%09%09%09%09%09return ImmutableSet.of(a.getCategoryName(),b.getCategoryName());%0A%09%09%09%09%09%09%7D%0A%09%09%09%09return ImmutableSet.of(((AScoreRow) input).getIdType().getIDCategory().getCategoryName());%0A%09%09%09%7D%0A%0A%09%09%09%09%7D, s.build(), %22%22).setWidth(150));%0A%0A
 %09%09table.add(

fix #87 entity type column in data lineup