package com.eactive.eai.custom.adapter.handler;

import java.util.Properties;

import org.apache.commons.lang3.StringUtils;

import com.eactive.eai.adapter.handler.TemplateCodeConvertAdapterErrorMsgHandler;
import com.eactive.eai.common.message.EAIMessage;
import com.eactive.eai.common.util.Logger;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ErpAdapterErrorMsgHandler extends TemplateCodeConvertAdapterErrorMsgHandler {

	private static final Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);

	private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

	@Override
	public Object generateNonStandardErrorResponseMessage(String inboundAdapterGroupName, String inboundAdapterName,
			Properties callProp, Object outboundRequestData, EAIMessage resEaiMsg) throws Exception {

		Object responseMsessage = super.generateNonStandardErrorResponseMessage(inboundAdapterGroupName,
				inboundAdapterName, callProp, outboundRequestData, resEaiMsg);

		logger.debug("generateNonStandardErrorResponseMessage - {}", responseMsessage);

		if (!(responseMsessage instanceof String)) {
			return responseMsessage;
		}

		String jsonStr = (String) responseMsessage;
		if (StringUtils.isBlank(jsonStr)) {
			return responseMsessage;
		}

		JsonNode rootNode = OBJECT_MAPPER.readTree(jsonStr);

		boolean modified = false;

		if (modified) {
			responseMsessage = OBJECT_MAPPER.writeValueAsString(rootNode);
		}

		return responseMsessage;
	}

}
