package com.eactive.eai.batch.job.jobItem.bokng;

import com.eactive.eai.batch.job.jobConstant.TelegramKeys;
import com.eactive.eai.batch.job.jobItem.DefaultTelegram;
import com.eactive.eai.batch.job.jobItem.TelegramService;
import com.eactive.eai.batch.rule.nodeinfo.NodeInfoManager;
import com.eactive.eai.batch.rule.nodeinfo.NodeInfoVO;
import com.eactive.eai.common.exception.ExceptionUtil;

public class RECV_BOKNG extends DefaultTelegram implements TelegramService {

    /**
     * 생성자 : 한국은행 국고망 길이 결정용 텔레그램 클래스 이다.
     */
    public RECV_BOKNG() {
    }

    /**
     * 전문 Validation
     */
    public boolean doValidation()
    {
        if (!super.doValidation()) return false;
        
        try {
			logger.info("{} >>>>> 응답코드  Checking......", logHeader);
			int currFiledIndex = 6;
			String currFieldValue = new String((byte[])al_Field.get(currFiledIndex));
			if (!currFieldValue.equals(TelegramKeys.BOKGG_RES_CODE_SUCESS))  {
				String errMsg = "정상거래 응답코드를 수신하지 못했습니다. 수신된 응답코드 ["+currFieldValue+"]-"+TelegramKeys.getBokGGRetDescription(currFieldValue);
				batchDoc.getBatchMsg().getBody().setErrorMsg(errMsg);	//
				super.setResolveResponseCode(TelegramKeys.BOKGG_RES_CODE_ERROR);
				super.setConditionTxt(TelegramKeys.ERROR);
				return false;
			}
        }catch (Exception e){
        	logger.error( e.getMessage(), e);
        	this.batchDoc.getBatchMsg().getBody().setErrorMsg(e.toString());
        	super.setResolveResponseCode(TelegramKeys.BOKGG_RES_CODE_ERROR);
        	super.setConditionTxt(TelegramKeys.ERROR);
        	return false;
        }
        return true;
    }
   
    public boolean doExecute() {
        return true;
    }

    //---------------- 특정 필드값을 구하는 부분
    // 전문종별코드 필드값 리턴
    public String getTelegramType()
    {
		return super.getFieldValueString(3);
    }
    // 거래구분코드 필드값 리턴
    public String getTrClass()
    {
		return super.getFieldValueString(7);
    }
    //응답코드 필드값 리턴
    public String getResCode()
    {
		return super.getFieldValueString(6);
    }
    //업무관리코드 필드값 리턴
    public String getOpCode()
    {
    	if ( super.getFieldValueString(3).equalsIgnoreCase("0600") ||super.getFieldValueString(3).equalsIgnoreCase("0610") ){
        	return super.getFieldValueString(9);
    	}
    	else {
    		return super.getOpCode();
    	}
    }

    public int getRemainDataLength()
    {
        try {
            int     idx;
            String  strLength;
    
            String  strRuleCode   = batchDoc.getBatchMsg().getHeader().getRuleCode();
            String  strPhaseCode  = batchDoc.getBatchMsg().getBody().getFlowPhaseCode();
            NodeInfoVO  vo = NodeInfoManager.getInstance().getNodeInfo(strRuleCode, strPhaseCode);
    
            idx = vo.getLengthField();
            if (idx == 0) return -1;
    
            strLength = new String((byte[])this.al_Field.get(idx-1));
            int     nDocLen = Integer.parseInt(strLength);
            int     nReceivedLen = 0;
            for (int i=1; i<this.al_FieldLength.length ; i++) {
                nReceivedLen = nReceivedLen + Integer.parseInt(this.al_FieldLength[i]);
            }
    
            return (nDocLen-nReceivedLen);
            
        } catch (Exception ex) {
            this.batchDoc.getBatchMsg().getBody().setErrorMsg(ex.toString());
            return -1;
        }
    }
}
