package tutorial.client5;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JOptionPane;

import kr.fujitsu.com.ffw.daemon.core.config.repository.property.PropertyUtil;
import kr.fujitsu.com.ffw.daemon.net.client.ClientAction;
import kr.fujitsu.com.ffw.daemon.net.filter.BasicByteFilter;
import kr.fujitsu.com.ffw.util.StringUtil;

import org.apache.log4j.Logger;

import biz.comm.AES256;
import biz.comm.COMMBiz;

public class TutorialClientAction5 extends ClientAction {
private static Logger logger = Logger.getLogger(TutorialClientAction5.class); 
 	
  private TutorialClientDAO5 dao = null;

  /**
   * ClientAction의 execute메소드를 구현하도록 합니다.
   */
  public void execute() {
	  
    TutorialClientActionSocket5 sock = null;
    List list = null;
    Map map = null;
	HashMap trmap		= new HashMap();

	AES256 trAs = null;
	
	logger.info("sendmsg start" );

    try {
      // TutorialClientDAO를 선언해 Tutorial Database의 TUTORIAL_SRC 테이블에
      // 모든 데이터를 SELECT해 옵니다.    	
     logger.info("ClientActionSoket1");

     StringBuffer trbuf = new StringBuffer();
     StringBuffer hrbuf = new StringBuffer();
     String dataMsg = null;
     
     /*
      dao = new TutorialClientDAO();
      list = dao.findTutorialSrc();
      StringBuffer buf = null;
      String col = null;
      logger.info("ClientActionSoket2");

      
      //trAs = new AES256((String)hmCommon.get("POS_NO")+ (String)hmCommon.get("SYS_YMD")+ (String)hmCommon.get("SYS_HMS") );
      
      // ROW는 java.util.List
      for (int i = 0; i < list.size(); i++) {
    	// COLUMN은 java.util.map
 	    	
	        map = (Map)list.get(i);
	        buf = new StringBuffer();
	        // 사용되는 map에서 데이터를 가져오기 위해 조회한 테이블의 컬럼명을 입력
 	        // map 의 키 값은 대소문자 구분함..!!!!!!!!!!
	        col = (String) map.get("STORE_CD");
	        buf.append(col); // store_cd * 4
	        col = (String) map.get("POS_NO"); // pos_no * 2
	        buf.append(col);
	        col = (String) map.get("CASHIER_NM"); // cachier_nm * 10
	        col = StringUtil.rPad(col, 10, " ");
	        buf.append(col);
	        col = (String) map.get("TOT_SALE_AMOUNT"); // tot_sale_amount * 6
	        col = StringUtil.lPad(col, 6, " ");
	        buf.append(col);
	        col = (String) map.get("SALE_DATE_TIME"); // sale_date_time * 23
	        col = StringUtil.lPad(col, 23, " ");
	        buf.append(col);
	
	        // PropertyUtil을 통해 tutorial-property xml에 들어있는 TARGET_IP외ㅏ
	        // TARGET_PORT값을 가져온다.
	        String ip = PropertyUtil.findProperty("tutorial-property","TARGET_IP");
	        int port = Integer.parseInt(PropertyUtil.findProperty("tutorial-property", "TARGET_PORT"));
	
	        // ClientActionSocket을 생성하고 대상 ip, port를 입력한 후 사용할 Filter를
	        // 지정한다.
	        sock = new TutorialClientActionSocket(ip, port,new BasicByteFilter());
	        // ClientActionScoket을 통해 전송할 메시지를 지정한다.
	        sock.setUserMap("telegram", buf.toString());
	         
	        // 일단 Stack에 샇아 두게 된다. 가급적 1000건 이상 스텍에 넣지는 않도로 한다.
	        addClientActionSocket(sock);
      }
      */
     
      //trbuf.append(str)
     logger.info("trSend 0 " );
      trbuf.append("01|000000|0|0|0|00|20161101||190121|0017496000||0||||||0||||0|||||0| |0|0|0|3|5800.00|0.00|5800.00|0.00|0.00|0.00|0.00|0.00|0.00|0.00|0.00|0.00|0.00|0.00|0.00|0.00|0.00||| |0||");      				
      trbuf.append("#~02|000000|1||||0017496|050253|0|1|1600.00|1600.00| |0.00|1||1|1|1||0.00|0.00|0.00||1600.00|0|0000|0|0.00|0.00|0.00|0|0.00|0000|0.00|0000|0.00|0000|0.00|0000|0.00|0.00|00|0.00||0.00|0|| |0|0.00||||0");
      trbuf.append("#~02|000000|2||||0017496|050248|0|1|2200.00|2200.00| |0.00|1||1|1|1||0.00|0.00|0.00||2200.00|0|0000|0|0.00|0.00|0.00|0|0.00|0000|0.00|0000|0.00|0000|0.00|0000|0.00|0.00|00|0.00||0.00|0|| |0|0.00||||0");
      trbuf.append("#~02|000000|3||||0017496|050252|0|1|2000.00|2000.00| |0.00|1||1|1|1||0.00|0.00|0.00||2000.00|0|0000|0|0.00|0.00|0.00|0|0.00|0000|0.00|0000|0.00|0000|0.00|0000|0.00|0.00|00|0.00||0.00|0|| |0|0.00||||0");
      trbuf.append("#~03|000000|1|00|1000.00|00||||1|0.00|0.00|0.00|0.00|0.00|1000.00|0.00|0.00|0.00|0.00|0.00|0.00|0.00|0.00|0.00||||||||||||||||||||현금|1|||||0|0");
      trbuf.append("#~03|000000|2|02|2000.00|00||||1|0.00|0.00|0.00|0.00|0.00|2000.00|0.00|0.00|0.00|0.00|0.00|0.00|0.00|0.00|0.00|123456|123789|161101|1901|K|123789||1|||99|||00|0|01|0|||기타카드|0|||||0|0");
      trbuf.append("#~03|000000|3|13|2800.00|00||1||1|0.00|0.00|0.00|0.00|0.00|2800.00|0.00|0.00|0.00|0.00|0.00|0.00|0.00|0.00|0.00|161029114406|||||||0||0|2|||||||||외상||||||0|0");
      
   	  logger.info("trSend 0 " );

/*
 * 			int nlens[]= {6,2,4,4,10,8,2,5,8,6,1,3};  
 * 		String strHeaders[] = {
				"MSG_LEN",  	
				"MSG_TYPE",
				"CMP_CD",
				"BRAND_CD",
				"STOR_CD",
				"SALE_DT",
				"POS_NO",
				"TRADE_NO",
				"SYS_YMD",
				"SYS_HMS",
				"ENCRYT_MODE",
				"ERR_CD" 
				00019213PCKR002 0000190   2016112203000122016112211042010008
			};
			CMP_CD	SALE_DT		STOR_CD			POS_NO			TRADE_NO
			REG_NO	TRADE_DIV	TRAIN_MODE_YN	TRADE_KINDPER	SYS_DT
			PAY_TIME		ORG_SALE_DT			ORG_POS_NO		ORG_TRADE_NO		QTY
			TOTSALE_AMT		TOTDC_AMT			NET_SALE_AMT	AF_ADD_CASH_DIV		AF_ADD_POINT_DIV
			RET_DIV			RET_SALE_DT			RET_POS_NO		RET_TRADE_NO
			            
 */
 
 	 for (int i = 1; i < 501 ; i++) {
 		 
 		 
 	   	  trmap.put("CMP_CD", "PCKR"); 
 	   	  trmap.put("SALE_DT", PropertyUtil.findProperty("tutorial-property", "SALE_DT"));
 	   	  trmap.put("STOR_CD", PropertyUtil.findProperty("tutorial-property", "STOR_CD"));
 	   	  trmap.put("POS_NO", PropertyUtil.findProperty("tutorial-property", "005"));
 	   	   
 		  trmap.put("TRADE_NO", StringUtil.lPad(String.valueOf(i),5,"0"));
 		  trmap.put("REG_NO", StringUtil.lPad(String.valueOf(i),5,"0"));
 	   	   
 	   	  trmap.put("TRADE_DIV", "0");
 	   	  trmap.put("TRAIN_MODE_YN", "0");
 	   	  trmap.put("TRADE_KINDPER", "00");
 	   	  trmap.put("SYS_DT", (new SimpleDateFormat("yyyyMMdd")).format(new Date()));
 	   	 
 	   	  
 	   	  trmap.put("PAY_TIME",(new SimpleDateFormat("HHmmss")).format(new Date()));
 	   	  trmap.put("ORG_SALE_DT", "");
 	   	  trmap.put("ORG_POS_NO", "");
 	   	  trmap.put("ORG_TRADE_NO", "");
 	   	  trmap.put("QTY", "3");
 	   	  
 	 	  trmap.put("TOTSALE_AMT", "5800");
 	   	  trmap.put("TOTDC_AMT", "0");
 	   	  trmap.put("NET_SALE_AMT", "5800");
 	   	  trmap.put("AF_ADD_CASH_DIV", "");
 	   	  trmap.put("AF_ADD_POINT_DIV", ""); 
 	   	  trmap.put("DEAL_DATA", trbuf.toString());
 	   	  
 	   	  dataMsg = COMMBiz.makeMapToSendData(trmap);

	   	  logger.info("dataMsg  length1  " + dataMsg.getBytes().length);
	   	  logger.info("dataMsg  length2  " + dataMsg.getBytes("UTF-8").length);
	   	  logger.info("dataMsg  length3  " + dataMsg.length());

	   	  /*
	      hrbuf.append(StringUtil.lPad(String.valueOf(dataMsg.getBytes().length),6,"0"));
	      hrbuf.append("01");
	      hrbuf.append("PCKR");
	      hrbuf.append(" 002");
	      hrbuf.append("   0007786");
	      hrbuf.append(PropertyUtil.findProperty("tutorial-property", "SALE_DT"));
	      hrbuf.append(PropertyUtil.findProperty("tutorial-property", "005")); 
	      hrbuf.append(StringUtil.lPad(String.valueOf(i),5,"0"));
	      hrbuf.append((new SimpleDateFormat("yyyyMMdd")).format(new Date()));
	      hrbuf.append((new SimpleDateFormat("HHmmss")).format(new Date()));
	      hrbuf.append("0");
	      hrbuf.append("000");
	      hrbuf.append(dataMsg);
	      */
	      
	   	  StringUtil.appendSpace(hrbuf, StringUtil.lPad(String.valueOf(dataMsg.getBytes().length),6,"0"), 6);		 
	   	  StringUtil.appendSpace(hrbuf, "01", 2);
	   	  StringUtil.appendSpace(hrbuf, "PCKR",4);
		  StringUtil.appendSpace(hrbuf, "002", 4);
		  StringUtil.appendSpace(hrbuf, PropertyUtil.findProperty("tutorial-property", "STOR_CD"), 10);
		  StringUtil.appendSpace(hrbuf, PropertyUtil.findProperty("tutorial-property", "SALE_DT"), 8);
		  StringUtil.appendSpace(hrbuf, PropertyUtil.findProperty("tutorial-property", "005"), 2);	//POS 번호 
		  StringUtil.appendSpace(hrbuf, StringUtil.lPad(String.valueOf(i),5,"0"), 5);
		  StringUtil.appendSpace(hrbuf, (new SimpleDateFormat("yyyyMMdd")).format(new Date()), 8);
		  StringUtil.appendSpace(hrbuf, (new SimpleDateFormat("HHmmss")).format(new Date()), 6);
		  StringUtil.appendSpace(hrbuf, "0", 1);
		  StringUtil.appendSpace(hrbuf, "000", 3); 
		  StringUtil.appendSpace(hrbuf, dataMsg, 0); 
		  
	      
	   	  logger.info("trSend2 " + hrbuf.toString() );

		 String ip = "10.0.40.98";
		 int port = 9012;
		 
		 sock = new TutorialClientActionSocket5(ip, port,new BasicByteFilter());
		 sock.setUserMap("telegram", hrbuf.toString());
		 addClientActionSocket(sock);
		 hrbuf.setLength(0);
	         
 	 }
 	 
      //trmap.put(, value)
     /*
        StringBuffer buf = null;
        String col = null;
    	 for (int i = 0; i < 5 ; i++) {
    		 
    		 logger.info("sendmsg start" );
    		 
    		 buf = new StringBuffer();
    		 buf.append("00556801SLKR500021076      ");
    
    		 String ip = "10.0.40.98";
 	         int port = 9012;
 	        logger.info(buf.toString());
 	        
 	         sock = new TutorialClientActionSocket(ip, port,new BasicByteFilter());
 	         sock.setUserMap("telegram", buf.toString());
 	         addClientActionSocket(sock);
    	 }
    	 */
      // add된 ClientActionSocket을 Multi-Threaded로 동시에 타겟 IP로 
      // 전문을 보내게된다.
      dispatch();
    } catch (Exception e) {
      logger.info("에러 발생 --> " +  e.toString());      
    }

  }

}
