package com.tsi.comm.server.process.dbms;

import com.tsi.comm.server.config.TsiCvimServerConfig;
import com.tsi.comm.server.process.AbstractTsiCvimProcess;
import com.tsi.comm.server.process.AbstractTsiCvimWorker;
import com.tsi.comm.server.repository.TsiNodeManager;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

@Slf4j
@Getter
@RequiredArgsConstructor
@Service
public class TsiCvimDbmsProcess extends AbstractTsiCvimProcess {

    private final TsiCvimServerConfig config;
    private final TsiNodeManager nodeManager;
    private ExecutorService executor = null;
    private int qSize;
    public void start() {
        final ThreadGroup workerGroup = new ThreadGroup("dbmsProcess");
        this.workers = this.config.getDbmsWorkers();
        this.qSize = this.nodeManager.size();
        if (this.workers == 1) {
            this.qSize *= 2;
        }

        this.executor = Executors.newFixedThreadPool(this.workers, new ThreadFactory() {
            private int count = 0;
            public Thread newThread(Runnable r) {
                Thread t = new Thread(workerGroup, r);
                t.setName(String.format("dbsWorker-%02d.%02d", workers, ++count));
                t.setDaemon(true);
                return t;
            }
        });

        for (int ii = 0; ii < this.workers; ii++) {
            TsiCvimDbmsWorker dbmsWorker = new TsiCvimDbmsWorker(ii, this.qSize);
            this.workerList.add(dbmsWorker);
            this.executor.submit(dbmsWorker); // 여기서 실행됨!
        }

//        for (int ii = 0; ii < this.workers; ii++) {
//            TsiCvimDbmsWorker dbmsWorker = new TsiCvimDbmsWorker(ii, qSize);
//            this.workerList.add(dbmsWorker);
//            Thread worker = new Thread(workerGroup, dbmsWorker);
//            worker.setName(String.format("dbsWorker-%02d.%02d", this.workers, ii+1));
//            worker.setDaemon(true);
//            this.threadList.add(worker);
//        }
//        for (Thread worker : this.threadList) {
//            worker.start();
//        }
    }

    public boolean add(Object object, int idx) {
        boolean offer = false;
        try {
            //idx = idx % this.workers;
            idx = Math.abs(idx % this.workers);
            offer = this.workerList.get(idx).add(object);
        }
        catch (Exception e) {
            log.error("DbmsProcess: QUEUE_DATA.add: Exception: {}, {}", object.toString(), e.getMessage());
        }
        return offer;
    }

    public void stop() {
        log.info("DbmsProcess Stopping...");
        this.executor.shutdown();
        try {
            if (!this.executor.awaitTermination(3, TimeUnit.SECONDS)) {
                this.executor.shutdownNow(); // 실행 중인 작업도 중단 시도
            }
        } catch (InterruptedException e) {
            this.executor.shutdownNow();
            Thread.currentThread().interrupt(); // 현재 스레드도 중단
        }
        log.info("DbmsProcess Stopped...");
    }

    public void report() {
        for (AbstractTsiCvimWorker worker : this.workerList) {
            worker.report();
        }
    }
}
