package TestingSystem.SAPFOR;
import Common.Constants;
import Common.Global;
import Common.Utils.Utils;
import TestingSystem.SAPFOR.Json.SapforConfiguration_json;
import TestingSystem.SAPFOR.Json.SapforTasksPackage_json;
import TestingSystem.SAPFOR.Json.SapforTasksResults_json;
import TestingSystem.SAPFOR.Json.SapforTest_json;
import TestingSystem.SAPFOR.SapforTask.SapforTask;
import TestingSystem.Common.TaskThread;
import TestingSystem.Common.ThreadsPlanner.ThreadsPlanner;
import Visual_DVM_2021.Passes.PassCode_2021;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.Vector;
public class PackageModeSupervisor extends ThreadsPlanner {
    SapforTasksPackage_json package_json = null;
    File sapfor_drv=null;

    public PackageModeSupervisor() throws Exception {
        super(2000);
        package_json = (SapforTasksPackage_json) Utils.jsonFromFile(new File(Constants.package_json), SapforTasksPackage_json.class);
        //--
        File sapfor_src = new File(package_json.sapfor_drv);
        sapfor_drv = new File(Global.Home,Utils.getDateName("SAPFOR_F"));
        FileUtils.copyFile(sapfor_src, sapfor_drv);
        if (!sapfor_drv.setExecutable(true))
            throw new Exception("Не удалось сделать файл " + sapfor_drv.getName() + " исполняемым!");
        File PID = new File( "PID");
        FileUtils.writeStringToFile(PID, sapfor_drv.getName(), Charset.defaultCharset());
        //---
        Date startDate = new Date();
        File started = new File(Constants.STARTED);
        FileUtils.writeStringToFile(started, String.valueOf(startDate));
        //формирование списка задач.
        setMaxKernels(package_json.kernels);
        int max_rask_id=0;
        for (SapforConfiguration_json sapforConfiguration_json : package_json.configurations) {
            for (SapforTest_json test : package_json.tests) {
                //--- чтобы было можно на нее сослаться после выполнения всех нитей.
                SapforTask task = new SapforTask();
                task.id = max_rask_id++;
                task.group_description = test.group_description;
                task.test_description = test.description;
                task.flags = sapforConfiguration_json.flags;
                task.sapfor_configuration_id = sapforConfiguration_json.id;
                task.sapfortaskspackage_id = Integer.parseInt(new File(Global.Home).getName());
                package_json.tasks.add(task);
                Vector<String> codes_s = new Vector<>();
                for (PassCode_2021 code : sapforConfiguration_json.codes) {
                    codes_s.add(code.toString());
                }
                task.codes = String.join(" ", codes_s);
                //---
                addThread(new TaskThread(task, sapfor_drv, sapforConfiguration_json));
            }
        }
        interruptThread.start();
    }
    @Override
    public String printThread(Integer id) {
        TaskThread taskThread = (TaskThread) threads.get(id);
        return taskThread.task.getSummary();
    }
    @Override
    protected void finalize() {
        //записать результаты всех задач.
        try {
            Utils.jsonToFile(package_json, new File(Constants.package_json));
            FileUtils.writeStringToFile(new File(Constants.DONE), "");
            //--
            //Очистка

            //очистка служебных файлов.
            Utils.deleteFilesByExtensions(new File(Global.Home),
                    "proj", "dep", "jar"
                    //  ,"sh", "exe", "bat"
            );
            //удаление сапфора

            if (sapfor_drv.exists())
                FileUtils.forceDelete(sapfor_drv);
        } catch (Exception e) {
            Global.Log.PrintException(e);
        }
        System.exit(0);
    }
}
