package _VisualDVM.TestingSystem.SAPFOR.SapforTasksPackage.UI;
import Common.Visual.Trees.DataTree;
import _VisualDVM.Current;
import _VisualDVM.Global;
import _VisualDVM.TestingSystem.SAPFOR.Json.SapforVersion_json;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import java.util.Vector;
public class SapforTasksPackageTree extends DataTree {
    Current current;
    SapforTasksPackageTree slave_tree = null;
    public SapforTasksPackageTree(DefaultMutableTreeNode root_in, Current current_in) {
        super(root_in);
        current = current_in;
    }
    public void setSlaveTree(SapforTasksPackageTree slave_tree_in) {
        slave_tree = slave_tree_in;
    }
    @Override
    protected int getStartLine() {
        return 1;
    }
    @Override
    public Class getRendererClass() {
        return RendererTreeSapforVersions.class;
    }
    @Override
    public Current currentName() {
        return current;
    }
    public void selectSamePath_r(TreePath example, int index, DefaultMutableTreeNode node, Vector<DefaultMutableTreeNode> res) {
        if (index < example.getPathCount()) {
            DefaultMutableTreeNode exampleNode = (DefaultMutableTreeNode) example.getPathComponent(index);
            if (exampleNode.toString().equals(node.toString())) {
                res.add(node);
                for (int i = 0; i < node.getChildCount(); ++i)
                    selectSamePath_r(example, index + 1, (DefaultMutableTreeNode) node.getChildAt(i), res);
            }
        }
    }
    public void selectSamePath(TreePath path_in) {
        Vector<DefaultMutableTreeNode> pathNodes = new Vector<>();
        selectSamePath_r(path_in, 0, root, pathNodes);
        if (!pathNodes.isEmpty()) {
            TreePath path = new TreePath(pathNodes.toArray());
            setSelectionPath(path);
        }
    }
    @Override
    public void SelectionAction(TreePath e) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getLastPathComponent();
        Object o = node.getUserObject();
        //---
        if (slave_tree != null) {
            slave_tree.selectSamePath(e);
            slave_tree.scrollPathToVisible(slave_tree.getSelectionPath());
            //   System.out.println(getSelectionPath());
            //   System.out.println(slave_tree.slave_tree.getSelectionPath());
        }
        //---
        if (o instanceof SapforVersion_json) {
            SapforVersion_json version = (SapforVersion_json) o;
            Global.mainModule.set(current, version);
            if (current.equals(Current.SapforEtalonVersion))
                Global.mainModule.getUI().getMainWindow().getTestingWindow().ShowCurrentSapforPackageVersionEtalon();
            else
                Global.mainModule.getUI().getMainWindow().getTestingWindow().ShowCurrentSapforPackageVersion();
            //--
        }
    }
}