package _VisualDVM.TestingSystem.DVM.DVMTasks;
import Common.CommonConstants;
import Common.Database.Objects.DBObject;
import Common.Database.Objects.iDBObject;
import Common.Utils.Index;
import _VisualDVM.Constants;
import _VisualDVM.Global;
import _VisualDVM.GlobalData.Tasks.TaskState;
import _VisualDVM.ProjectData.LanguageName;
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 com.google.gson.annotations.Expose;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.nio.file.Paths;
import java.util.Vector;
public class DVMTask extends iDBObject {
    @Expose
    public int dvm_package_id = CommonConstants.Nan;
    @Expose
    public int group_id = CommonConstants.Nan;
    @Expose
    public String group_description = "";
    @Expose
    public int test_id = CommonConstants.Nan;
    @Expose
    public String test_description = "";
    @Expose
    public LanguageName language = LanguageName.fortran;
    @Expose
    public String flags = "";
    @Expose
    public int kernels = 1;
    @Expose
    public TaskState state = TaskState.Inactive;
    @Expose
    public int maxtime = 40;
    @Expose
    public TestType test_type = TestType.Default;
    //результаты-------------------------------
    @Expose
    public double Time; //время выполнения.
    public DVMTask(DVMTask src) {
        this.SynchronizeFields(src);
    }
    public DVMTask() {
    }
    public DVMTask(Index index, DVMConfiguration configuration,
                   Group group, Test test, String flags_in) {
        id = index.Inc();
        group_id = group.id;
        test_id = test.id;
        group_description = group.description;
        test_description = test.description;
        test_type = group.type;
        language = group.language;
        flags = flags_in;
    }
    //------------------------------------------------------
    @Override
    public void SynchronizeFields(DBObject src) {
        super.SynchronizeFields(src);
        DVMTask t = (DVMTask) src;
        group_id = t.group_id;
        group_description = t.group_description;
        test_id = t.test_id;
        test_description = t.test_description;
        language = t.language;
        flags = t.flags;
        kernels = t.kernels;
        state = t.state;
        maxtime = t.maxtime;
        test_type = t.test_type;
        Time = t.Time;
    }
    public File getLocalWorkspace() {
        return
                Paths.get(Global.DVMPackagesDirectory.getAbsolutePath(),
                        String.valueOf(dvm_package_id),
                        "results",
                        String.valueOf(id)).toFile();
    }
    public String getResultFile(File resultFile) {
        String res = "";
        if (dvm_package_id == CommonConstants.Nan) res = "задача ещё не выполнялась";
        else {
            if (resultFile.exists()) {
                try {
                    res = FileUtils.readFileToString(resultFile);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            } else res = "не существует";
        }
        return res;
    }
    public String getOutput() {
        return getResultFile(new File(getLocalWorkspace(), Constants.out_file));
    }
    public String getErrors() {
        return getResultFile(new File(getLocalWorkspace(), Constants.err_file));
    }
}
