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

#include "PrintResultReportRecord.hpp"
#include "cJSON.h"
#include "MyStringUtils.h"

namespace otx {
	namespace jidu {

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

		std::string PrintResultReportRecord::getCarConfig() { return this->mCarConfig; }
		void PrintResultReportRecord::setCarConfig(const std::string& v) { this->mCarConfig = v; }

		std::string PrintResultReportRecord::getTpmsId()
		{
			return this->mTPMSId;
		}

		void PrintResultReportRecord::setTpmsId(const std::string& v)
		{
			this->mTPMSId = v;
		}

		std::string PrintResultReportRecord::getSshStatus()
		{
			return this->mSshStatus;
		}

		void PrintResultReportRecord::setSshStatus(const std::string& v)
		{
			this->mSshStatus = v;
		}

		std::string PrintResultReportRecord::getChargingIsOpen()
		{
			return this->mChargingIsOpen;
		}

		void PrintResultReportRecord::setChargingIsOpen(const std::string& v)
		{
			this->mChargingIsOpen = v;
		}

		std::string PrintResultReportRecord::getGloveboxIsOpen()
		{
			return this->mGloveboxIsOpen;
		}

		void PrintResultReportRecord::setGloveboxIsOpen(const std::string& v)
		{
			this->mGloveboxIsOpen = v;
		}

		std::string PrintResultReportRecord::getElectricity()
		{
			return this->mElectricity;
		}

		void PrintResultReportRecord::setElectricity(const std::string& v)
		{
			this->mElectricity = v;
		}

		std::size_t PrintResultReportRecord::getTestStatisticTotalNum() { return this->mTestStatisticTotalNum; }
		void PrintResultReportRecord::setTestStatisticTotalNum(std::size_t v) { this->mTestStatisticTotalNum = v; }

		std::size_t PrintResultReportRecord::getTestStatisticFailNum() { return this->mTestStatisticFailNum; }
		void PrintResultReportRecord::setTestStatisticFailNum(std::size_t v) { this->mTestStatisticFailNum = v; }

		std::size_t PrintResultReportRecord::getTestBlockFailNum() { return this->mTestBlockFailNum; }
		void PrintResultReportRecord::setTestBlockFailNum(std::size_t v) { this->mTestBlockFailNum = v; }

		std::string PrintResultReportRecord::getStationTime() { return this->mStationTime; }
		void PrintResultReportRecord::setStationTime(const std::string& v) { this->mStationTime = v; }

		std::vector<PrintResultModule*>* PrintResultReportRecord::getTestStatisticList() { return &this->mTestStatisticList; }
		PrintResultModule* PrintResultReportRecord::newStatisticItem() {
			PrintResultModule* result = new PrintResultModule();
			this->mTestStatisticList.push_back(result);
			return result;
		}

		std::string PrintResultReportRecord::toJsonString() {
			std::string result;
			cJSON* obj = cJSON_Parse(ReportRecord::toJsonString().c_str());

			if (getStation()._Equal("01")) {
				cJSON_AddStringToObject(obj, "tpmsId", this->mTPMSId.c_str());
			}
			else if (getStation()._Equal("02")) {
				cJSON_AddStringToObject(obj, "tpmsId", this->mTPMSId.c_str());
				if (!this->mElectricity.empty())
					cJSON_AddStringToObject(obj, "elecDisplay", this->mElectricity.c_str());
			}
			if (getStation()._Equal("00")) {
				if (!this->mChargingIsOpen.empty())
					cJSON_AddStringToObject(obj, "chargingIsOpen", this->mChargingIsOpen.c_str());
				if (!this->mGloveboxIsOpen.empty())
					cJSON_AddStringToObject(obj, "gloveboxIsOpen", this->mGloveboxIsOpen.c_str());
			}

			cJSON_AddStringToObject(obj, "carConfig", this->mCarConfig.c_str());
			cJSON_AddStringToObject(obj, "testStatisticTotalNum", std::to_string(this->mTestStatisticTotalNum).c_str());
			cJSON_AddStringToObject(obj, "testStatisticFailNum", std::to_string(this->mTestStatisticFailNum).c_str());
			cJSON_AddStringToObject(obj, "testBlockFailNum", std::to_string(this->mTestBlockFailNum).c_str());
			cJSON_AddStringToObject(obj, "stationTime", this->mStationTime.c_str());

			int size = this->mTestStatisticList.size();
			if (size > 0) {
				cJSON* arrObj = cJSON_CreateArray();
				for (int i = 0; i < size; i++) {
					cJSON* tempItem = cJSON_Parse(this->mTestStatisticList[i]->toJsonString(getStation()).c_str());
					if (cJSON_HasObjectItem(tempItem, "testStatisticResult")) {
						std::string testStatisticResult = cJSON_GetObjectItem(tempItem, "testStatisticResult")->valuestring;
						int testStatisticId = cJSON_GetObjectItem(tempItem, "testStatisticId")->valueint;
						if (0x22 == testStatisticId/*TODO 忽略大项 充电口盖打开*/ ||
							0x25 == testStatisticId/*TODO 忽略大项 手套箱打开*/)
						{
							cJSON_Delete(tempItem);
							continue;
						}
						cJSON_AddItemToArray(arrObj, tempItem);
					}
					else {
						cJSON_AddItemToArray(arrObj, tempItem);
					}
				}
				cJSON_AddItemToObject(obj, "testStatisticList", arrObj);
			}


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

	} // namespace jidu
} // namespace otx