package _VisualDVM.TestingSystem.DVM.DVMPackage;
import Common.CommonConstants;
import _VisualDVM.Current;
import Common.Database.Objects.DBObject;
import _VisualDVM.Global;
import _VisualDVM.ServerObjectsCache.DVMConfigurationCache;
import _VisualDVM.ServerObjectsCache.VisualCaches;
import _VisualDVM.GlobalData.Account.Account;
import _VisualDVM.GlobalData.Compiler.Compiler;
import _VisualDVM.GlobalData.Machine.Machine;
import _VisualDVM.GlobalData.Machine.MachineType;
import _VisualDVM.GlobalData.User.User;
import _VisualDVM.TestingSystem.Common.Group.Group;
import _VisualDVM.TestingSystem.Common.TasksPackageState;
import _VisualDVM.TestingSystem.Common.Test.Test;
import _VisualDVM.TestingSystem.Common.TestingPackage.TestingPackage;
import _VisualDVM.TestingSystem.DVM.DVMConfiguration.DVMConfiguration;
import _VisualDVM.TestingSystem.DVM.DVMSettings.DVMSettings;
import _VisualDVM.TestingSystem.DVM.DVMTasks.DVMCompilationTask;

import java.io.File;
import java.util.Vector;
public class DVMPackage extends TestingPackage<DVMPackage_json> {
    //---
    public String machine_name = "";
    public String machine_address = "";
    public int machine_port = 22;
    //---
    public String user_name = "";
    public String user_password;
    public String user_workspace;
    //---
    public DVMPackage() {
    }
    public DVMPackage(Account account,
                      Machine machine,
                      User user,
                      Compiler compiler,
                      Vector<DVMConfiguration> configurations,
                      int neeedsEmail_in) {
        id = CommonConstants.Nan;
        //-
        sender_name = account.name;
        sender_address = account.email;
        //-
        machine_name = machine.name;
        machine_address = machine.address;
        machine_port = machine.port;
        //-
        user_name = user.login;
        user_password = user.password;
        user_workspace = user.workspace;
        //-
        drv = compiler.call_command;
        version = compiler.getVersionInfo();
        //-
        needsEmail = neeedsEmail_in;
        //--
        state = TasksPackageState.Queued;
        //--
        for (DVMConfiguration configuration : configurations)
            kernels = Math.max(configuration.kernels, kernels);
        //-
        tasksCount = 0;
        Vector<DVMCompilationTask> compilationTasks = new Vector<DVMCompilationTask>();
        for (DVMConfiguration configuration : configurations) {
            DVMConfigurationCache cache = (DVMConfigurationCache) VisualCaches.GetCache(configuration);
            for (Group group : cache.getGroups()) {
                for (Test test : cache.getGroupTests(group)) {
                    for (DVMSettings dvmSettings : cache.getSettings()) {
                        DVMCompilationTask compilationTask = new DVMCompilationTask(configuration, dvmSettings, group, test, kernels);
                        compilationTasks.add(compilationTask);
                        tasksCount += compilationTask.runTasks.size();
                    }
                }
            }
        }
        package_json = new DVMPackage_json(compilationTasks);
        saveConfigurationsAsJson(configurations);
    }
    @Override
    public Class getJsonClass() {
        return DVMPackage_json.class;
    }
    @Override
    public File getHomeDirectory() {
        return Global.DVMPackagesDirectory;
    }
    public DVMPackage(DVMPackage p) {
        super(p);
        this.SynchronizeFields(p);
    }
    //---
    @Override
    public void SynchronizeFields(DBObject src) {
        super.SynchronizeFields(src);
        DVMPackage tasksPackage = (DVMPackage) src;
        machine_name = tasksPackage.machine_name;
        machine_address = tasksPackage.machine_address;
        machine_port = tasksPackage.machine_port;
        user_name = tasksPackage.user_name;
        user_workspace = tasksPackage.user_workspace;
        user_password = tasksPackage.user_password;
    }
    public Machine getMachine() {
        return new Machine(machine_name, machine_address, machine_port, MachineType.Server);
    }
    public User getUser() {
        return new User(user_name, user_password, user_workspace);
    }
    @Override
    public boolean isVisible() {
        return (!DVMPackageDBTable.filterMyOnly || Global.mainModule.getAccount().email.equals(sender_address)) &&
                (!DVMPackageDBTable.filterActive || state.isActive());
    }
    @Override
    public void checkFinishState() throws Exception {

    }
}

