package com.kbstar.eai.common.header;

import com.eactive.eai.common.header.HeaderAction;
import com.eactive.eai.common.header.HeaderActionFactory;
import com.eactive.eai.common.header.HeaderActionKeys;
import com.eactive.eai.common.header.HeaderActionSupport;

/** 
* 1. 기능 : KB신용정보업무의 HEADER 와 CAP 매핑을 위한 Class
* 2. 처리 개요 : 
*     - 메시지종류에 따라 헤더를 생성하거나 삭제하는 API를 제공
* 3. 주의사항 
*     - AD 의 경우는 S는 HEADER->CAP, R 는 CAP -> HEADER
*     - DA 의 경우는 S는 CAP -> HEADER, R 는 HEADER->CAP
* @author  : 
* @version : v 1.0.0
* @see :
* @since : 
*/
public class KBSHeaderAction extends HeaderActionSupport 
{ 
    private static final int HEADER_LENGTH = 62; // 기관간의 헤더Length
    private static final int CAP_LENGTH    = 80; // HOST CAP 헤더Length

    public KBSHeaderAction(){
    	super.setHeaderLength(HEADER_LENGTH);
    }

    public byte[] addHeaderAD(byte[] orgData , byte[] message ) {
    	return headerToCap(message);
    }   	
    
    public byte[] deleteHeaderAD(byte[] message, int headerLength) {
     	return capToHeader(message);
    }
    
    public byte[] addHeaderDA(byte[] orgData , byte[] message ) {
    	return capToHeader(message);
	}
	
	
	public byte[] deleteHeaderDA(byte[] message, int headerLength) {
		return headerToCap(message);
    }
    
	public byte[] headerToCap(byte[] message) {
		int msg_size = message.length - HEADER_LENGTH + CAP_LENGTH;

		byte[] retHeader = new byte[msg_size];
		byte[] capHeader = new byte[CAP_LENGTH];
		//--------------------------------------------
	    /*
		 host_msg.trx_code     ':=' kb_msg.tran_code     for 8 bytes; 
	     host_msg.filler1      ':=' [%H40];                           
	     host_msg.io_meche     ':=' [%HE3];                           
	     host_msg.brno         ':=' [%HF2,%HF5,%HF2,%HF7];            
	     host_msg.brno_sub     ':=' [%HF0,%HF0,%HF0];                 
	     host_msg.term_no      ':=' kb_msg.term_no       for 3 bytes; 
	     host_msg.term_code    ':=' kb_msg.term_code     for 2 bytes; 
	     host_msg.serl_no      ':=' kb_msg.serl_no       for 4 bytes; 
	     host_msg.gsbr         ':=' msg_text[62]         for 4 bytes; 
	     host_msg.gsbr_sub     ':=' [%HF0,%HF0,%HF0];                 
	     host_msg.appl_code    ':=' kb_msg.upmu_code     for 2 bytes; 
	     host_msg.input_code   ':=' kb_msg.input_code    for 4 bytes; 
	     host_msg.operator     ':=' kb_msg.op_no         for 4 bytes; 
	     host_msg.stats_flag   ':=' kb_msg.flag_code     for 4 bytes; 
	     host_msg.resv_no      ':=' [%HF0,%HF0];                      
	     host_msg.pf_key       ':=' kb_msg.pf_key        for 1 bytes; 
	     host_msg.user_head    ':=' kb_msg.user_head     for 30 bytes;
	    */ 
		
		System.arraycopy(message,  0, capHeader, 0, 8);
		
		capHeader[ 8] = (byte)0x40;		
		
		capHeader[ 9] = (byte)0xE3;
		
		capHeader[10] = (byte)0xF2;
		capHeader[11] = (byte)0xF5;
		capHeader[12] = (byte)0xF2;
		capHeader[13] = (byte)0xF7;
		
		capHeader[14] = (byte)0xF0;
		capHeader[15] = (byte)0xF0;
		capHeader[16] = (byte)0xF0;
		
		System.arraycopy(message,  8, capHeader, 17, 3);
		System.arraycopy(message, 11, capHeader, 20, 2);
		System.arraycopy(message, 13, capHeader, 22, 4);
		System.arraycopy(message, 62, capHeader, 26, 4);
		
		capHeader[30] = (byte)0xF0;
		capHeader[31] = (byte)0xF0;
		capHeader[32] = (byte)0xF0;
		
		System.arraycopy(message, 17, capHeader, 33, 2);
		System.arraycopy(message, 19, capHeader, 35, 4);
		System.arraycopy(message, 23, capHeader, 39, 4);
		System.arraycopy(message, 27, capHeader, 43, 4);
		capHeader[47] = (byte)0xF0;
		capHeader[48] = (byte)0xF0;
		System.arraycopy(message, 31, capHeader, 49, 1);
		System.arraycopy(message, 32, capHeader, 50,30);
		//--------------------------------------------
		System.arraycopy(capHeader,              0, retHeader,          0, CAP_LENGTH);
		System.arraycopy(message,    HEADER_LENGTH, retHeader, CAP_LENGTH, message.length - HEADER_LENGTH);
		return retHeader;		
	}
	
