package _VisualDVM.Passes.Testing;
import _VisualDVM.Global;
import _VisualDVM.Passes.Server.EditServerObject;
import _VisualDVM.TestingSystem.Common.Configuration.Configuration;
import _VisualDVM.TestingSystem.Common.Group.Group;
import _VisualDVM.TestingSystem.Common.Settings.Settings;
import _VisualDVM.TestingSystem.Common.Test.Test;
import _VisualDVM.TestingSystem.Common.TestingServer;

import java.util.Vector;
public abstract class SaveCurrentConfiguration<C extends Configuration, S extends Settings> extends EditServerObject<TestingServer, C> {
    Vector<Group> groups;
    Vector<Test> tests;
    Class<S> s;
    Vector<Settings> settings;
    //--
    public SaveCurrentConfiguration(Class<C> d_in, Class<S> s_in) {
        super(Global.testingServer, d_in);
        s = s_in;
    }
    @Override
    public String getIconPath() {
        return "/Common/icons/Save.png";
    }
    @Override
    public String getDescription_() {
        return "сохранение тестов и параметров";
    }
    protected abstract Class currentClass();
    @Override
    protected boolean canStart(Object... args) throws Exception {
        if (!Global.testingServer.db.getTable(currentClass()).getUI().canModifyCurrent(Log)) return false;
        target = (C) Global.testingServer.db.getTable(currentClass()).getUI().getCurrent();
        //--
        groups = new Vector<>();
        tests = new Vector<>();
        settings = new Vector<>();
        //---
        for (Group group : Global.testingServer.db.groups.getUI().getSelectedItems()) {
            groups.add(group);
            Vector<Test> groupTests = Global.testingServer.db.tests.getSelectedGroupTests(group);
            tests.addAll(groupTests);
        }
        for (Object setting : Global.testingServer.db.getTable(s).getUI().getSelectedItems()) {
            settings.add((Settings) setting);
        }
        return true;
    }
    @Override
    protected void ServerAction() throws Exception {
        //занесение информации об участвующих группах конфигурациях и тестах
        target.saveGroups(groups);
        target.saveTests(tests);
        target.saveSettings(settings);
        //--
        super.ServerAction();
    }
}