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

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

BEGIN_TEG_NAMESPACE();

// DataString constructor.
DataString::DataString(const std::string& _value)
  : IData(DATA_STRING), value(_value) { }

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

// Method to print the value of the string.
void DataString::print() {
  PRINT_LOG(TYPE_MESSAGE, "\"%s\"", value.c_str());
}

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

// Function to create a data with a string value.
SmartPointer<IData> newData(const std::string& t) {
  return new DataString(t);
}

// Function to try to convert a data in a string.
SmartPointer<DataString> castToString(SmartPointer<IData> d) {
  if (d == NULL) return NULL;
  if (d->type == IData::DATA_STRING)
    return dynamic_cast<DataString*>((IData*) d);
  if (d->type == IData::DATA_BOOL) {
    const SmartPointer<DataBool> tmp = castToBool(d);
	return new DataString(tmp->value ? _T("true") : _T("false"));
  }
  if (d->type == IData::DATA_INT) {
    const SmartPointer<DataInt> tmp = castToInt(d);
    return new DataString(toString(tmp->value));
  }
  if (d->type == IData::DATA_FLOAT) {
    const SmartPointer<DataFloat> tmp = castToFloat(d);
    return new DataString(toString(tmp->value));
  }
  return NULL;
}

END_TEG_NAMESPACE();
