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

#include "StationDataItem.hpp"
#include "cJSON.h"

namespace otx {
	namespace jidu {

		StationDataItem::StationDataItem() {
			this->mTestItemResult = "OK";
		}
		StationDataItem::~StationDataItem() {

			int size = this->mTestEcuItems.size();
			for (int i = 0; i < size; i++) {
				StationDataECU* item = this->mTestEcuItems[i];
				delete item;
			}
			this->mTestEcuItems.clear();
		}

		std::string StationDataItem::getTestItemName() { return this->mTestItemName; }
		void StationDataItem::setTestItemName(const std::string& v) { this->mTestItemName = v; }

		std::string StationDataItem::getTestItemId() { return this->mTestItemId; }
		void StationDataItem::setTestItemId(const std::string& v) { this->mTestItemId = v; }

		std::string StationDataItem::getTestItemResult() { return this->mTestItemResult; }
		void StationDataItem::setTestItemResult(const std::string& v) { this->mTestItemResult = v; }

		std::vector<StationDataECU*>* StationDataItem::getTestEcuItems() { return &this->mTestEcuItems; }
		StationDataECU* StationDataItem::newEcu() {
			StationDataECU* result = new StationDataECU();
			this->mTestEcuItems.push_back(result);
			return result;
		}

		std::string StationDataItem::toJsonString() {
			std::string result;

			cJSON* obj = cJSON_CreateObject();

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

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

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

			int ecuCount = this->mTestEcuItems.size();
			if (ecuCount > 0) {
				cJSON* arrObj = cJSON_CreateArray();
				for (int i = 0; i < ecuCount; i++) {
					cJSON* tempItem = cJSON_Parse(this->mTestEcuItems[i]->toJsonString().c_str());

					cJSON_AddItemToArray(arrObj, tempItem);
				}

				cJSON_AddItemToObject(obj, "testItem", arrObj);
			}

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

	} // namespace jidu
} // namespace otx
