package kr.gmtc.tss.filelog;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Queue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import kr.gmtc.tss.data.vo.AlarmVO;
import kr.gmtc.tss.data.vo.StandStatusVO;
import kr.gmtc.tss.main.MainServer;
import kr.gmtc.tss.util.FileUtil; 

public class MessageBodyLogger implements Runnable {

private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	private Queue<StandStatusVO> messageBodyStandStatusQueue;
	private Queue<AlarmVO> messageBodyAlarmQueue;
	private boolean isRunning = true;
	private int listChunkSize = 100;
	private long sleepMillis;
	
	public MessageBodyLogger(Queue<StandStatusVO> messageBodyStandStatusQueue, Queue<AlarmVO> messageBodyAlarmQueue, int listChunkSize, long sleepMillis) {
		this.messageBodyStandStatusQueue = messageBodyStandStatusQueue; 
		this.messageBodyAlarmQueue = messageBodyAlarmQueue; 
		this.listChunkSize = listChunkSize;
		this.sleepMillis = sleepMillis;
	}
	
	@Override
	public void run() {
		isRunning = true;
		
		while (isRunning) {
			try {
				StandStatusVO []  chunkStandStatusArr = getStatusChunkMessageArr(messageBodyStandStatusQueue);
				for (int i = 0; i < chunkStandStatusArr.length; i++) {
					//logger.error(getMessageBody(chunkArr[i]));
					FileUtil.writeLogFile(getMessageBody(chunkStandStatusArr[i]), MainServer.FILE_STAND_LOG_NAME);
				}
				
				AlarmVO []  chunkAlarmArr = getAlarmChunkMessageArr(messageBodyAlarmQueue);
				for (int i = 0; i < chunkAlarmArr.length; i++) {
					//logger.error(getMessageBody(chunkArr[i]));
					FileUtil.writeLogFile(getMessageBody(chunkAlarmArr[i]), MainServer.FILE_SITTN_LOG_NAME);
				}
				
			} catch (Exception e) {
				logger.info("-- [SqlLogger] LogSave error : " +e.getLocalizedMessage());
			} 
			
			sleep(sleepMillis);
		}
	}
	
	
	private StandStatusVO [] getStatusChunkMessageArr(Queue<StandStatusVO> queue) {
		int loopCnt = 0;
		
		if(queue.size()>listChunkSize) {
			loopCnt = listChunkSize;
		}else {
			loopCnt = queue.size();
		}
		
		StandStatusVO [] chunkArr = new StandStatusVO[loopCnt];
		
		for (int i = 0; i < loopCnt; i++) {
			chunkArr[i] = queue.poll();
		}
		
		return chunkArr;
	}
	
	private AlarmVO [] getAlarmChunkMessageArr(Queue<AlarmVO> queue) {
		int loopCnt = 0;
		
		if(queue.size()>listChunkSize) {
			loopCnt = listChunkSize;
		}else {
			loopCnt = queue.size();
		}
		
		AlarmVO [] chunkArr = new AlarmVO[loopCnt];
		
		for (int i = 0; i < loopCnt; i++) {
			chunkArr[i] = queue.poll();
		}
		
		return chunkArr;
	}
	
//	private String [] getChunkMessageArr(Queue<String> messageBodyQueue) {
//		int loopCnt = 0;
//		
//		if(messageBodyQueue.size()>listChunkSize) {
//			loopCnt = listChunkSize;
//		}else {
//			loopCnt = messageBodyQueue.size();
//		}
//		
//		String [] chunkArr = new String[loopCnt];
//		
//		for (int i = 0; i < loopCnt; i++) {
//			chunkArr[i] = messageBodyQueue.poll();
//		}
//		
//		return chunkArr;
//	}

//	private String getMessageBody(String str) {
//		Date date = new Date();
//		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//
//		StringBuilder sb = new StringBuilder();
//		
//		sb.append(formatter.format(date));
//		sb.append(" ");
//		sb.append(str);
//		
//		
//		return sb.toString();
//	}
	
	private String getMessageBody(Object vo) {
		StringBuilder sb = new StringBuilder();
		sb.append(vo.toString());
		sb.append("\n");
		
		return sb.toString();
	}
	
	private void sleep(long millis) {
		try {
			Thread.sleep(millis);
		} catch (InterruptedException e) {
			logger.debug("[SqlLogger] sleep error : "+e.getLocalizedMessage());
		}
	}

	public void setRunning(boolean isRunning) {
		this.isRunning = isRunning;
	}
}