package TestingSystem.SAPFOR.Json;
import Common.Utils.Utils;
import GlobalData.Tasks.TaskState;
import TestingSystem.SAPFOR.SapforTask.MatchState;
import TestingSystem.SAPFOR.SapforTask.SapforTask;
import com.google.gson.annotations.Expose;

import javax.swing.tree.DefaultMutableTreeNode;
import java.io.File;
import java.io.Serializable;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Vector;
public class SapforTasksResults_json implements Serializable {
    /*
    //---
    public PackageSummary root = null;
    public PackageSummary comparison_root = null;
    //---
    @Expose
    public long StartDate = 0;
    @Expose
    public long EndDate = 0;
    @Expose
    public List<SapforTask> tasks = new Vector<>();
    //все задачи по ключам.
    public LinkedHashMap<String, SapforTask> allTasks = new LinkedHashMap<>();
    public LinkedHashMap<TaskState, LinkedHashMap<Integer, LinkedHashMap<String, Vector<SapforTask>>>> sortedTasks = new LinkedHashMap<>();
    //-- задачи, отсортированные для сравнения.
    public LinkedHashMap<MatchState, LinkedHashMap<TaskState, LinkedHashMap<Integer, LinkedHashMap<String, Vector<SapforTask>>>>> comparisonSortedTasks = new LinkedHashMap<>();
    //----
    public void buildTree(SapforTasksPackage package_in) {
        root = new PackageSummary();
        //---
        for (TaskState state : sortedTasks.keySet()) {
            //--
            StateSummary stateSummary = new StateSummary(state);
            //--
            LinkedHashMap<Integer, LinkedHashMap<String, Vector<SapforTask>>> tasksByConfigurations = sortedTasks.get(state);
            for (int configuration_id : tasksByConfigurations.keySet()) {
                //--
                DefaultMutableTreeNode configurationNode = null;
                //--
                LinkedHashMap<String, Vector<SapforTask>> groups_tasks = tasksByConfigurations.get(configuration_id);
                for (String group : groups_tasks.keySet()) {
                    //--
                    GroupSummary groupSummary = new GroupSummary(group);
                    //--
                    for (SapforTask task : groups_tasks.get(group)) {
                        //--
                        stateSummary.count++;
                        root.count++;
                        //--
                        if (configurationNode == null) {
                            configurationNode = new ConfigurationSummary(configuration_id, task);
                        }
                        //--
                        groupSummary.add(task.getVersionsTree(new File(package_in.getLocalWorkspace(), String.valueOf(configuration_id))));
                    }
                    if (configurationNode != null)
                        configurationNode.add(groupSummary);
                }
                stateSummary.add(configurationNode);
            }
            if (stateSummary.count > 0) {
                root.add(stateSummary);
            }
        }
    }
    public void buildComparisonTree(SapforTasksPackage package_in) {
        comparison_root = new PackageSummary();
        for (MatchState match_state : comparisonSortedTasks.keySet()) {
            //--
            MatchesSummary matchesSummary = new MatchesSummary(match_state);
            //---
            LinkedHashMap<TaskState, LinkedHashMap<Integer, LinkedHashMap<String, Vector<SapforTask>>>> task_states = comparisonSortedTasks.get(match_state);
            //---
            for (TaskState state : task_states.keySet()) {
                //--
                StateSummary stateSummary = new StateSummary(state);
                //--
                LinkedHashMap<Integer, LinkedHashMap<String, Vector<SapforTask>>> tasksByConfigurations = task_states.get(state);
                for (int configuration_id : tasksByConfigurations.keySet()) {
                    //--
                    DefaultMutableTreeNode configurationNode = null;
                    //--
                    LinkedHashMap<String, Vector<SapforTask>> groups_tasks = tasksByConfigurations.get(configuration_id);
                    for (String group : groups_tasks.keySet()) {
                        //--
                        GroupSummary groupSummary = new GroupSummary(group);
                        //--
                        for (SapforTask task : groups_tasks.get(group)) {
                            //--
                            stateSummary.count++;
                            matchesSummary.count++;
                            comparison_root.count++;
                            //--
                            if (configurationNode == null) {
                                configurationNode = new ConfigurationSummary(configuration_id, task);
                            }
                            //--
                            groupSummary.add(task.getVersionsTree(new File(package_in.getLocalWorkspace(), String.valueOf(configuration_id))));
                        }
                        if (configurationNode != null)
                            configurationNode.add(groupSummary);
                    }
                    stateSummary.add(configurationNode);
                }
                if (stateSummary.count > 0) {
                    matchesSummary.add(stateSummary);
                }
            }
            //---
            if (matchesSummary.count > 0) {
                comparison_root.add(matchesSummary);
            }
        }
    }
    //----
    public void SortTasks() {
        sortedTasks.clear();
        //--
        for (TaskState state : TaskState.values()) {
            LinkedHashMap<Integer, LinkedHashMap<String, Vector<SapforTask>>> configuration_tasks = new LinkedHashMap<>();
            sortedTasks.put(state, configuration_tasks);
            //--
            for (SapforTask task : tasks) {
                if (task.state.equals(state)) {
                    LinkedHashMap<String, Vector<SapforTask>> groups_tasks = null;
                    if (configuration_tasks.containsKey(task.sapfor_configuration_id)) {
                        groups_tasks = configuration_tasks.get(task.sapfor_configuration_id);
                    } else {
                        groups_tasks = new LinkedHashMap<>();
                        configuration_tasks.put(task.sapfor_configuration_id, groups_tasks);
                    }
                    Vector<SapforTask> tasks_ = null;
                    if (groups_tasks.containsKey(task.group_description)) {
                        tasks_ = groups_tasks.get(task.group_description);
                    } else {
                        tasks_ = new Vector<>();
                        groups_tasks.put(task.group_description, tasks_);
                    }
                    tasks_.add(task);
                }
            }
            //--
        }
        //--
        for (TaskState state : TaskState.values()) {
            LinkedHashMap<Integer, LinkedHashMap<String, Vector<SapforTask>>> configuration_tasks = sortedTasks.get(state);
            for (int configuration_id : configuration_tasks.keySet()) {
                LinkedHashMap<String, Vector<SapforTask>> groups_tasks = configuration_tasks.get(configuration_id);
                for (String group : groups_tasks.keySet()) {
                    Vector<SapforTask> tasks_ = groups_tasks.get(group);
                    tasks_.sort(Comparator.comparing(SapforTask::getUniqueKey));
                }
            }
        }
    }
    public void SortTasksForComparison() {
        comparisonSortedTasks.clear();
        //раскидать задачи по состояниям, конфигам, группам
        for (MatchState matchState : MatchState.values()) {
            LinkedHashMap<TaskState, LinkedHashMap<Integer, LinkedHashMap<String, Vector<SapforTask>>>> state_tasks = new LinkedHashMap<>();
            comparisonSortedTasks.put(matchState, state_tasks);
            //--
            for (TaskState state : TaskState.values()) {
                LinkedHashMap<Integer, LinkedHashMap<String, Vector<SapforTask>>> configuration_tasks = new LinkedHashMap<>();
                state_tasks.put(state, configuration_tasks);
                //--
                for (SapforTask task : tasks) {
                    if (task.match.equals(matchState) && task.state.equals(state)) {
                        LinkedHashMap<String, Vector<SapforTask>> groups_tasks = null;
                        if (configuration_tasks.containsKey(task.sapfor_configuration_id)) {
                            groups_tasks = configuration_tasks.get(task.sapfor_configuration_id);
                        } else {
                            groups_tasks = new LinkedHashMap<>();
                            configuration_tasks.put(task.sapfor_configuration_id, groups_tasks);
                        }
                        Vector<SapforTask> tasks = null;
                        if (groups_tasks.containsKey(task.group_description)) {
                            tasks = groups_tasks.get(task.group_description);
                        } else {
                            tasks = new Vector<>();
                            groups_tasks.put(task.group_description, tasks);
                        }
                        tasks.add(task);
                    }
                }
            }
            //--
        }
        //рассортировать задачи в группах по ключам.
        for (MatchState matchState : MatchState.values()) {
            LinkedHashMap<TaskState, LinkedHashMap<Integer, LinkedHashMap<String, Vector<SapforTask>>>> state_tasks = comparisonSortedTasks.get(matchState);
            for (TaskState state : TaskState.values()) {
                LinkedHashMap<Integer, LinkedHashMap<String, Vector<SapforTask>>> configuration_tasks = state_tasks.get(state);
                for (int configuration_id : configuration_tasks.keySet()) {
                    LinkedHashMap<String, Vector<SapforTask>> groups_tasks = configuration_tasks.get(configuration_id);
                    for (String group : groups_tasks.keySet()) {
                        Vector<SapforTask> tasks_ = groups_tasks.get(group);
                        tasks_.sort(Comparator.comparing(SapforTask::getUniqueKey));
                    }
                }
            }
        }
    }
    public void DropComparison() {
        comparison_root = null;
        comparisonSortedTasks.clear();
        for (SapforTask task : allTasks.values())
            task.match = MatchState.NotMatch;
    }
    //---
    public String getEmailSummary() {
        String res = "";
        Vector<String> summary_lines = new Vector<>();
        summary_lines.add("Всего задач: " + tasks.size());
        for (TaskState state : sortedTasks.keySet()) {
            LinkedHashMap<Integer, LinkedHashMap<String, Vector<SapforTask>>> tasksByConfigurations = sortedTasks.get(state);
            if (!tasksByConfigurations.isEmpty()) {
                int count = 0;
                if (!state.equals(TaskState.Done)) {
                    Vector<String> flagsLines = new Vector<>();
                    for (int configuration_id : tasksByConfigurations.keySet()) {
                        LinkedHashMap<String, Vector<SapforTask>> tasksByGroups = tasksByConfigurations.get(configuration_id);
                        for (String group : tasksByGroups.keySet()) {
                            Vector<SapforTask> tasks = tasksByGroups.get(group);
                            flagsLines.add("Группа " + group + ": " + tasks.size());
                            count += tasks.size();
                            for (SapforTask task : tasks) {
                                task.versionsDescription = task.getVersionsChain();
                                flagsLines.add(
                                        "тест: " +
                                                Utils.Brackets(task.test_description) + " " +
                                                "флаги: "
                                                + Utils.Brackets(task.flags) + " " +
                                                "версии: " +
                                                task.versionsDescription
                                        //  + " " +   "конфигурация " + task.sapfor_configuration_id
                                );
                            }
                        }
                    }
                    summary_lines.add(state.getDescription() + " :" + count);
                    summary_lines.addAll(flagsLines);
                } else {
                    for (int configurationId : tasksByConfigurations.keySet()) {
                        LinkedHashMap<String, Vector<SapforTask>> tasksByGroups = tasksByConfigurations.get(configurationId);
                        for (String group : tasksByGroups.keySet()) {
                            Vector<SapforTask> tasks = tasksByGroups.get(group);
                            for (SapforTask task : tasks)
                                task.versionsDescription = task.getVersionsChain();
                            count += tasks.size();
                        }
                    }
                    summary_lines.add(state.getDescription() + " :" + count);
                }
            }
        }
        res = String.join("\n", summary_lines);
        return res;
    }
    //---
     */
}