package kr.gmtc.tss.data;

import java.util.Queue;

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

import kr.gmtc.tss.data.vo.SetDynamicVO;
import kr.gmtc.tss.db.BatchInsertManager;
import kr.gmtc.tss.util.vo.StaticVO; 

public class DataInsertRunnable implements Runnable {


	private Logger logger = LoggerFactory.getLogger(this.getClass());

	Queue<StaticVO> staticVoQueue;
	Queue<SetDynamicVO> dynamicInsertQueue;
//	final private int listChunkSize=100; 
	private boolean isRunning = false;	
	private int batchInsertSize=1000;
	
	BatchInsertManager batchInsertManager;
	private long sleepMillis;
//	private int dynamicSetSize;
//	private int dynamicRemoveUnitSize;

//	private Comparator<SetDynamicVO> comparator = Comparator.comparingLong(v -> ((LocalDateTime)v.getDateTime()).toInstant(ZoneOffset.ofHours(9)).toEpochMilli());
//	private Comparator<SetDynamicVO> comparator = Comparator.comparing( ,(a,b) -> a.getdattime())
//	Set<SetDynamicVO> dynamicSet = ConcurrentSkipListMap.n.newKeySet(10000);
//	ConcurrentSkipListSet<SetDynamicVO> dynamicSet = new ConcurrentSkipListSet<SetDynamicVO>(comparator);
	
	public DataInsertRunnable(Queue<StaticVO> staticVoQueue, Queue<SetDynamicVO> dynamicInsertQueue, int batchInsertSize, BatchInsertManager batchInsertManager, long sleepMillis) {
		this.staticVoQueue=staticVoQueue;
		this.dynamicInsertQueue=dynamicInsertQueue;
		this.batchInsertSize=batchInsertSize;
		this.batchInsertManager=batchInsertManager;
		this.sleepMillis=sleepMillis;
	}
	
	@Override
	public void run() {

		isRunning=true;
		
		while (isRunning) {
			
			try {
				
				//Dynamic
				SetDynamicVO []  chunkArr = getChunkDynamicArr(dynamicInsertQueue);
				//if(isRunning) batchInsertManager.insertDynamicData(chunkArr);
				//if(isRunning) batchInsertManager.mergeDynamicLastData(chunkArr);
				
				
				//static
				StaticVO []  chunkStaticArr =getChunkStaticArr(staticVoQueue);
				//if(isRunning) batchInsertManager.mergeStaticData(chunkStaticArr);
				
				
				
				
				
			} catch (Exception e) {
//				if (e.getMessage() != null) {
					logger.info("[DataInsertRunnable] Data Insert error : " ,e);
//				} else {
////					logger.info("[ParserThread] Parsing Error Packet Message :" + packetMsg);
//					logger.info("[ParserThread] Parsing Error Call Stack" ,e);
//					
//				}
			} 
			
			
			sleep(sleepMillis);
			
		}
	}
	

	private SetDynamicVO [] getChunkDynamicArr(Queue<SetDynamicVO> sqlQueue) {
		int loopCnt=0;
		if(sqlQueue.size()>batchInsertSize) {
			loopCnt=batchInsertSize;
		}else {
			loopCnt=sqlQueue.size();
		}
		
		SetDynamicVO [] chunkArr = new SetDynamicVO[loopCnt];
		
		for (int i = 0; i < loopCnt; i++) {
			chunkArr[i]=sqlQueue.poll();
		}
		return chunkArr;
	}
	
	

	private StaticVO [] getChunkStaticArr(Queue<StaticVO> sqlQueue) {
		int loopCnt=0;
		if(sqlQueue.size()>batchInsertSize) {
			loopCnt=batchInsertSize;
		}else {
			loopCnt=sqlQueue.size();
		}
		
		StaticVO [] chunkArr = new StaticVO[loopCnt];
		
		for (int i = 0; i < loopCnt; i++) {
			chunkArr[i]=sqlQueue.poll();
		}
		return chunkArr;
	}


	
//	private void putQueueData(DynamicVO dynamicVO) {
//		
//		removeOverCountQueue();
//		
//		SetDynamicVO vo = (SetDynamicVO)dynamicVO;
//		
//		//Set에 안 담겨 있는경우에는 insert 큐에 입력(중복 방지)
//		if(!dynamicSet.contains(vo)) {
//			dynamicSet.add(vo);
//			dynamicInsertQueue.add(vo);
//		}
//		
//		
//		
//	}
//
//	//Set 개수가 10000개가 넘어가면 과거데이터를 500개씩 제거한다.
//	private void removeOverCountQueue() {
//		
//		if(dynamicSet.size()>dynamicSetSize) {
//			int i=0;
//			for( Iterator<SetDynamicVO> itr =dynamicSet.descendingIterator(); itr.hasNext()&&i<dynamicRemoveUnitSize; )
//			{
//				dynamicSet.remove(itr.next());
//				i++;
//			}
//		}
//	}
	
	
	private void sleep(long millis) {
		try {
			Thread.sleep(millis);
		} catch (InterruptedException e) {
			logger.debug("[TargetManager] sleep error : " +e.getLocalizedMessage());
		}
	}
	

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