package kr.gmtc.tss.sqllog;

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

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

import kr.gmtc.tss.data.vo.TrackVO;
import kr.gmtc.tss.main.MainServer;

public class SqlLogger implements Runnable {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	private Queue<TrackVO> sqlLogTrackQueue;
	private Queue<TrackVO> sqlLogTrackLastQueue;
	private boolean isRunning=true;
	private int listChunkSize= 100;
	private long sleepMillis;
	
	public SqlLogger(Queue<TrackVO> sqlLogTrackQueue,Queue<TrackVO> sqlLogTrackLastQueue, int listChunkSize, long sleepMillis) {
		this.sqlLogTrackQueue = sqlLogTrackQueue; 
		this.sqlLogTrackLastQueue = sqlLogTrackLastQueue;
		this.listChunkSize = listChunkSize;
		this.sleepMillis = sleepMillis;
	}
	
	@Override
	public void run() {
		isRunning = true;
		
		while (isRunning) {
			try {
				TrackVO []  chunkArr = getChunkTrackArr(sqlLogTrackQueue);
				for (int i = 0; i < chunkArr.length; i++) {
					logger.error(getInsertTrackSql(chunkArr[i]));
				}

				/*
				 * chunkArr = getChunkTrackArr(sqlLogTrackLastQueue); for (int i = 0; i <
				 * chunkArr.length; i++) { logger.error(getInsertLastTrackSql(chunkArr[i])); }
				 */
				 
			} catch (Exception e) {
				logger.info("-- [SqlLogger] LogSave error : " +e.getLocalizedMessage());
			} 
			
			sleep(sleepMillis);
		}
	}
	
	private TrackVO [] getChunkTrackArr(Queue<TrackVO> sqlQueue) {
		int loopCnt=0;
		
		if(sqlQueue.size()>listChunkSize) {
			loopCnt=listChunkSize;
		}else {
			loopCnt=sqlQueue.size();
		}
		
		TrackVO [] chunkArr = new TrackVO[loopCnt];
		
		for (int i = 0; i < loopCnt; i++) {
			chunkArr[i]=sqlQueue.poll();
		}
		
		return chunkArr;
	}

	private String getInsertTrackSql(TrackVO vo) {
		StringBuilder sb = new StringBuilder();
//		sb.append("\n");
		sb.append("INSERT INTO ");
		sb.append("SACP_TRACK_FUSION");
		sb.append(" (TRGT_ID, RECPTN_DT, LAT, LON, SPD, COS, TRGT_TY, REVISN_LAT, REVISN_LON, MODE_S_CD, SSR_CD, CLSGN, AC_REG_NO, REGISTER_ID)");
		sb.append("VALUES (");
		sb.append("'"); sb.append(vo.getTargetId()); sb.append("',");
		sb.append("'"); sb.append(vo.getReceptionDate()); sb.append("',");
		sb.append("'"); sb.append(vo.getLatitude()); sb.append("',");
		sb.append("'"); sb.append(vo.getLongitude()); sb.append("',");
		sb.append("'"); sb.append(vo.getSpeed()); sb.append("',");
		sb.append("'"); sb.append(vo.getCourse()); sb.append("',");
		sb.append("'"); sb.append(vo.getTargetType()); sb.append("',");
		sb.append("'"); sb.append(vo.getCorrectedLatitude()); sb.append("',");
		sb.append("'"); sb.append(vo.getCorrectedLongitude()); sb.append("',");
		sb.append("'"); sb.append(vo.getModeSCode()); sb.append("',");
		sb.append("'"); sb.append(vo.getSsrCode()); sb.append("',");
		sb.append("'"); sb.append(vo.getCallsign()); sb.append("',");
		sb.append("'"); sb.append(vo.getTailNumber()); sb.append("',");
		sb.append("'"); sb.append("fusion"); sb.append("');");
		
		for (int j = 0; j < vo.getBbox().size(); j++) {
			sb.append("\n");
			sb.append("INSERT INTO ");
			sb.append("SACP_FUSION_TRACK_BBOX");
			sb.append(" (TRGT_ID, RECPTN_DT, CCTV_ID, BBOX_TOP_LEFT_X, BBOX_TOP_LEFT_Y, BBOX_BOTTOM_RIGHT_X, BBOX_BOTTOM_RIGHT_Y, CRDNT_X, CRDNT_Y, REGISTER_ID)");
			sb.append("VALUES (");
			sb.append("'"); sb.append(vo.getTargetId()); sb.append("',");
			sb.append("'"); sb.append(vo.getReceptionDate()); sb.append("',");
			sb.append("'"); sb.append(vo.getBbox().get(j).getCctvId()); sb.append("',");
			sb.append("'"); sb.append(vo.getBbox().get(j).getLeftTopX()); sb.append("',");
			sb.append("'"); sb.append(vo.getBbox().get(j).getLeftTopY()); sb.append("',");
			sb.append("'"); sb.append(vo.getBbox().get(j).getRightBottomX()); sb.append("',");
			sb.append("'"); sb.append(vo.getBbox().get(j).getRightBottomY()); sb.append("',");
			sb.append("'"); sb.append(vo.getBbox().get(j).getPx()); sb.append("',");
			sb.append("'"); sb.append(vo.getBbox().get(j).getPy()); sb.append("',");
			sb.append("'"); sb.append("fusion"); sb.append("');");
		}
		
		return sb.toString();
	}

	private String getInsertLastTrackSql(TrackVO vo) {
		StringBuilder sb = new StringBuilder();
		if(MainServer.LAST_DATA_CNT > 0) {
			sb.append("UPDATE SACP_TRACK_LAST");
		}
		
		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;
	}
}
