package armory.logicnode;

class MathNode extends LogicNode {

	public var property0: String; // Operation
	public var property1: String; // Clamp

	public function new(tree: LogicTree) {
		super(tree);
	}

	override function get(from: Int): Dynamic {
		var r = 0.0;
		switch (property0) {
			case "Sine":
				r = Math.sin(inputs[0].get());
			case "Cosine":
				r = Math.cos(inputs[0].get());
			case "Abs":
				r = Math.abs(inputs[0].get());
			case "Tangent":
				r = Math.tan(inputs[0].get());
			case "Arcsine":
				r = Math.asin(inputs[0].get());
			case "Arccosine":
				r = Math.acos(inputs[0].get());
			case "Arctangent":
				r = Math.atan(inputs[0].get());
			case "Logarithm":
				r = Math.log(inputs[0].get());
			case "Round":
				r = Math.round(inputs[0].get());
			case "Floor":
				r = Math.floor(inputs[0].get());
			case "Ceil":
				r = Math.ceil(inputs[0].get());
			case "Fract":
				var v = inputs[0].get();
				r = v - Std.int(v);
			case "Square Root":
				r = Math.sqrt(inputs[0].get());
			case "Exp":
				r = Math.exp(inputs[0].get());
			case "Max":
				r = Math.max(inputs[0].get(), inputs[1].get());
			case "Min":
				r = Math.min(inputs[0].get(), inputs[1].get());
			case "Power":
				r = Math.pow(inputs[0].get(), inputs[1].get());
			case "Less Than":
				r = inputs[0].get() < inputs[1].get() ? 1.0 : 0.0;
			case "Greater Than":
				r = inputs[0].get() > inputs[1].get() ? 1.0 : 0.0;
			case "Modulo":
				r = inputs[0].get() % inputs[1].get();
			case "Arctan2":
				r = Math.atan2(inputs[0].get(), inputs[1].get());
			case "Add":
				for (inp in inputs) r += inp.get();
			case "Multiply":
				r = inputs[0].get();
				var i = 1;
				while (i < inputs.length) {
					r *= inputs[i].get();
					i++;
				}
			case "Subtract":
				r = inputs[0].get();
				var i = 1;
				while (i < inputs.length) {
					r -= inputs[i].get();
					i++;
				}
			case "Divide":
				r = inputs[0].get();
				var i = 1;
				while (i < inputs.length) {
					r /= inputs[i].get();
					i++;
				}
			}
		// Clamp
		if (property1 == "true") r = r < 0.0 ? 0.0 : (r > 1.0 ? 1.0 : r);

		return r;
	}
}