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

import java.io.File;
import java.lang.String;
import java.io.ByteArrayOutputStream;

import com.eactive.eai.common.exception.ExceptionUtil;

import com.eactive.eai.batch.job.jobConstant.TelegramKeys;
import com.eactive.eai.batch.job.jobItem.DefaultTelegram;
import com.eactive.eai.batch.job.jobItem.TelegramService;

public class RECV_BOKEX_03000020 extends DefaultTelegram implements TelegramService
{
    /**
     * 생성자
     */
    public RECV_BOKEX_03000020() {
        //파일 시작 요구
    }

    /**
     * 전문 해체하기
     */
    public boolean doExecute() {
        if (!super.doExecute()) return false;
        try{
            String bizCode     = new String ((byte[]) al_Field.get( 9));
            String fileVer     = new String ((byte[]) al_Field.get(10));
            String compGb      = new String ((byte[]) al_Field.get(12));
            String strFileSize = new String ((byte[]) al_Field.get(13));
            long fileSize      = Long.parseLong(strFileSize);     //화일 size

            String fileExt = "";
            if ( compGb.equals("1")){
                fileExt = ".Z";
            } else if (compGb.equals("2")){
                fileExt = ".ZIP";
            }
            String fileName = bizCode + "_"+ fileVer + "_" + TelegramKeys.getYYYYMMddHHmmss() + fileExt;
            batchDoc.getBatchMsg().getHeader().setBizCode(bizCode);
            batchDoc.getBatchMsg().getHeader().setFileName(fileName);
            String fileDir = batchDoc.getBatchMsg().getHeader().getFilePath();
            if (fileDir.charAt(fileDir.length()-1) != File.separatorChar) {
                fileDir = fileDir + File.separator;
            }
            String fileFullName = fileDir + fileName;

            File file = new File(fileFullName);
            if (file.exists()) {
                file.delete();
            }
            batchDoc.getBatchMsg().getBody().setCurFileSize(0);
            batchDoc.getBatchMsg().getHeader().setFileSize(fileSize);
            batchDoc.getBatchMsg().getHeader().setTotRecCnt(1);
            batchDoc.getBatchMsg().getBody().setBlockNum(0);
            batchDoc.getBatchMsg().getBody().setSeqNum(0);

        } catch ( Exception e) {
            String[] msgArgs = new String[1];
            msgArgs[0] = this.batchDoc.getBatchMsg().getHeader().getProcessCode();
            String errCode = "BECEAIFJI001";
            String errMsg = ExceptionUtil.getErrorCode (e, errCode, msgArgs);
            this.batchDoc.getBatchMsg().getBody().setErrorMsg(errMsg);
            logger.error( e.getMessage(), e);
            return false;
        }
        return true;
    }

    /**
     * 전문 Validation
     */
    public boolean doValidation()
    {
        if (!super.doValidation()) return false;
        return true;
    }

    public boolean doPostExecute() {
        if (!super.doPostExecute()) return false;
        try {
            //DB로그에 남길 전문 헤더 설정
            ByteArrayOutputStream baout = new ByteArrayOutputStream();
            for (int i=0; i<9; i++) {
                baout.write((byte[])this.al_Field.get(i));
            }
            batchDoc.getBatchMsg().setTelegramHeader(new String(baout.toByteArray()));

            //DB로그에 남길 전문 바디 설정
            baout = new ByteArrayOutputStream();
            for (int i=9; i< al_Field.size(); i++) {
                baout.write((byte[])this.al_Field.get(i));
            }
            batchDoc.getBatchMsg().setTelegramBody(new String(baout.toByteArray()));
            return true;
        } catch(Exception ex) {
            String[] msgArgs = new String[1];
            msgArgs[0] = this.batchDoc.getBatchMsg().getHeader().getProcessCode();
            String errCode = "BECEAIFJI002";
            String errMsg = ExceptionUtil.getErrorCode (ex, errCode, msgArgs);
            this.batchDoc.getBatchMsg().getBody().setErrorMsg(errMsg);
            logger.error( ex.getMessage(), ex);
            return false;
        }
    }

    //---------------- 특정 필드값을 구하는 부분
    // 전문종별코드 필드값 리턴
    public String getTelegramType()
    {
        String telegramNo = super.getFieldValueString(4);
        if ( telegramNo.length() < 8 ) return "";
        return telegramNo.substring(0,3) + telegramNo.substring(6,7);
    }

    //응답코드 필드값 리턴
    public String getResCode()
    {
        return super.getFieldValueString(8);
    }
}