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

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

BEGIN_TEG_NAMESPACE();

// DataBool cosntructor.
DataBool::DataBool(const bool& _value)
  : IData(DATA_BOOL), value(_value) { }

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

// Method to print the value of the bool.
void DataBool::print() {
  PRINT_LOG(TYPE_MESSAGE, "%s", value ? "True" : "False");
}

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

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

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

END_TEG_NAMESPACE();
