package _VisualDVM.TestingSystem.DVM.DVMTasks;
import Common.CommonConstants;
import Common.Database.Objects.DBObject;
import Common.Utils.Index;
import _VisualDVM.Constants;
import _VisualDVM.Global;
import _VisualDVM.GlobalData.Tasks.TaskState;
import _VisualDVM.TestingSystem.Common.Group.Group;
import _VisualDVM.TestingSystem.Common.Test.Test;
import _VisualDVM.TestingSystem.Common.Test.TestType;
import _VisualDVM.TestingSystem.DVM.DVMConfiguration.DVMConfiguration;
import _VisualDVM.Utils;
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 = CommonConstants.Nan;
    @Expose
    public int ethalon_id = CommonConstants.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 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(Index index,
                      DVMCompilationTask dvmCompilationTask,
                      DVMConfiguration configuration, int cube_in, String paramsText_in,
                      Group group, Test test,
                      String matrix_in, String flags_in,
                      String environments_in,
                      int kernels_in
    ) {
        super(index, configuration, group, test, flags_in);
        //--------------------------
        dvmcompilationtask_id = dvmCompilationTask.id;
        //инфа о компиляции.
        compilation_maxtime = configuration.c_maxtime;
        compilation_state = TaskState.Waiting;
        //инфа о запуске
        cube = cube_in;
        usr_par = paramsText_in;
        maxtime = configuration.maxtime;
        environments = environments_in;
        args = test.args;
        //---------
        matrix = matrix_in;
        kernels = (test_type == TestType.Performance) ? kernels_in :
                Math.min(Utils.getMatrixProcessors(matrix), kernels_in);
    }
    public DVMRunTask() {
    }
    public DVMRunTask(DVMRunTask src) {
        this.SynchronizeFields(src);
    }
    @Override
    public void SynchronizeFields(DBObject src) {
        super.SynchronizeFields(src);
        DVMRunTask rt = (DVMRunTask) src;
        dvmcompilationtask_id = rt.dvmcompilationtask_id;
        ethalon_id = rt.ethalon_id;
        matrix = rt.matrix;
        CleanTime = rt.CleanTime;
        progress = rt.progress;
        language = rt.language;
        cube = rt.cube;
        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 String getEnvironments() {
        return environments.replace("\n", ";");
    }
    public String getUsrPar() {
        return usr_par.replace("\n", ";");
    }
    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"));
    }
}
//--
