package com.kbstar.eai.common.header; 

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

import com.eactive.eai.common.header.HeaderActionSupport;
//import com.kbstar.b2bgw.base.Utillity;

/** 
* 1. 기능 : 연계기관 의 Header를 EAI에서 생성하거나 삭제해야하는 경우를 위한 Class
* 2. 처리 개요 : 
*     - 메시지종류에 따라 헤더를 생성하거나 삭제하는 API를 제공
* 3. 주의사항 
*     - AD 의 경우는 S는 ADD, R 는 DELETE
*     - DA 의 경우는 S는 DELETE, R 는 ADD
* @author  : 
* @version : v 1.0.0
* @see :
* @since : 
*/
public class KSHINBOHeaderAction extends HeaderActionSupport 
{ 
    private static final int HEADER_LENGTH = 100; // B2B 헤더Length
    
    public KSHINBOHeaderAction(){
    	super.setHeaderLength(HEADER_LENGTH);
    	
    }
    
    public byte[] addHeaderDA(byte[] orgData , byte[] message ) {
		byte[] header = new byte[HEADER_LENGTH];
		byte[] full = null;
    	full = new byte[HEADER_LENGTH + message.length];
    	byte[] body		= message;
	    
    	System.arraycopy(orgData, 0, header, 0, HEADER_LENGTH);
    	
    	//전문 총길이를 헤더에 8byte로 추가    	
//    	byte[] msgSize = Utillity.fillZero(Integer.toString(full.length), 8).getBytes();    	
//    	System.arraycopy(msgSize, 0, header, 0, 8);
    	byte[] RES_CODE = "0000".getBytes();
    	System.arraycopy(RES_CODE, 0, header, 8, 4);
    	byte[] SYSTEM_ID = "BLS".getBytes();
    	System.arraycopy(SYSTEM_ID, 0, header, 12, 3);
    	byte[] FLOW_ID = "01".getBytes();
    	System.arraycopy(FLOW_ID, 0, header, 25, 2);    	
    	String tmpSERVICE_CODE = "";
    	try
		{			
    		//DB 삭제(2008.12.3)
    		tmpSERVICE_CODE = "DD" + new String(body, 19, 4);
		
		}catch(Exception ex){			
			tmpSERVICE_CODE = "DD????";
		}
		byte[] SERVICE_CODE = tmpSERVICE_CODE.getBytes();
		System.arraycopy(SERVICE_CODE, 0, header, 27, 6);
    	byte[] IO = "2".getBytes();
    	System.arraycopy(IO, 0, header, 33, 1);
    	byte[] FORMAT_ID = "01".getBytes();
    	System.arraycopy(FORMAT_ID, 0, header, 34, 2);
    	byte[] USER_ID = "WAPUSER".getBytes();
    	System.arraycopy(USER_ID, 0, header, 46, USER_ID.length);
    	byte[] CERT = "0".getBytes();
    	System.arraycopy(CERT, 0, header, 54, 1);
    	
       	String tmpIP = "";
       	
       	try{
       		tmpIP = InetAddress.getByName("www.shinbo.co.kr").getHostAddress();
		}
		catch(UnknownHostException exHost){
			//tmpIP = "shinbo";
			tmpIP = "210.96.165.5";
		}

		byte[] IP = tmpIP.getBytes();
    	System.arraycopy(IP, 0, header, 55, IP.length);
/*
    	byte[] LANG = "1".getBytes();
    	System.arraycopy(LANG, 0, header, 77, 1);
*/    	
//    	String tmpTRAN_DATE = Utillity.getDateStamp();
//    	byte[] TRAN_DATE = tmpTRAN_DATE.getBytes();
//    	System.arraycopy(TRAN_DATE, 0, header, 78, TRAN_DATE.length);
/*    	
    	String tmpTRAN_SEQ = "SB"+Utillity.fillZero(Long.toString(new Object().hashCode()), 6);
    	byte[] TRAN_SEQ = tmpTRAN_SEQ.getBytes();
    	System.arraycopy(TRAN_SEQ, 0, header, 86, TRAN_SEQ.length);
*/    	
    	System.arraycopy(header, 0, full, 0, HEADER_LENGTH );    	
    	System.arraycopy(message, 0, full, HEADER_LENGTH, body.length );
    	    	
    	return full;
	}
	public byte[] addHeaderAD(byte[] orgData , byte[] message ) {
		byte[] header = new byte[HEADER_LENGTH];
		byte[] full = null;
				
		int bodySize = message.length - 15;		
		//15 byte Skip - 받은 메시지 중에 15byte는 Skip하여 처리한다.
    	full = new byte[HEADER_LENGTH + bodySize];
    	byte[] bodyMsg = new byte[bodySize];       
       	System.arraycopy(message, 15, bodyMsg, 0, bodySize);           		
       	byte[] body = bodyMsg;
	    //Header에 화이트 스페이스 초기화   	
    	Arrays.fill(header, 0, HEADER_LENGTH, (byte)' ');
    	
    	//전문 총길이를 헤더에 8byte로 추가    	
//    	byte[] msgSize = Utillity.fillZero(Integer.toString(full.length), 8).getBytes();    	
//    	System.arraycopy(msgSize, 0, header, 0, 8);
    	byte[] RES_CODE = "0000".getBytes();
    	System.arraycopy(RES_CODE, 0, header, 8, 4);
    	byte[] SYSTEM_ID = "BLS".getBytes();
    	System.arraycopy(SYSTEM_ID, 0, header, 12, 3);
    	byte[] FLOW_ID = "01".getBytes();
    	System.arraycopy(FLOW_ID, 0, header, 25, 2);    	
    	String tmpSERVICE_CODE = "";
    	try
		{	
    		//DB 삭제(2008.12.3)
			tmpSERVICE_CODE = "DD" + new String(body, 19, 4);
			
		}catch(Exception ex){			
			tmpSERVICE_CODE = "DD????";
		}
		byte[] SERVICE_CODE = tmpSERVICE_CODE.getBytes();
		System.arraycopy(SERVICE_CODE, 0, header, 27, 6);
    	byte[] IO = "1".getBytes();
    	System.arraycopy(IO, 0, header, 33, 1);
    	byte[] FORMAT_ID = "01".getBytes();
    	System.arraycopy(FORMAT_ID, 0, header, 34, 2);
    	byte[] USER_ID = "WAPUSER".getBytes();
    	System.arraycopy(USER_ID, 0, header, 46, USER_ID.length);
    	byte[] CERT = "0".getBytes();
    	System.arraycopy(CERT, 0, header, 54, 1);
    	
       	String tmpIP = "";
       	
       	try{
       		tmpIP = InetAddress.getByName("www.shinbo.co.kr").getHostAddress();
		}
		catch(UnknownHostException exHost){
			tmpIP = "shinbo";
		}

		byte[] IP = tmpIP.getBytes();
    	System.arraycopy(IP, 0, header, 55, IP.length);
/*
    	byte[] LANG = "1".getBytes();
    	System.arraycopy(LANG, 0, header, 77, 1);
*/    	
//    	String tmpTRAN_DATE = Utillity.getDateStamp();
//    	byte[] TRAN_DATE = tmpTRAN_DATE.getBytes();
//    	System.arraycopy(TRAN_DATE, 0, header, 78, TRAN_DATE.length);
    	//IB.최진원대리,이민욱대리요청(2008.11.27) SB --> GB
//    	String tmpTRAN_SEQ = "GB"+Utillity.fillZero(Long.toString(new Object().hashCode()), 6);
//    	byte[] TRAN_SEQ = tmpTRAN_SEQ.getBytes();
//    	System.arraycopy(TRAN_SEQ, 0, header, 86, TRAN_SEQ.length);
    	
    	System.arraycopy(header, 0, full, 0, HEADER_LENGTH );    	
    	System.arraycopy(bodyMsg, 0, full, HEADER_LENGTH, body.length );
//    	System.out.println("full][" +new String(full)+ "]");    	
    	return full;
    }   
	
	public byte[] deleteHeaderAD(byte[] message, int headerLength) {		
        int msgLength = message.length - headerLength;
        //원 전문(요청전문-업무)에서 앞 15byte를 skip하고 호스트에 데이타를 전송하게 되면 
        //응답 전문 또한 15byte를 추가하여 응답으로 전송하게 된다. 
        //이에 15byte를 추가하는 부분
        int TMP_HEADER_SIZE = 15;  // 15byte 크기 확보
        if(msgLength > 0) {        	
            byte[] retHeader = new byte[msgLength+TMP_HEADER_SIZE];    
            byte[] tmpHeader = new byte[TMP_HEADER_SIZE];
            tmpHeader = "               ".getBytes();
            System.arraycopy(tmpHeader, 0, retHeader, 0, TMP_HEADER_SIZE);  
            System.arraycopy(message, headerLength, retHeader, 15, msgLength);            
            return retHeader;
        }else {        	
            return message;
        }
        
    }

} 

