package SapforTestingSystem.SapforTasksPackage;
import Common.Constants;
import Common.Global;
import Common.Utils.Utils;
import SapforTestingSystem.Json.SapforTasksResults_json;
import SapforTestingSystem.SapforTask.SapforTask;

import javax.swing.tree.DefaultMutableTreeNode;
import java.io.File;
import java.nio.file.Paths;
import java.util.LinkedHashMap;
import java.util.Vector;
public class SapforTasksPackageInterface {
    public static File getPackageArchive(SapforTasksPackage package_in) {
        return new File(Global.SapforPackagesDirectory, package_in.id + ".zip");
    }
    public static DefaultMutableTreeNode getTreeRoot(SapforTasksPackage package_in) {

        SapforTasksResults_json results_json = getLocalResults(package_in);
        //--  флаги                 группы
        LinkedHashMap<String, LinkedHashMap<String, Vector<SapforTask>>> tasksByConfigurations = results_json.sortTasksByConfigurationsAndGroups();
        //--
        DefaultMutableTreeNode root = new DefaultMutableTreeNode(package_in.id);
        //  DefaultMutableTreeNode configurationsTitleNode = new DefaultMutableTreeNode("Конфигурации");
        //  root.add(configurationsTitleNode);
        for (String configuration_id : tasksByConfigurations.keySet()) {
            DefaultMutableTreeNode configurationNode = new DefaultMutableTreeNode(configuration_id);
            // DefaultMutableTreeNode groupsTitleNode = new DefaultMutableTreeNode("Группы тестов");
            //  Vector<DefaultMutableTreeNode> configurationDescription = null;
            LinkedHashMap<String, Vector<SapforTask>> groups_tasks = tasksByConfigurations.get(configuration_id);
            for (String group : groups_tasks.keySet()) {
                DefaultMutableTreeNode groupNode = new DefaultMutableTreeNode(group);
                for (SapforTask task : groups_tasks.get(group)) {
                    /*
                    if (configurationDescription == null)
                        configurationDescription = getTaskConfigurationDescription(task);
                     */
                    DefaultMutableTreeNode taskNode = new DefaultMutableTreeNode(task.test_description);
                    //--
                    taskNode.add(task.getVersionsTree(new File(getLocalWorkspace(package_in), configuration_id)));
                    //--
                    groupNode.add(taskNode);
                }
                /*
                if (configurationDescription != null) {
                    for (DefaultMutableTreeNode node : configurationDescription)
                        configurationNode.add(node);
                }
                 */
                //  groupsTitleNode.add(groupNode);
                configurationNode.add(groupNode);
            }
            //  configurationNode.add(groupsTitleNode);
            //   configurationsTitleNode.add(configurationNode);
            root.add(configurationNode);
        }
        return root;
    }
    public static File getLocalWorkspace(SapforTasksPackage package_in) {
        return new File(Global.SapforPackagesDirectory, package_in.id);
    }
    public static SapforTasksResults_json getLocalResults(SapforTasksPackage package_in) {
        File json_file = new File(getLocalWorkspace(package_in), Constants.results_json);
        SapforTasksResults_json res = null;
        try {
            res = (SapforTasksResults_json) Utils.jsonFromFile(json_file,
                    SapforTasksResults_json.class);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return res;
    }
    public static File getLoadedSign(SapforTasksPackage package_in) {
        return Paths.get(Global.SapforPackagesDirectory.getAbsolutePath(), package_in.id, Constants.LOADED).toFile();
    }
    public static boolean isLoaded(SapforTasksPackage package_in) {
        return getLoadedSign(package_in).exists();
    }
}
