package kr.gmtc.gw.asderecv.rest;

import java.io.IOException;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Queue;

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

import kr.gmtc.gw.asderecv.rest.vo.ServiceAsdeData;
import kr.gmtc.gw.asderecv.utils.CmmnUtil;
import kr.gmtc.gw.comp.thread.CustomThread;


//@Component("serviceQueManager")
public class ServiceQueManager {

    HashMap<Integer, Queue<ServiceAsdeData>> serviceQueue;

    private boolean serviceRunnig = false;
    private Integer scvQcount;
    private Integer scvQmaxCount;
    private Integer scvQdiffTime;
    private Integer scvQclearTime;

    private CustomThread serviceQClearThread;

    protected Logger logger;


    public ServiceQueManager(HashMap<Integer, Queue<ServiceAsdeData>> serviceQueue, boolean serviceRunnig){

        this.serviceQueue = serviceQueue;
        this.serviceRunnig = serviceRunnig;

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

         // 업무 스레드 생성, 대기 없이 무한반복
        serviceQClearThread = new CustomThread("serviceQClearThread", this, CustomThread.NO_SLEEP, this::serviceQClear, null, false);
    }

    public void start(){

         // 전송용 Queue 생성
        createQueue();

        serviceQClearThread.start();

    }

    // 설정에따라 REST용 서비스 큐 생성
    @SuppressWarnings("unchecked")
    public void createQueue() {

        // String className = "java.util.Queue";
        //Class cls = Class.forName(className);
        
        for(int idx=0; idx<scvQcount; idx++) {

            Queue<ServiceAsdeData> obj = null;

            try {

                obj = (Queue<ServiceAsdeData>) java.util.LinkedList.class.newInstance();

            } catch (InstantiationException e) {
                logger.error("[ServiceQueManager] createQueue-InstantiationException : " + CmmnUtil.getStatckTrace(e));
            } catch (IllegalAccessException e) {
                logger.error("[ServiceQueManager] createQueue-IllegalAccessException : " + CmmnUtil.getStatckTrace(e));
            }

            serviceQueue.put(idx, obj);

        }

    }

    private void serviceQClear() throws InterruptedException, IOException {

        String sRecvTime = "";

        ServiceAsdeData jsonData = new ServiceAsdeData();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");

        LocalDateTime dateTime;

        int iQcnt = 0;

        if(serviceRunnig) return;

        try {

            for(int idx=0; idx<scvQcount; idx++) {

                iQcnt = serviceQueue.get(idx).size();

                if (iQcnt > scvQmaxCount  ) {

                    jsonData = serviceQueue.get(idx).peek();

                    try {
                        sRecvTime = jsonData.getRecptn_dt();
                    } catch (Exception e) {
                       // 비정상 큐 Clear
                       serviceQueue.get(idx).clear();
                    }

                    dateTime = LocalDateTime.parse(sRecvTime, formatter);

                    Duration diff = Duration.between(dateTime, LocalDateTime.now());

                    if( scvQclearTime > 0 && diff.toMillis() > scvQclearTime) { // 일정시간 미사용으로 판단
                        serviceQueue.get(idx).clear();
                        logger.info("[MainController] serviceQClear : " + (idx +1) +"번 Queue 장기 미사용으로 초기화 (" + iQcnt +"건), 설정시간:" + scvQclearTime);
                    }
                    else if( diff.toMillis() > scvQdiffTime) { // 데이터 과적재

                        iQcnt = serviceQueue.get(idx).size();
                        int iPollCnt = iQcnt - scvQmaxCount;

                        while (iQcnt - scvQmaxCount >= 0) {
                            serviceQueue.get(idx).poll();
                            iQcnt--;
                        }

                        logger.info("[MainController] serviceQpoll : " + (idx +1) +"번 Queue 일부 버림 (" + iPollCnt +"건), 설정시간:" + scvQdiffTime + " 설정건수:"+ scvQmaxCount);
                    }
                }
            }


            Thread.sleep(1000);
        } catch (InterruptedException e) {
            logger.error("[MainController] serviceQClear-InterruptedException : " + CmmnUtil.getStatckTrace(e));

        } catch (Exception e) {
            logger.error("[MainController] serviceQClear-Exception : " + CmmnUtil.getStatckTrace(e) + ">> " + sRecvTime.toString() );
        }
    }

    public void setScvQcount(Integer scvQcount) {
        this.scvQcount = scvQcount;
    }

    public void setScvQmaxCount(Integer scvQmaxCount) {
        this.scvQmaxCount = scvQmaxCount;
    }

    public void setScvQdiffTime(Integer scvQdiffTime) {
        this.scvQdiffTime = scvQdiffTime;
    }

    public void setScvQclearTime(Integer scvQclearTime) {
        this.scvQclearTime = scvQclearTime;
    }


    

    
}