	public byte[] capToHeader(byte[] message) {
		int msg_size = message.length + HEADER_LENGTH - CAP_LENGTH;
	
		byte[] retHeader = new byte[msg_size];
		byte[] header    = new byte[HEADER_LENGTH];
		//--------------------------------------------
		/*
		kb_msg.tran_code      ':=' host_msg.trx_code    for 8 bytes;  
		kb_msg.term_no        ':=' host_msg.term_no     for 3 bytes;  
		kb_msg.term_code      ':=' host_msg.term_code   for 2 bytes;  
		kb_msg.serl_no        ':=' host_msg.serl_no     for 4 bytes;  
		kb_msg.upmu_code      ':=' host_msg.appl_code   for 2 bytes;  
		kb_msg.input_code     ':=' host_msg.input_code  for 4 bytes;  
		kb_msg.op_no          ':=' host_msg.operator    for 4 bytes;  
		kb_msg.flag_code      ':=' host_msg.stats_flag  for 4 bytes;  
		kb_msg.pf_key         ':=' host_msg.pf_key      for 1 bytes;  
		kb_msg.user_head      ':=' host_msg.user_head   for 30 bytes; 
		*/
		
		System.arraycopy(message,  0, header,  0,  8);
		System.arraycopy(message, 17, header,  8,  3);
		System.arraycopy(message, 20, header, 11,  2);
		System.arraycopy(message, 22, header, 13,  4);
		System.arraycopy(message, 33, header, 17,  2);
		System.arraycopy(message, 35, header, 19,  4);
		System.arraycopy(message, 39, header, 23,  4);
		System.arraycopy(message, 43, header, 27,  4);
		System.arraycopy(message, 49, header, 31,  1);
		System.arraycopy(message, 50, header, 32, 30);
		//--------------------------------------------
		System.arraycopy(header,           0, retHeader,             0, HEADER_LENGTH);
		System.arraycopy(message, CAP_LENGTH, retHeader, HEADER_LENGTH, message.length - CAP_LENGTH);
        return retHeader;		
	}
	
//	   public static void main(String args[]) throws Exception {
//	   test();
//}

// private static void test() throws Exception {
//	   String actionName = "com.kbstar.eai.common.header.KBSHeaderAction";
//	   
//
//	    /////전송 AD 
//	        HeaderAction action = HeaderActionFactory.createAction(actionName);
//
//	        Object[] bizObjs =new Object[1];
//	        bizObjs[0] = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890".getBytes();
//	        Object bizObj = null;
//	        if(bizObjs != null && bizObjs.length > 0) {
//	            bizObj = bizObjs[0];
//	        }
//	        byte[] orgData = null; 
//			System.out.println("orgData=>"+new String(orgData==null?"".getBytes():orgData));
//			System.out.println("message=>"+new String(bizObj==null?"".getBytes():(byte[])bizObj));
//			System.out.println("sendRecv=>"+HeaderActionKeys.MESSAGE_RECV);
//			System.out.println("commandType=>"+HeaderActionKeys.ADD_DELETE_HEADER);	    
//			byte[] header = action.excute(orgData ,(byte[])bizObj, HeaderActionKeys.MESSAGE_RECV, HeaderActionKeys.ADD_DELETE_HEADER);
//			System.out.println("header=>"+new String(header));
//	    }	
} 

