package TestingSystem.DVM.DVMTasks;
import Common.Constants;
import Common.Database.DBObject;
import Common.Global;
import Common.Utils.Utils;
import GlobalData.Tasks.TaskState;
import TestingSystem.Common.Group.Group;
import TestingSystem.Common.Test.Test;
import TestingSystem.Common.Test.TestType;
import TestingSystem.DVM.Configuration.Configuration;
import com.google.gson.annotations.Expose;

import java.io.File;
import java.nio.file.Paths;
import java.util.Vector;
public class DVMRunTask extends DVMTask {
    @Expose
    public int dvmcompilationtask_id = Constants.Nan;
    @Expose
    public String matrix = "";
    @Expose
    public String args = "";
    @Expose
    public double CleanTime = 0.0;
    @Expose
    public int progress = 0;
    @Expose
    public int cube = 1;
    @Expose
    public int min_dim = 1;
    @Expose
    public int max_dim = 1;
    @Expose
    public String environments = "";
    @Expose
    public String usr_par = "";
    @Expose
    public int compilation_maxtime = 40;
    @Expose
    public TaskState compilation_state = TaskState.Waiting;
    @Expose
    public double compilation_time = 0.0;
    public DVMRunTask(Configuration configuration,
                      Group group, Test test,
                      String matrix_in, String flags_in,
                      String environments_in,
                      String par_in,
                      int kernels_in
    ) {
        super(configuration, group, test, flags_in);
        //--------------------------
        //инфа о компиляции.
        compilation_maxtime = configuration.c_maxtime;
        compilation_state = TaskState.Waiting;
        //инфа о запуске
        cube = configuration.cube;
        min_dim = configuration.max_dim_proc_count;
        max_dim = configuration.max_dim_proc_count;
        maxtime = configuration.r_maxtime;
        environments = environments_in;
        usr_par = par_in;
        args = test.args;
        //---------
        matrix = matrix_in;
        kernels = (test_type == TestType.Performance) ? kernels_in :
                Math.min(Utils.getMatrixProcessors(matrix), kernels_in);
    }
    public DVMRunTask() {
    }
    @Override
    public void SynchronizeFields(DBObject src) {
        super.SynchronizeFields(src);
        DVMRunTask rt = (DVMRunTask) src;
        dvmcompilationtask_id = rt.dvmcompilationtask_id;
        matrix = rt.matrix;
        CleanTime = rt.CleanTime;
        progress = rt.progress;
        language = rt.language;
        cube = rt.cube;
        min_dim = rt.min_dim;
        max_dim = rt.max_dim;
        maxtime = rt.maxtime;
        environments = rt.environments;
        usr_par = rt.usr_par;
        compilation_maxtime = rt.compilation_maxtime;
        compilation_state = rt.compilation_state;
        compilation_time = rt.compilation_time;
        args = rt.args;
    }
    public DVMRunTask(DVMRunTask src) {
        this.SynchronizeFields(src);
    }
    //-
    @Override
    public Vector<String> pack(Object arg) {
        Vector<String> res = new Vector<>();
        res.add(String.valueOf(id)); //1
        res.add(String.valueOf(maxtime)); //2
        res.add(String.valueOf(dvmcompilationtask_id)); //3
        res.add(matrix); //4
        res.add(environments); //5
        res.add(usr_par.replace("\n", "|")); //6
        res.add(args); //7
        res.add(String.valueOf(kernels)); //8
        return res;
    }
    public String getEnvironments() {
        return environments.replace("\n", ";");
    }
    public String getUsrPar() {
        return usr_par.replace("\n", ";");
    }
    @Override
    public boolean isVisible() {
        return Global.testingServer.db.dvmRunTasks.applyFilters(this);
    }
    public File getCompilationTaskWorkspace() {
        return Paths.get(
                Global.DVMPackagesDirectory.getAbsolutePath(),
                String.valueOf(dvm_package_id),
                "results",
                String.valueOf(dvmcompilationtask_id)
        ).toFile();
    }
    public String getCompilationOutput() {
        return getResultFile(new File(getCompilationTaskWorkspace(),Constants.out_file));
    }
    public String getCompilationErrors() {
        return getResultFile(new File(getCompilationTaskWorkspace(),Constants.err_file));
    }
    public String getStatistic() {
        return getResultFile(new File(getLocalWorkspace(), Constants.statistic + ".txt"));
    }
}
//--
