package _VisualDVM.ServerObjectsCache;
import Common.Utils.Utils_;
import _VisualDVM.Global;
import _VisualDVM.TestingSystem.Common.Configuration.Configuration;
import _VisualDVM.TestingSystem.Common.Group.Group;
import _VisualDVM.TestingSystem.Common.Group.Json.GroupJson;
import _VisualDVM.TestingSystem.Common.Group.Json.GroupsJson;
import _VisualDVM.TestingSystem.Common.Settings.Json.SettingsArrayJson;
import _VisualDVM.TestingSystem.Common.Settings.Json.SettingsJson;
import _VisualDVM.TestingSystem.Common.Test.Json.TestJson;
import _VisualDVM.TestingSystem.Common.Test.Json.TestsJson;
import _VisualDVM.TestingSystem.Common.Test.Test;

import java.util.LinkedHashMap;
import java.util.Vector;
public class ConfigurationCache extends VisualCache {
    public GroupsJson groupsJson = null;
    public TestsJson testsJson = null;
    public SettingsArrayJson settingsJson = null;
    public String settingsSummary = null;
    public Vector<String> groupsSummary = null;
    //--
    public ConfigurationCache(Configuration configuration) {
        if (configuration.packedGroupsJson.isEmpty()) {
            groupsJson = new GroupsJson(); //просто пустой
        } else {
            groupsJson = Utils_.gson.fromJson(configuration.packedGroupsJson, GroupsJson.class);
        }
        //--
        if (testsJson == null) {
            if (configuration.packedTestsJson.isEmpty())
                testsJson = new TestsJson(); //просто пустой
            else
                testsJson = Utils_.gson.fromJson(configuration.packedTestsJson, TestsJson.class);
        }
        //-
        if (settingsJson == null) {
            if (configuration.packedSettingsJson.isEmpty())
                settingsJson = new SettingsArrayJson(); //просто пустой
            else
                settingsJson = Utils_.gson.fromJson(configuration.packedSettingsJson, SettingsArrayJson.class);
        }
        //-
        LinkedHashMap<String, Vector<String>> gmap = new LinkedHashMap<>();
        for (GroupJson groupJson : groupsJson.array) {
            Vector<String> vector = null;
            if (gmap.containsKey(groupJson.language)) {
                vector = gmap.get(groupJson.language);
            } else {
                vector = new Vector<>();
                gmap.put(groupJson.language, vector);
            }
            vector.add(groupJson.description);
        }
        groupsSummary = new Vector<>();
        for (String language : gmap.keySet()) {
            groupsSummary.add(language + ": " + String.join(";", gmap.get(language)));
        }
        Vector<String> settingsDescriptionsVector = new Vector<>();
        for (SettingsJson settingsJson : settingsJson.array)
            settingsDescriptionsVector.add(settingsJson.description);
        settingsSummary = String.join(";", settingsDescriptionsVector);
    }
    public int getTestsCount() {
        return testsJson.array.size();
    }
    public Vector<Group> getGroups() {
        Vector<Group> groups = new Vector<>();
        for (GroupJson groupJson : groupsJson.array) {
            if (Global.testingServer.db.groups.containsKey(groupJson.id))
                groups.add(Global.testingServer.db.groups.get(groupJson.id));
        }
        return groups;
    }
    public Vector<Test> getTests() {
        Vector<Test> tests = new Vector<>();
        for (TestJson testJson : testsJson.array) {
            if (Global.testingServer.db.tests.containsKey(testJson.id))
                tests.add(Global.testingServer.db.tests.get(testJson.id));
        }
        return tests;
    }
    public Vector<Test> getGroupTests(Group group) {
        Vector<Test> tests = new Vector<>();
        for (TestJson testJson : testsJson.array) {
            if (Global.testingServer.db.tests.containsKey(testJson.id)) {
                Test test = Global.testingServer.db.tests.get(testJson.id);
                if (test.group_id == group.id)
                    tests.add(test);
            }
        }
        return tests;
    }
}
