/*
 * messagedata.cpp
 *
 *  Created on: Aug 17, 2017
 *      Author: vadims
 */
#include <ctime>
#include <iostream>
#include "messagedata.hpp"


messagedata::messagedata()
{
	msgStatus = messagedata::success;
	msgType = messagedata::log;
	msgLevel = messagedata::info_lvl;
	function = "";
	sendTo = "";
	guid = "";
	arg1 = "";
	arg2 = "";
	arg3 = "";
	isDefined = false;
}

// for return
messagedata::messagedata(status msgStatus_, type msgType_, std::string text_data_) :
		msgStatus(msgStatus_), msgType(msgType_), text_data(text_data_)
{
	msgLevel = messagedata::info_lvl;
	function = "";
	sendTo = "";
	guid = "";
	arg1 = "";
	arg2 = "";
	arg3 = "";
	isDefined = true;

}

messagedata::messagedata(status msgStatus_, type msgType_, std::string text_data_, std::string guid_) :
		msgStatus(msgStatus_), msgType(msgType_), text_data(text_data_), guid(guid_)
{
	msgLevel = messagedata::info_lvl;
	function = "";
	sendTo = "";
	arg1 = "";
	arg2 = "";
	arg3 = "";
	isDefined = true;
}

// TO DO expand for all functionality. Currently only for use in SalomeModule
messagedata::messagedata(json j)
{
	//guid
	if (j.find("guid") != j.end())
		guid = j.at("guid").get<std::string>();
	else
		guid = "";

	//command
	if (j.find("command") != j.end())
		function = j.at("command").get<std::string>();
	else
		function = "";

	//status
	if (j.find("status") != j.end())
	{
		std::string stat = j.at("status").get<std::string>();
		if (stat == "success")
			msgStatus = status::success;
		else
			msgStatus = status::error;
	}
	else
		msgStatus = status::error;

	//text_data
	// TO DO fix. Message field is present only in msg with status error
	if (j.find("message") != j.end())
		text_data = j.at("message").get<std::string>();
	else
		text_data = "";


	msgLevel = messagedata::info_lvl;

}

// for logging
messagedata::messagedata(level msgLevel_, type msgType_, std::string text_data_) :
		msgLevel(msgLevel_), msgType(msgType_), text_data(text_data_)
{
	msgStatus = messagedata::success;
	function = "";
	sendTo = "";
	guid = "";
	arg1 = "";
	arg2 = "";
	arg3 = "";
	isDefined = true;
}

void messagedata::addDataToMessage(messagedata msgd)
{
	json j0;
	std::istringstream(text_data) >> j0;
	json j1;
	std::istringstream(msgd.text_data) >> j1;

	j0.update(j1);
	text_data = j0.dump();
}

std::string messagedata::outputData()
{
	json j;


	switch(msgType)
	{
	    case response  :  {
	    	j["type"] = "response";
	    	j["guid"] = guid;
	    	j["sendTo"] = "gui";
	    	switch(msgStatus)
	    	{
	    	    case success  : {
	    	    	j["status"] = "success";
	    	    	std::istringstream(text_data) >> j["data"];
	    	    }
	    	    break;
	    	    case error: {
	    	    	j["status"] = "error";
	    	    	j["message"] = text_data;
	    	    }
	    	    break;
	    	}
		 }
	    break;
	    case call : {
	    	j["type"] = "call";
	    	j["function"] = function;
	    	j["guid"] = guid;
	    	j["arg1"] = arg1;
	    	j["sendTo"] = sendTo;
	    }
	    break;
	    case log: {
	    	j["type"] = "log";
	    	j["sendTo"] = "gui";

	    	switch(msgLevel)
	    	{
				case error_lvl  : {
					j["level"] = "error";
				//	text_data.insert(0, "ERROR:: ");
				}
				break;
				case warn_lvl: {
					j["level"] = "warn";
				//	text_data.insert(0, "WARNING:: ");
				}
				break;
				case info_lvl: {
					j["level"] = "info";
				//	text_data.insert(0, "INFO:: ");
				}
				break;
				case debug_lvl: {
					j["level"] = "debug";
				//	text_data.insert(0, "DEBUG:: ");
				}
				break;
			}

	    	j["message"] = text_data;
	    }
		break;
		case progress:
		{
			j["type"] = "progress";
			j["sendTo"] = "gui";
			j["guid"] = guid;
			j["data"] = text_data;
		}
		break;
		case wait:
		{
			j["type"] = "wait";
			j["sendTo"] = "gui";
			j["guid"] = guid;
		}
		break;

	}
	return j.dump();
}


