package com.eactive.eai.transformer.util;

import org.apache.commons.codec.binary.Hex;

import com.eactive.eai.transformer.layout.Item;
import com.eactive.eai.transformer.message.ISO8583Message;
import com.eactive.eai.transformer.message.ISO8583MessageFactory;
import com.solab.iso8583.IsoMessage;
import com.solab.iso8583.IsoValue;

public class ISO8583DumpUtil {
	private ISO8583DumpUtil() {
		
	}
	
	public static String dumpIsoMessage(IsoMessage isoMessage, ISO8583Message iso8583Message) {
		StringBuilder dumpBuffer = new StringBuilder();

		String format = String.format("  MsgType : [ %04x ]\n", isoMessage.getType());
		dumpBuffer.append(format);

		StringBuilder fieldValueBuffer = new StringBuilder();
		String fields = "  Fields  : [ ";
		for (int i = 1; i < 129; i++) {
			if (isoMessage.hasField(i)) {
				IsoValue<?> isoValue = isoMessage.getField(i);
				Object val = isoValue.getValue();
				String parsedVal;
				if (val instanceof String) {
					parsedVal = (String) val;
				} else if (val instanceof byte[]) {
					parsedVal = "b'" + Hex.encodeHexString((byte[]) val) + "'";
				} else {
					//parsedVal = String.valueOf(val);
					parsedVal = isoValue.toString();
				}
				fields = fields + i + " ";
				if (iso8583Message != null) {
					Item item = iso8583Message.getChildByName(String.format("%03d", i));
					format = String.format("    %3d {%7s} %-35s [%s]\n", i, isoValue.getType(),
							"\"" + item.getDescription() + "\"", parsedVal);
				} else {
					format = String.format("    %3d {%7s} - [%s]\n", i, isoValue.getType(), parsedVal);
				}
				fieldValueBuffer.append(format);
			}
		}
		fields += "]";
		dumpBuffer.append(fields);
		dumpBuffer.append("\n");
		dumpBuffer.append(fieldValueBuffer);

		return dumpBuffer.toString();
	}
	
	public static String dumpIsoMessageForEms(IsoMessage isoMessage, String messageType) {
		StringBuilder dumpBuffer = new StringBuilder();
		
		ISO8583Message iso8583Message = ISO8583MessageFactory.create(messageType);
		
		String format = String.format("  MsgType : [ %04x ]\n", isoMessage.getType());
		dumpBuffer.append(format);
		
		StringBuilder fieldValueBuffer = new StringBuilder();
		String fields = "  Fields  : [ ";
		for (int i = 1; i < 129; i++) {
			if (isoMessage.hasField(i)) {
				IsoValue<?> isoValue = isoMessage.getField(i);
				Object val = isoValue.getValue();
				String parsedVal;
				if (val instanceof String) {
					parsedVal = (String) val;
				} else if (val instanceof byte[]) {
					parsedVal = "b'" + Hex.encodeHexString((byte[]) val) + "'";
				} else {
					//parsedVal = String.valueOf(val);
					parsedVal = isoValue.toString();
				}
				fields = fields + i + " ";
				format = String.format("    %3d {%7s} %-50s [%s]\n", i, isoValue.getType(),
						"\"" + getFieldName(iso8583Message, i) + "\"", parsedVal);
				fieldValueBuffer.append(format);
			}
		}
		fields += "]";
		dumpBuffer.append(fields);
		dumpBuffer.append("\n");
		dumpBuffer.append(fieldValueBuffer);
		
		return dumpBuffer.toString();
	}

	public static String getFieldName(ISO8583Message iso8583Message, int i) {
		String fieldName = "";
		try {
			fieldName = iso8583Message.getFieldName(i);
		} catch (Exception e) {
			fieldName = String.format("%03d", i);
		}
		return fieldName;
	}
}
