package com.kbstar.eai.common.header; 

import java.util.Arrays;

import com.eactive.eai.common.header.HeaderActionSupport;
import com.eactive.eai.common.util.DatetimeUtil;


/** 
* 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 PPSHeaderAction extends HeaderActionSupport 
{ 
    private static final int HEADER_LENGTH = 100; // 조달청 헤더Length
    private static int mCount = 0;

    public PPSHeaderAction(){
    	super.setHeaderLength(HEADER_LENGTH);
    	
    }
    private static int getIncMCount() {
		if (mCount >= 99999){
			mCount =0;
		}
		mCount++;
		return mCount;
	}

	public byte[] addHeaderDA(byte[] orgData , byte[] message ) {
		byte[] full        = null;
		byte[] reqMessage = null;
		reqMessage  = orgData;
		
    	full = new byte[HEADER_LENGTH+message.length];
    	byte [] header = new byte[HEADER_LENGTH];

    	
		System.arraycopy(reqMessage, 0, header, 0, HEADER_LENGTH );

		//header 응답코드 변경
		Arrays.fill(header, 38 , 42, (byte)'0');  	//header = header.substring(0, 38) + "0000" + header.substring(42);
    	
		//
		System.arraycopy(header, 0, full, 0, HEADER_LENGTH );
		System.arraycopy(message, 0, full, HEADER_LENGTH, message.length);
		
		
		// 전체 전문길이 계산
		byte len[] = Integer.toString( full.length  ).getBytes();
		
		
		//길이값 '0'으로 셋팅
		Arrays.fill(full, 0, 4, (byte)'0');
		//길이값  value 셋팅
		System.arraycopy(len, 0, full, 4 - len.length, len.length);        	
   		return full;        	

    }
	public byte[] addHeaderAD(byte[] orgData , byte[] message ) {
		byte[] full        = null;
    	full = new byte[HEADER_LENGTH + message.length];
    	byte[] body		= message;
		
    	
		// 보안헤더
		byte[] sec		= new byte[HEADER_LENGTH];
		Arrays.fill(sec, 0, HEADER_LENGTH, (byte)' ');

		//길이 셋팅
		byte[] length	= Integer.toString(body.length + HEADER_LENGTH).getBytes();
		Arrays.fill(sec, 0, 4, (byte)'0');
		System.arraycopy(length, 0, sec, 4 - length.length, length.length);

		byte[] date		= DatetimeUtil.getCurrentDateTime().getBytes();
		System.arraycopy(date, 0, sec, 4, 14);

		byte[] seq		= Integer.toString( PPSHeaderAction.getIncMCount()  ).getBytes();
		Arrays.fill(sec, 18, 30, (byte)'0');
		System.arraycopy(seq, 0, sec, 30 - seq.length, seq.length);

		byte[] m_type	= {'0', '2', '0', '0'};
		System.arraycopy(m_type, 0, sec, 30, 4);
		
		byte[] t_code	= {'X', '2', '0', '3'};
		System.arraycopy(t_code, 0, sec, 34, 4);

		byte[] c_code	= {'1', '3', '7', '0', '1', '0', '0', '0'};
		System.arraycopy(c_code, 0, sec, 42, 8);
		
		byte[] i_code	= {'0', '0', '0', '0', '2'};
		System.arraycopy(i_code, 0, sec, 50, 5);
			
		byte[] s_code	= {'S', 'F', 'X', ' '};
		System.arraycopy(s_code, 0, sec, 55, 4);

		byte[] sr_type	= {'1'};
		System.arraycopy(sr_type, 0, sec, 68, 1);    	
		
		
		System.arraycopy(sec,0,full,0,HEADER_LENGTH);
		System.arraycopy(body,0,full,HEADER_LENGTH,body.length);
 
        return full;
        

    }   	
//	   public static void main(String args[]) throws Exception {
//	   test();
//}

// private static void test() throws Exception {
//		   String actionName = "com.kbstar.eai.common.header.PPSHeaderAction";
		    /////전송 AD 
//	        HeaderAction action = HeaderActionFactory.createAction(actionName);
//
//	        Object[] bizObjs =new Object[1];
//	        bizObjs[0] = "1234567890".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_SEND);
//			System.out.println("commandType=>"+HeaderActionKeys.ADD_DELETE_HEADER);	    
//         byte[] header = action.excute(orgData ,(byte[])bizObj, HeaderActionKeys.MESSAGE_SEND, HeaderActionKeys.ADD_DELETE_HEADER);
//         System.out.println("header=>"+new String(header));
		   	/////////////
		    
		    /////수신 AD 
//	        HeaderAction action = HeaderActionFactory.createAction(actionName);
//
//	        Object[] bizObjs =new Object[1];
//	        bizObjs[0] = "0110200805021112280000000000010200X203    1370100000002SFX          1                               1234567890".getBytes();
//	        Object bizObj = null;
//	        if(bizObjs != null && bizObjs.length > 0) {
//	            bizObj = bizObjs[0];
//	        }
//	        byte[] orgData = null; 
//       	    //orgData = new byte[((byte[])bizObj).length ];
//   	 	    //System.arraycopy(((byte[])bizObj), 0, orgData, 0,orgData.length);
//			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));
            ///////////////////////////////////////////

		   
		    /////전송 DA 
//	        HeaderAction action = HeaderActionFactory.createAction(actionName);
//
//	        Object[] bizObjs =new Object[1];
//	        bizObjs[0] = "0110200805021112280000000000010200X203    1370100000002SFX          1                               1234567890".getBytes();
//	        Object bizObj = null;
//	        if(bizObjs != null && bizObjs.length > 0) {
//	            bizObj = bizObjs[0];
//	        }
//	        byte[] orgData = null; 
//       	orgData = new byte[((byte[])bizObj).length ];
//   		System.arraycopy(((byte[])bizObj), 0, orgData, 0,orgData.length);
//			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_SEND);
//			System.out.println("commandType=>"+HeaderActionKeys.DELETE_ADD_HEADER);	               
//         byte[] header = action.excute(orgData ,(byte[])bizObj, HeaderActionKeys.MESSAGE_SEND, HeaderActionKeys.DELETE_ADD_HEADER);
//         System.out.println("header=>"+new String(header));
		   	/////////////
		    
		    /////수신 DA 
//	        HeaderAction action = HeaderActionFactory.createAction(actionName);
//
//	        Object[] bizObjs =new Object[1];
//	        bizObjs[0] = "1234567890".getBytes();
//	        Object bizObj = null;
//	        if(bizObjs != null && bizObjs.length > 0) {
//	            bizObj = bizObjs[0];
//	        }
//	        byte[] orgData = null; 
//      	orgData = new byte["0110200805021112280000000000010200X203    1370100000002SFX          1                               1234567890".getBytes().length ];
//  	 	System.arraycopy("0110200805021112280000000000010200X203    1370100000002SFX          1                               1234567890".getBytes(), 0, orgData, 0,orgData.length);
//			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.DELETE_ADD_HEADER);	                   
//         byte[] header = action.excute(orgData ,(byte[])bizObj, HeaderActionKeys.MESSAGE_RECV, HeaderActionKeys.DELETE_ADD_HEADER);
//         System.out.println("header=>"+new String(header));
//	    }	
} 

