package _VisualDVM.TestingSystem.SAPFOR.SapforConfiguration;
import Common.CommonConstants;
import Common.Database.Objects.DBObject;
import Common.Utils.TextLog;
import _VisualDVM.Global;
import _VisualDVM.ProjectData.LanguageName;
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.SAPFOR.SapforConfigurationGroup.SapforConfigurationGroup;
import _VisualDVM.TestingSystem.SAPFOR.SapforConfigurationSettings.SapforConfigurationSettings;
import _VisualDVM.TestingSystem.SAPFOR.SapforConfigurationTest.SapforConfigurationTest;
import _VisualDVM.TestingSystem.SAPFOR.SapforSettings.SapforSettings;
import com.sun.org.glassfish.gmbal.Description;

import java.util.Vector;
public class SapforConfiguration extends Configuration {
    @Description("DEFAULT -1")
    public int ethalon_id = CommonConstants.Nan; //пакет-эталон ( в двм тестировании не нужен.)
    @Description("IGNORE")
    public Vector<SapforConfigurationGroup> groups = null;
    @Description("IGNORE")
    public Vector<SapforConfigurationTest> tests = null;
    @Description("IGNORE")
    public Vector<SapforConfigurationSettings> settings = null;
    public SapforConfiguration(SapforConfiguration sapforConfiguration) {
        this.SynchronizeFields(sapforConfiguration);
    }
    public SapforConfiguration() {
    }
    //--
    public String printEthalon() {
        return (ethalon_id == CommonConstants.Nan) ? "—" : String.valueOf(ethalon_id);
    }
    public void saveGroups(Vector<Group> groups_in) {
        groups = new Vector<>();
        for (Group group : groups_in)
            groups.add(new SapforConfigurationGroup(this, group));
    }
    @Override
    public void saveTests(Vector<Test> tests_in) {
        tests = new Vector<>();
        for (Test test : tests_in)
            tests.add(new SapforConfigurationTest(this, test));
    }
    @Override
    public void saveSettings(Vector<Settings> settings_in) {
        settings = new Vector<>();
        for (Settings sapforSettings : settings_in)
            settings.add(new SapforConfigurationSettings(this, (SapforSettings) sapforSettings));
    }
    @Override
    public void SynchronizeFields(DBObject src) {
        super.SynchronizeFields(src);
        SapforConfiguration c = (SapforConfiguration) src;
        ethalon_id = c.ethalon_id;
    }
    @Override
    public boolean validate(TextLog Log) {
        Vector<Integer> groups_ids = Global.testingServer.db.getSapforConfigurationGroupsIds(this);
        Vector<Integer> tests_ids = Global.testingServer.db.getSapforConfigurationTestsIds(this);
        Vector<Integer> settings_ids = Global.testingServer.db.getSapforConfigurationSettingsIds(this);
        Vector<Group> groups = new Vector<>();
        Vector<LanguageName> groupsLanguages = new Vector<>();
        for (int group_id : groups_ids) {
            LanguageName languageName = Global.testingServer.db.groups.get(group_id).language;
            if (!groupsLanguages.contains(languageName))
                groupsLanguages.add(languageName);
        }
        //---
        //1. проверка цепочек команд на корректность
        for (int settings_id : settings_ids) {
            Global.testingServer.db.sapforSettings.get(settings_id).validate(Log);
        }
        //2. Проверка входящих групп на единственный язык фортран
        if (groupsLanguages.size() > 1) {
            Log.Writeln_("Запуск тестов на разных языках в рамках одного пакета запрещен!\n");
        }
        if ((!groupsLanguages.contains(LanguageName.fortran))) {
            Log.Writeln_("Поддерживается пакетный режим только для языка Fortran");
        }
        //-
        Vector<String> testsDescriptions = new Vector<>();
        for (int test_id : tests_ids) {
            String l_description = Global.testingServer.db.tests.get(test_id).description.toLowerCase();
            if (testsDescriptions.contains(l_description)) {
                Log.Writeln_("В конфигурацию тестирования SAPFOR не могут входить тесты с одинаковыми именами (без учета регистра):" + l_description);
            } else {
                testsDescriptions.add(l_description);
            }
        }
        //-
        return Log.isEmpty();
    }
}
