/******************************************************************************
*	File		-	OpRes.cpp
*	Author		-	Joey Pollack
*	Date		-	2020/03/05 (y/m/d)
*	Mod Date	-	2020/03/05 (y/m/d)
*	Description	-	Operation Result. Also contains a message describing the result.
*
******************************************************************************/

#include "logger.h"
#include "op_res.h"

#include <stdarg.h>

namespace lunarium
{
	char OpRes::Buffer[OpRes::BUFFER_SIZE];

	bool OpRes::operator==(const OpRes& rhs)
	{
		return this->Type == rhs.Type;
	}

	bool OpRes::operator==(ResultType rhs)
	{
		return this->Type == rhs;
	}

	OpRes OpRes::OK()
	{
		return { ResultType::OK, "The operation succeeded" };
	}

	OpRes OpRes::Fail(const char* why, ...)
	{
		va_list args;
		va_start(args, why);
		vsprintf(OpRes::Buffer, why, args);
		va_end(args);

		return { ResultType::FAIL, OpRes::Buffer };
	}

	OpRes OpRes::LogIfFailed(int category, const char* prepend_msg)
	{
		if (ResultType::FAIL == Type)
		{
			std::string msg;
			if (prepend_msg)
			{
				msg = prepend_msg;
			}
			msg += Description;
			Logger::Error(category, msg.c_str());
		}
		return *this;
	}

	bool IsOK(OpRes&& res)
	{
		return res.Type == ResultType::OK;
	}

	bool IsOK(OpRes& res)
	{
		return res.Type == ResultType::OK;
	}

	bool Failed(OpRes&& res)
	{
		return res.Type == ResultType::FAIL;
	}

	bool Failed(OpRes& res)
	{
		return res.Type == ResultType::FAIL;
	}
}