// Copyrigth Xtooltech @ 2023- All rights reserved.(LXH)

#include "PrintResultModule.hpp"
#include "cJSON.h"

namespace otx {
	namespace jidu {

		PrintResultModule::~PrintResultModule() {
			int size = this->mTestBlockList.size();
			for (int i = 0; i < size; i++) {
				PrintResultItem* item = this->mTestBlockList[i];
				delete item;
			}
			this->mTestBlockList.clear();
		}

		std::string PrintResultModule::getTestStatisticName() { return this->mTestStatisticName; }
		void PrintResultModule::setTestStatisticName(const std::string& v) { this->mTestStatisticName = v; }

		std::string PrintResultModule::getTestStatisticId() { return this->mTestStatisticId; }
		void PrintResultModule::setTestStatisticId(const std::string& v) { this->mTestStatisticId = v; }

		std::string PrintResultModule::getTestStatisticResult() { return this->mTestStatisticResult; }
		void PrintResultModule::setTestStatisticResult(const std::string& v) { this->mTestStatisticResult = v; }

		std::vector<PrintResultItem*>* PrintResultModule::getTestBlockList() { return &this->mTestBlockList; }
		PrintResultItem* PrintResultModule::newTestBlockItem() {
			PrintResultItem* result = new PrintResultItem();
			this->mTestBlockList.push_back(result);
			return result;
		}

		std::string PrintResultModule::toJsonString(std::string station) {
			std::string result;

			cJSON* obj = cJSON_CreateObject();

			if (this->mTestStatisticName.size() > 0) {
				cJSON_AddStringToObject(obj, "testStatisticName", this->mTestStatisticName.c_str());
			}

			if (this->mTestStatisticId.size() > 0) {
				cJSON_AddNumberToObject(obj, "testStatisticId", std::stoi(this->mTestStatisticId, nullptr, 16));
			}

			if (this->mTestStatisticResult.size() > 0) {
				cJSON_AddStringToObject(obj, "testStatisticResult", this->mTestStatisticResult.c_str());
			}

			int size = this->mTestBlockList.size();
			if (size > 0) {
				cJSON* arrObj = cJSON_CreateArray();
				for (int i = 0; i < size; i++) {
					std::string tempString = this->mTestBlockList[i]->toJsonString();
					int blockId = std::stoi(this->mTestBlockList[i]->getTestBlockId(), NULL, 16);

					//TODO 不上报
					if (blockId == 0x0601 || blockId == 0x0901 || blockId == 0x0082)
						continue;
					//if (station._Equal("04") && (blockId == 0x0031 || blockId == 0x0011 || blockId == 0x0028)) {
					//	continue;
					//}

					cJSON* objItem = cJSON_Parse(tempString.c_str());

					std::string testBlockResult = "OK";
					if (cJSON_HasObjectItem(objItem, "testBlockResult"))
					{
						testBlockResult = cJSON_GetObjectItem(objItem, "testBlockResult")->valuestring;
					}

					if (station._Equal("03")) {
						cJSON_AddItemToArray(arrObj, objItem);
					}
					else if (!testBlockResult._Equal("OK"))
					{
						cJSON_AddItemToArray(arrObj, objItem);
					}
				}
				if (cJSON_GetArraySize(arrObj) > 0)
				{
					cJSON_AddItemToObject(obj, "testBlockList", arrObj);
				}
			}

			char* tepm = cJSON_PrintUnformatted(obj);
			cJSON_Delete(obj);
			result = tepm;
			cJSON_free(tepm);
			return result;
		}

	} // namespace jidu
} // namespace otx
