package SapforTestingSystem.SapforTasksPackage;
import Common.Constants;
import Common.Global;
import Common.Utils.Utils;
import GlobalData.Tasks.TaskState;
import SapforTestingSystem.Json.SapforTasksResults_json;
import SapforTestingSystem.SapforTask.SapforTask;
import SapforTestingSystem.SapforTasksPackage.UI.ConfigurationSummary;
import SapforTestingSystem.SapforTasksPackage.UI.GroupSummary;
import SapforTestingSystem.SapforTasksPackage.UI.StateSummary;

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 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 getLocalWorkspace(SapforTasksPackage package_in) {
        return new File(Global.SapforPackagesDirectory, package_in.id);
    }
    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();
    }
    public static DefaultMutableTreeNode buildTree(SapforTasksPackage package_in) {
        SapforTasksResults_json results_json = getLocalResults(package_in);
        //--  флаги                 группы
        LinkedHashMap<TaskState, LinkedHashMap<String, LinkedHashMap<String, Vector<SapforTask>>>> sortedTasks = results_json.sortTasksByStatesConfigurationsAndGroups();
        //--
        DefaultMutableTreeNode root = new DefaultMutableTreeNode(package_in.id);
        for (TaskState state : sortedTasks.keySet()) {
            //--
            StateSummary stateSummary = new StateSummary(state);
            //--
            DefaultMutableTreeNode stateNode = new DefaultMutableTreeNode(stateSummary);
            LinkedHashMap<String, LinkedHashMap<String, Vector<SapforTask>>> tasksByConfigurations = sortedTasks.get(state);
            for (String configuration_id : tasksByConfigurations.keySet()) {
                //--
                ConfigurationSummary configurationSummary = new ConfigurationSummary();
                DefaultMutableTreeNode configurationNode = new DefaultMutableTreeNode(configurationSummary);
                //--
                LinkedHashMap<String, Vector<SapforTask>> groups_tasks = tasksByConfigurations.get(configuration_id);
                for (String group : groups_tasks.keySet()) {
                    //--
                    GroupSummary groupSummary = new GroupSummary(group);
                    DefaultMutableTreeNode groupNode = new DefaultMutableTreeNode(groupSummary);
                    //--
                    for (SapforTask task : groups_tasks.get(group)) {
                        //--
                        stateSummary.count++;
                        //--
                        if (configurationSummary.text.isEmpty())
                            configurationSummary.init(configuration_id, task);
                        //--
                        groupNode.add(task.getVersionsTree(new File(getLocalWorkspace(package_in), configuration_id)));

                    }
                    configurationNode.add(groupNode);
                }
                stateNode.add(configurationNode);
            }
            if (stateSummary.count > 0) {
                stateSummary.refreshText();
                root.add(stateNode);
            }
        }
        return root;
    }
    public static DefaultMutableTreeNode getTree(SapforTasksPackage package_in) {
        if (package_in.root == null)
            package_in.root = buildTree(package_in);
        return package_in.root;
    }
}
