// __author__ = Juan Carlos De Abreu (jcabreur@gmail.com)

#include "DataFloat.h"
#include "DataInt.h"
#include "DataBool.h"
#include "DataString.h"
#include "../../Util/String.h"
#include "../DataMemoryManagement.h"

BEGIN_TEG_NAMESPACE();

// DataFloat constructor.
DataFloat::DataFloat(const float& _value)
  : IData(DATA_FLOAT), value(_value) { }

// DataFloat destructor.
DataFloat::~DataFloat() { }

// Method to print the value of the float.
void DataFloat::print() {
  PRINT_LOG(TYPE_MESSAGE, "%.4f", value);
}

// Method to execute the expresion.
SmartPointer<IData> DataFloat::resolve(
    std::map<std::string, SmartPointer<IData>>& variables) {
  return castToDATA(SmartPointer<DataFloat> (this));
}

// Function to create a data with a float value.
SmartPointer<IData> newData(const float& t) {
  return castToDATA(NEW_DATA_FLOAT(t));
}

// Function to try to convert a data in a float.
SmartPointer<DataFloat> castToFloat(SmartPointer<IData> d) {
  if (d == NULL) return NULL;
  if (d->type == IData::DATA_FLOAT)
    return dynamic_cast<DataFloat*>((IData*) d);
  if (d->type == IData::DATA_INT) {
    const SmartPointer<DataInt> tmp = castToInt(d);
    return NEW_DATA_FLOAT(static_cast<float>(tmp->value));
  }
  if (d->type == IData::DATA_BOOL) {
    const SmartPointer<DataBool> tmp = castToBool(d);
    return NEW_DATA_FLOAT(tmp->value ? 1.0f : 0.0f);
  }
  if (d->type == IData::DATA_STRING) {
    const SmartPointer<DataString> tmp = castToString(d);
    return NEW_DATA_FLOAT(toFloat(tmp->value));
  }
  return NULL;
}

END_TEG_NAMESPACE();
