/*******************************************************************************
 * 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.vis.lineup.ui.mapping;

import org.caleydo.vis.lineup.model.mapping.IMappingFunction;
import org.caleydo.vis.lineup.model.mapping.PiecewiseMapping;

/**
 * @author Samuel Gratzl
 *
 */
public enum EStandardMappings{
	LINEAR, INVERT, ABS, Z_SCORE, LINEAR_LOG;

	public void apply(IMappingFunction mapping) {
		switch (this) {
		case LINEAR:
			if (mapping instanceof PiecewiseMapping) {
				PiecewiseMapping p = (PiecewiseMapping) mapping;
				p.clear();
				p.put(mapping.getActMin(), 0);
				p.put(mapping.getActMax(), 1);
			} else {
				mapping.fromJavaScript("linear(value_min, value_max, value, 0, 1)");
			}
			return;
		case INVERT:
			if (mapping instanceof PiecewiseMapping) {
				PiecewiseMapping p = (PiecewiseMapping) mapping;
				p.clear();
				p.put(mapping.getActMin(), 1);
				p.put(mapping.getActMax(), 0);
			} else {
				mapping.fromJavaScript("linear(0, 1, value, 1, 0)");
			}
			return;
		case ABS:
			if (mapping instanceof PiecewiseMapping) {
				PiecewiseMapping p = (PiecewiseMapping) mapping;
				p.clear();
				p.put(mapping.getActMin(), 1);
				p.put((mapping.getActMax() + mapping.getActMin()) * 0.5f, 0);
				p.put(mapping.getActMax(), 1);
			} else {
				mapping.fromJavaScript("linear(0, abs(value_min, value_max), abs(value), 0, 1)");
			}
			return;
		case Z_SCORE:
			mapping.fromJavaScript("((value - data.mean) / data.sd) + 0.5");
			return;
		case LINEAR_LOG:
			mapping.fromJavaScript("linear(0, log(value_max), log(value), 0, 1)");
			return;
		}
		throw new IllegalStateException();
	}

	@Override
	public String toString() {
		switch (this) {
		case LINEAR:
			return "Linear";
		case INVERT:
			return "Invert";
		case ABS:
			return "Abs";
		case Z_SCORE:
			return "Z-Score";
		case LINEAR_LOG:
			return "Log";
		}
		throw new IllegalStateException();
	}
}

@@ -627,23 +627,16 @@
 _SCORE, 
-LINEAR_
 LOG;%0A%0A%09p
@@ -1752,39 +1752,32 @@
 %09return;%0A%09%09case 
-LINEAR_
 LOG:%0A%09%09%09mapping.
@@ -1803,15 +1803,61 @@
 ear(
-0, 
 log(
+Math.max(10E-10,value_min)), log(Math.max(10E-10,
 valu
@@ -1866,20 +1866,38 @@
 max)
+)
 , log(
+Math.max(10E-10,
 value)
+)
 , 0,
@@ -2163,15 +2163,8 @@
 ase 
-LINEAR_
 LOG:

fix #1101: log mapping fails for 0 -> same workaround as for p-value: max(10E-10,value)