#include "Win32WebResponse.h"

#include "HttpResponse.h"
#include "Win32WebRequest.h"

#include "FileLogger.h"

Win32WebResponse::Win32WebResponse(const HttpResponse& response)
{
    RtlZeroMemory((&mResponse), sizeof(*(&mResponse)));

    mResponse.StatusCode = response.getStatusCode();

    mResponseReason = response.getResponseReason();
    mResponse.pReason = mResponseReason.c_str();
    mResponse.ReasonLength = (USHORT)strlen(mResponseReason.c_str());

    populateHeader(response);

    populateBody(response);
}

HTTP_RESPONSE& Win32WebResponse::getResponse()
{
	return mResponse;
}

void Win32WebResponse::populateBody(const HttpResponse& response)
{
    mBody = response.getBody();
    if (!mBody.empty())
    {
        mContentLength = std::to_string(mBody.size());

        mDataChunk.DataChunkType = HttpDataChunkFromMemory;
        char test[] = "test";
        mDataChunk.FromMemory.pBuffer = mBody.data();
        mDataChunk.FromMemory.BufferLength = (ULONG)strlen(mBody.data());

        mResponse.EntityChunkCount = 1;
        mResponse.pEntityChunks = &mDataChunk;
    }
}

void Win32WebResponse::populateHeader(const HttpResponse& response)
{
    mContentType = response.getHeader().getContentType();
    mResponse.Headers.KnownHeaders[HttpHeaderContentType].pRawValue = mContentType.c_str();
    mResponse.Headers.KnownHeaders[HttpHeaderContentType].RawValueLength = (USHORT)strlen(mContentType.c_str());
}

void Win32WebResponse::addRequestContentLength(const Win32WebRequest& request)
{
    mContentLength = std::to_string(request.getEntityTotalSize());
    mResponse.Headers.KnownHeaders[HttpHeaderContentLength].pRawValue = mContentLength.c_str();
    mResponse.Headers.KnownHeaders[HttpHeaderContentLength].RawValueLength = (USHORT)strlen(mContentLength.c_str());
}