#include "IMFResponse.h"
#include "Log.h"

namespace coreutils {

   IMFResponse::IMFResponse() {}
   IMFResponse::~IMFResponse() {}

   std::stringstream IMFResponse::getResponse(Mode mode) {
      std::stringstream stream("");
      return getResponse(stream, mode);
   }

   std::stringstream IMFResponse::getResponse(std::stringstream &content, Mode mode) {
      std::stringstream response;
      response << protocol << " " << code << " " << text << CRLF;

      if(mode == LENGTH)
        addHeader((char *)"Content-Length", (char *)std::to_string(content.gcount()).c_str());
      else
        addHeader((char *)"Transfer-Encoding", (char *)"chunked");

      addHeader((char *)"Server", (char *)"JETServer v0.0.1");

      output(response);
      response << content.str();
//      core::Log(core::LOG_DEBUG_4) << response.str();
      return response;
   }

   void IMFResponse::setProtocol(ZString protocol) {
      this->protocol = protocol;
   }

   void IMFResponse::setCode(ZString code) {
      this->code = code;
   }

   void IMFResponse::setText(ZString text) {
      this->text = text;
   }

   void IMFResponse::setCookie(ZString key, ZString data) {
      addHeader("Set-Cookie", data);
   }

   void IMFResponse::setCookie(ZString key, std::string data) {
      addHeader("Set-Cookie", ZString(data.c_str()));
   }

}
