package _VisualDVM.TestingSystem.Common.ThreadsPlanner;
import Common.Utils.InterruptThread;
import Common.Utils.Utils_;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.util.LinkedHashMap;
import java.util.Vector;
public abstract class ThreadsPlanner {
    //-->
    protected Thread interruptThread = new InterruptThread(5000, () -> {
        try {
            Interrupt();
        } catch (Exception exception) {
            Utils_.MainLog.PrintException(exception);
        }
        System.exit(0);
        return null;
    });
    protected int maxKernels;
    protected int kernels;
    //---
    protected int threadMaxId = 0;
    protected int wait_ms;
    protected int done_threads = 0;
    protected int progress = 0;
    protected LinkedHashMap<Integer, Thread> threads = new LinkedHashMap<>();
    protected Vector<Integer> activeThreads = new Vector<>();
    protected Vector<Integer> waitingThreads = new Vector<>();
    //--
    public ThreadsPlanner(int wait_ms_in) {
        wait_ms = wait_ms_in;
    }
    public void setMaxKernels(int maxKernels_in) {
        maxKernels = maxKernels_in;
        kernels = maxKernels;
    }
    public String printThread(Integer id) {
        return "thread id = " + id;
    }
    public String getThreadsSummary() {
        Vector<String> lines = new Vector<>();
        lines.add("Planner summary:");
        lines.add("Waiting: " + waitingThreads.size());
        lines.add("Running: " + activeThreads.size());
        for (Integer id : activeThreads) {
            lines.add(printThread(id));
        }
        lines.add("");
        return String.join("\n", lines);
    }
    //--
    public void Start() {
        Utils_.MainLog.Print("Planner started");
        try {
            //--
            while (!waitingThreads.isEmpty() || !activeThreads.isEmpty()) {
                Utils_.MainLog.Print(getThreadsSummary());
                checkActiveThreads();
                tryStartThreads();
                Thread.sleep(wait_ms);
            }
            //--
        } catch (Exception exception) {
            Utils_.MainLog.PrintException(exception);
        } finally {
            Utils_.MainLog.Print("Planner finished");
            finalize();
        }
    }
    public void Interrupt() throws Exception {
    }
    protected void checkActiveThreads() throws Exception {
        Vector<Integer> toExclude = new Vector<>();
        //--
        for (int i : activeThreads) {
            Thread thread = threads.get(i);
            if (!thread.isAlive()) {
                toExclude.add(i);
                kernels++;
                done_threads++;
            }
        }
        activeThreads.removeAll(toExclude);
        //--
        double progress = ((double) done_threads / threads.size()) * 100.0;
        Utils_.MainLog.Print("done_threads=" + done_threads + ";all_threads=" + threads.size() + ";progress=" + progress);
        File progress_file = new File("progress");
        try {
            FileUtils.writeStringToFile(progress_file, String.valueOf(((int) progress)));
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    protected void tryStartThreads() throws Exception {
        Vector<Integer> toExclude = new Vector<>();
        //-
        for (int i : waitingThreads) {
            if (kernels > 0) {
                Thread thread = threads.get(i);
                thread.start();
                activeThreads.add(i);
                kernels--;
                toExclude.add(i);
            } else break;
        }
        waitingThreads.removeAll(toExclude);
    }
    protected void finalize() {
    }
    protected void addThread(Thread thread) {
        threads.put(threadMaxId, thread);
        waitingThreads.add(threadMaxId);
        threadMaxId++;
    }
}
