/*
 * Parameter.cpp
 *
 *  Created on: Aug 5, 2015
 *      Author: jjpalacios
 */

#include "ParameterDefinition.h"

namespace clbDEB {

//-----  Main constructor  ----------------------------------------------------
ParameterDefinition::ParameterDefinition() {
	this->name = "";
	this->minValue = -1;
	this->maxValue = -1;
	this->step = 0;

	this->boundsReady = false;

	this->adaptedMin = -1;
	this->adaptedMax = -1;
	this->multiplier = 0;
}


//-----  setValues method  ----------------------------------------------------
int ParameterDefinition::setValues(const double min, const double max,
		const double step) {

	double marginError = 0.00001;
	this->minValue = min;
	this->maxValue = max;
	this->step = step;

	// Eliminate decimal values from the minimum value
	this->multiplier = 1;
	while((multiplier*min) - std::floor(multiplier*min) > marginError)
		multiplier *= 10;

	// Eliminate decimal values from the maximum value
	while((multiplier*max) - std::floor(multiplier*max) > marginError)
		multiplier *= 10;

	this->adaptedMin = multiplier * min;
	this->adaptedMax = multiplier * min + (max-min)/step;
	this->boundsReady = true;
	return this->adaptedMax;
}


//-----  getOriginalValue method  ---------------------------------------------
double ParameterDefinition::getOriginalValue(int x) {
	if(!this->boundsReady) return -1.0;
	double original;

	original = this->adaptedMin + (x-this->adaptedMin)*step*this->multiplier;
	original /= multiplier;

	return original;
}


//-----  getOriginalValue method  ---------------------------------------------
int ParameterDefinition::getIntegerValue(double x) {
	if(!this->boundsReady) return -1;
	int adapted;

	adapted = this->adaptedMin +
			(x-this->minValue)/this->step;

	return adapted;
}


//-----  getMaxValue method  --------------------------------------------------
int ParameterDefinition::getMaxValue() {
	if(!this->boundsReady) return -1;
	return this->adaptedMax;
}


//-----  getMinValue method  --------------------------------------------------
int ParameterDefinition::getMinValue() {
	if(!this->boundsReady) return -1;
	return this->adaptedMin;
}

}
