package com.eactive.eai.batch.doc;

import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

import com.eactive.eai.common.util.Logger;
import com.jcraft.jsch.ChannelSftp;

public class BatchDoc implements Serializable{

	private static final long serialVersionUID = 1L;
	BatchMsg batchMsg;

	/*** sFTP전송 종료	 */
	private ChannelSftp sftpChannel;
	
	/*** 사용자 취소인 경우 true */
	private boolean userCancel;
	
	/** 기관별 로깅 */
	Logger logger;
	
	/**/
	private Object bos;
	private long currIODataByte;

	public ChannelSftp getSftpChannel() {
		return sftpChannel;
	}

	public void setSftpChannel(ChannelSftp sftpChannel) {
		this.sftpChannel = sftpChannel;
	}

	public boolean isUserCancel() {
		return userCancel;
	}

	public void setUserCancel(boolean userCancel) {
		this.userCancel = userCancel;
	}

	public BatchMsg getBatchMsg() {
		return batchMsg;
	}

	public void setBatchMsg(BatchMsg batchMsg) {
		this.batchMsg = batchMsg;
	}
	
	public BatchMsg addNewBatchMsg(){
		batchMsg = new BatchMsg();
		return batchMsg;
	}

	@Override
	public String toString() {
		return batchMsg.toString();
	}

	public class BatchMsg implements Serializable
	{
		private static final long serialVersionUID = 1L;
		Header    header;
		Body      body;
		Phaseinfo phaseinfo;
		String    telegramHeader;
		String    telegramBody;
		String    fileHeader;
		String    fileTrailer;
		
		public void addNewHeader(){
			header = new Header();
		}
		
		public void addNewBody(){
			body = new Body();
		}
		
		public void addNewPhaseinfo(){
			phaseinfo = new Phaseinfo();
		}
		
		public Header getHeader() {
			return header;
		}
		public void setHeader(Header header) {
			this.header = header;
		}
		public Body getBody() {
			return body;
		}
		public void setBody(Body body) {
			this.body = body;
		}
		public Phaseinfo getPhaseinfo() {
			return phaseinfo;
		}
		public void setPhaseinfo(Phaseinfo phaseinfo) {
			this.phaseinfo = phaseinfo;
		}
		public String getTelegramHeader() {
			return telegramHeader;
		}
		public void setTelegramHeader(String telegramHeader) {
			this.telegramHeader = telegramHeader;
		}
		public String getTelegramBody() {
			return telegramBody;
		}
		public void setTelegramBody(String telegramBody) {
			this.telegramBody = telegramBody;
		}
		public String getFileHeader() {
			return fileHeader;
		}
		public void setFileHeader(String fileHeader) {
			this.fileHeader = fileHeader;
		}
		public String getFileTrailer() {
			return fileTrailer;
		}
		public void setFileTrailer(String fileTrailer) {
			this.fileTrailer = fileTrailer;
		}

		@Override
		public String toString() {
			return "[BatchMsg]\n" + header + body + phaseinfo + "\nlogger=" +  (logger == null ? "null" : logger.getName()) 
					+ "\ntelegramHeader=" + telegramHeader + "\ntelegramBody=" + telegramBody
					+ "\nfileHeader=" + fileHeader + "\nfileTrailer="
					+ fileTrailer + "]";
		}
	}
	
	/**
	 * 로거가 없을 경우 기본 로거를 ElinkLogger.LOGGER_DEFAULT로 얻는다.
	 */
	public Logger getLogger() {
		if (logger == null) {
			logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
			logger.error("inst logger is null {}", batchMsg.toString());
		}
		return logger;
	}

	public void setLogger(Logger logger) {
		this.logger = logger;
	}

	/**
	 * 로거가 없을 경우 기본 로거를 loggerAdapter로 얻는다.
	 * @param loggerAdapter 로거가 없을 경우 기본 로거
	 * @return
	 */
	public Logger getLogger(String loggerAdapter) {
		if (logger == null) {
			logger = Logger.getLogger(loggerAdapter);
			logger.error("inst logger is null {}", batchMsg.toString());
		}
		return logger;
	}

	public void setIOStream(Object bos) {
		this.bos = bos;
	}
	
	public Object getIOStream() {
		return this.bos;
	}

	public void setCurrIODataByte(long currIODataByte) {
		this.currIODataByte = currIODataByte;
	}
	
	public long getCurrIODataByte() {
		return this.currIODataByte ;
	}

}
