package com.eactive.eai.custom.transformer.message;

import java.util.HashMap;

import com.eactive.eai.transformer.message.ISO8583Message;
import com.solab.iso8583.IsoMessage;
import com.solab.iso8583.IsoType;
import com.solab.iso8583.MessageFactory;
import com.solab.iso8583.parse.FieldParseInfo;

/**
 * <pre>
* ISO 8583 H2H(전문) 형식의 데이터를 변환하기 위한 메시지.(For KB Bukopin)
*
* [참조]
* https://en.wikipedia.org/wiki/ISO_8583
 * </pre>
 */
@SuppressWarnings("serial")
public class ISO8583H2HMessage extends ISO8583Message {
	private static HashMap<Integer, FieldParseInfo> fullSpecMap = null;
	private static HashMap<Integer, String> fullFieldNameMap = null;

	@Override
	public MessageFactory<IsoMessage> createMessageFactory() {
		MessageFactory<IsoMessage> mf = new MessageFactory<>();
		mf.setCharacterEncoding(System.getProperty("file.encoding"));
		mf.setForceStringEncoding(false);
		return mf;
	}

	@Override
	public HashMap<Integer, FieldParseInfo> getFullSpecMap(String encode) {
		if (fullSpecMap != null) {
			return fullSpecMap;
		}

		fullSpecMap = new HashMap<>();

		fullSpecMap.put(2, FieldParseInfo.getInstance(IsoType.LLVAR, 19, encode));
		fullSpecMap.put(3, FieldParseInfo.getInstance(IsoType.NUMERIC, 6, encode));
		fullSpecMap.put(4, FieldParseInfo.getInstance(IsoType.NUMERIC, 12, encode));
		fullSpecMap.put(6, FieldParseInfo.getInstance(IsoType.NUMERIC, 12, encode));
		fullSpecMap.put(7, FieldParseInfo.getInstance(IsoType.DATE10, 10, encode));
		fullSpecMap.put(11, FieldParseInfo.getInstance(IsoType.NUMERIC, 6, encode));
		fullSpecMap.put(12, FieldParseInfo.getInstance(IsoType.TIME, 6, encode));
		fullSpecMap.put(13, FieldParseInfo.getInstance(IsoType.DATE4, 4, encode));
		fullSpecMap.put(14, FieldParseInfo.getInstance(IsoType.DATE_EXP, 4, encode));
		fullSpecMap.put(15, FieldParseInfo.getInstance(IsoType.DATE4, 4, encode));
		fullSpecMap.put(18, FieldParseInfo.getInstance(IsoType.NUMERIC, 4, encode));
		fullSpecMap.put(22, FieldParseInfo.getInstance(IsoType.NUMERIC, 3, encode));
		fullSpecMap.put(24, FieldParseInfo.getInstance(IsoType.NUMERIC, 3, encode));
		fullSpecMap.put(25, FieldParseInfo.getInstance(IsoType.NUMERIC, 2, encode));
		fullSpecMap.put(26, FieldParseInfo.getInstance(IsoType.NUMERIC, 2, encode));
		fullSpecMap.put(32, FieldParseInfo.getInstance(IsoType.LLVAR, 11, encode));
		fullSpecMap.put(33, FieldParseInfo.getInstance(IsoType.LLVAR, 11, encode));
		fullSpecMap.put(35, FieldParseInfo.getInstance(IsoType.LLVAR, 37, encode));
		fullSpecMap.put(37, FieldParseInfo.getInstance(IsoType.ALPHA, 12, encode));
		fullSpecMap.put(38, FieldParseInfo.getInstance(IsoType.ALPHA, 6, encode));
		fullSpecMap.put(39, FieldParseInfo.getInstance(IsoType.ALPHA, 2, encode));
		fullSpecMap.put(40, FieldParseInfo.getInstance(IsoType.ALPHA, 3, encode));
		fullSpecMap.put(41, FieldParseInfo.getInstance(IsoType.ALPHA, 8, encode));
		fullSpecMap.put(42, FieldParseInfo.getInstance(IsoType.ALPHA, 15, encode));
		fullSpecMap.put(43, FieldParseInfo.getInstance(IsoType.ALPHA, 40, encode));
		fullSpecMap.put(48, FieldParseInfo.getInstance(IsoType.LLLVAR, 999, encode));
		fullSpecMap.put(49, FieldParseInfo.getInstance(IsoType.ALPHA, 3, encode));
		// fullSpecMap.put(52, FieldParseInfo.getInstance(IsoType.BINARY, 64, encode));
		fullSpecMap.put(52, FieldParseInfo.getInstance(IsoType.ALPHA, 16, encode)); // 표준X
		fullSpecMap.put(54, FieldParseInfo.getInstance(IsoType.LLLVAR, 120, encode));
		fullSpecMap.put(55, FieldParseInfo.getInstance(IsoType.LLLVAR, 765, encode));
		fullSpecMap.put(60, FieldParseInfo.getInstance(IsoType.LLLVAR, 999, encode));
		fullSpecMap.put(61, FieldParseInfo.getInstance(IsoType.LLLVAR, 999, encode));
		fullSpecMap.put(62, FieldParseInfo.getInstance(IsoType.LLLVAR, 999, encode));
		fullSpecMap.put(63, FieldParseInfo.getInstance(IsoType.LLLVAR, 999, encode));
		fullSpecMap.put(70, FieldParseInfo.getInstance(IsoType.NUMERIC, 3, encode));
		fullSpecMap.put(90, FieldParseInfo.getInstance(IsoType.NUMERIC, 42, encode));
		fullSpecMap.put(98, FieldParseInfo.getInstance(IsoType.ALPHA, 25, encode));
		fullSpecMap.put(102, FieldParseInfo.getInstance(IsoType.LLVAR, 28, encode));
		fullSpecMap.put(103, FieldParseInfo.getInstance(IsoType.LLVAR, 28, encode));
		fullSpecMap.put(120, FieldParseInfo.getInstance(IsoType.LLLVAR, 999, encode));
		// fullSpecMap.put(126, FieldParseInfo.getInstance(IsoType.LLLVAR, 999,
		// encode));
		fullSpecMap.put(126, FieldParseInfo.getInstance(IsoType.LLLLVAR, 9999, encode)); // 표준X
		fullSpecMap.put(127, FieldParseInfo.getInstance(IsoType.LLLVAR, 999, encode));
		fullSpecMap.put(128, FieldParseInfo.getInstance(IsoType.BINARY, 64, encode));

		return fullSpecMap;
	}
	
