package _VisualDVM.TestingSystem.DVM.DVMTasks;
import Common.Utils.Utils_;
import _VisualDVM.TestingSystem.Common.Group.Group;
import _VisualDVM.TestingSystem.Common.Test.Test;
import _VisualDVM.TestingSystem.DVM.DVMConfiguration.DVMConfiguration;
import _VisualDVM.TestingSystem.DVM.DVMSettings.DVMSettings;
import com.google.gson.annotations.Expose;

import java.util.List;
import java.util.Vector;
public class DVMCompilationTask extends DVMTask {
    @Expose
    public List<DVMRunTask> runTasks = new Vector<>();
    //-
    public DVMCompilationTask() {

    }
    @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(test_id)); //3
        res.add(arg.toString().replace("\n", "|")); //4
        return res;
    }
    //--
    public static String checkFlags(String flags_in) {
        if (!flags_in.contains("-shared-dvm")) {
            if (flags_in.isEmpty())
                return "-shared-dvm";
            else return flags_in + " -shared-dvm";
        } else
            return flags_in;
    }
    public static String checkEnvironments(String environmentsSet_in) {
        if (!environmentsSet_in.contains("DVMH_NO_DIRECT_COPY")) {
            if (environmentsSet_in.isEmpty())
                return "DVMH_NO_DIRECT_COPY=" + Utils_.DQuotes("1");
            else
                return environmentsSet_in + " " + "DVMH_NO_DIRECT_COPY=" + Utils_.DQuotes("1");
        } else
            return environmentsSet_in;
    }
    //--
    public DVMCompilationTask(DVMConfiguration configuration, DVMSettings dvmSettings, Group group, Test test, int kernels_in){
        super (configuration, group, test,checkFlags(dvmSettings.flags));
        Vector<String> matrixes = dvmSettings.getMatrixes(test.max_dim);
        String checkedEnvironments = checkEnvironments(dvmSettings.environments);
        if (dvmSettings.flags.trim().equalsIgnoreCase("-s")) {
            runTasks.add(new DVMRunTask(
                    configuration,
                    dvmSettings,
                    group,
                    test,
                    "",
                    flags,
                    checkedEnvironments,
                    kernels_in
            ));
        } else
            for (String matrix : matrixes) {
                runTasks.add(new DVMRunTask(
                        configuration,
                        dvmSettings,
                        group,
                        test,
                        matrix,
                        flags,
                        checkedEnvironments,
                        kernels_in));
            }
    }
}