	public String getFieldName(int key) {
		if (fullFieldNameMap == null) {
			fullFieldNameMap = new HashMap<>();
			fullFieldNameMap.put(1, "Bitmap, Secondary");
			fullFieldNameMap.put(2, "Primary Account Number");
			fullFieldNameMap.put(3, "Processing Code");
			fullFieldNameMap.put(4, "Amount, Transaction");
			fullFieldNameMap.put(6, "Amount, Cardholder Billing");
			fullFieldNameMap.put(7, "Transmission Date and Time");
			fullFieldNameMap.put(11, "System Trace Audit Number");
			fullFieldNameMap.put(12, "Time, Local Transaction");
			fullFieldNameMap.put(13, "Date, Local Transaction");
			fullFieldNameMap.put(14, "Date, Expiration");
			fullFieldNameMap.put(15, "Date, Settlement");
			fullFieldNameMap.put(18, "Merchant Type");

			fullFieldNameMap.put(22, "Point of Service Entry Mode");
			fullFieldNameMap.put(24, "Network/Function Indentification Id");
			fullFieldNameMap.put(25, "Point-Of-Service Condition Code");

			fullFieldNameMap.put(26, "Point-Of-Service PIN Capture Code");
			fullFieldNameMap.put(32, "Acquiring Institution Identification Code");
			fullFieldNameMap.put(33, "Forwarding Institution Identification Code");

			fullFieldNameMap.put(35, "Track 2 Data");
			fullFieldNameMap.put(37, "Retrieval Reference Number");
			fullFieldNameMap.put(38, "Authorization Identification Response");

			fullFieldNameMap.put(39, "Response Code");
			fullFieldNameMap.put(40, "Service Restriction Code");
			fullFieldNameMap.put(41, "Card Acceptor Terminal Identification");

			fullFieldNameMap.put(42, "Card Acceptor Identification");
			fullFieldNameMap.put(43, "Card Acceptor Name and Location");
			fullFieldNameMap.put(48, "Additional Data – Private");

			fullFieldNameMap.put(49, "Transaction Currency Code");
			fullFieldNameMap.put(52, "Personal Identification Number");
			fullFieldNameMap.put(54, "Amount, Additional");

			fullFieldNameMap.put(55, "Integrated Circuit Card (ICC) System Related Data");

			fullFieldNameMap.put(60, "Reserved Private F60");
			fullFieldNameMap.put(61, "Reserved Private F61");
			fullFieldNameMap.put(62, "Reserved Private F62");

			fullFieldNameMap.put(63, "Reserved Private F63");
			fullFieldNameMap.put(70, "Network Management Information Code");
			fullFieldNameMap.put(90, "Original Data Element");

			fullFieldNameMap.put(98, "Payee");
			fullFieldNameMap.put(102, "Account Identification 1");
			fullFieldNameMap.put(103, "Account Identification 2");

			fullFieldNameMap.put(120, "Reserved Private F120");
			fullFieldNameMap.put(126, "Reserved Private F126");
			fullFieldNameMap.put(127, "Destination Institution Identification Code");
			fullFieldNameMap.put(128, "Message Authentication Code Field");
		}

		return fullFieldNameMap.get(new Integer(key));
	}

	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append(
				"[ISO8583H2H message]");
		sb.append("\n" + super.toString());
		sb.append(
				"\n");
		return sb.toString();
	}
}