/*public object EditorDataContext
         *      {
         *              get { return (object)GetValue(EditorDataContextProperty); }
         *              set { SetValue(EditorDataContextProperty, value); }
         *      }*/
        #endregion

        public AjuroJsonEditorBusiness()
        {
            // InitializeComponent();
            MainViewModel            = WizardModel.Instance;
            WizardModel.DataJsonPath = "C:\\OTB\\templates\\profile.json";
            if (!File.Exists(WizardModel.DataJsonPath))
            {
                WizardModel.DataJsonPath = "Resources\\data.json";
            }


            if (File.Exists(WizardModel.DataJsonPath))
            {
                MainViewModel.SampleJson = File.ReadAllText(WizardModel.DataJsonPath);
                SampleJson = JObject.Parse(MainViewModel.SampleJson);
                MainViewModel.SampleTree = UniversalTreeNode.CreateTree(SampleJson);
                MainViewModel.SampleJson = JsonConvert.SerializeObject(SampleJson, Formatting.Indented);
            }

            if (!File.Exists("Resources\\meta2.json"))
            {
                var stream = File.Create("Resources\\meta2.json");
                stream.Close();
                File.WriteAllText("Resources\\meta2.json", "{}");
            }

            if (File.Exists("Resources\\meta2.json"))
            {
                MainViewModel.MetaJson = File.ReadAllText("Resources\\meta2.json");
                var metaJson = JObject.Parse(MainViewModel.MetaJson);
                MainViewModel.MetaJson = JsonConvert.SerializeObject(metaJson, Formatting.Indented);
            }
            var steps = Newtonsoft.Json.JsonConvert.DeserializeObject <WizardStep>(MainViewModel.MetaJson).Children;

            if (steps == null)
            {
                steps = new ObservableCollection <WizardStep>();
            }
            foreach (var step in steps)
            {
                WizardModel.Instance.WizardSteps.Add(step);
            }
            this.EditorDataContext = MainViewModel;
        }
        public List <UniversalTreeNode> FindTreeViewNode(UniversalTreeNode node, string[] path, int index, string label, string name, int i = 1)
        {
            List <UniversalTreeNode> selectedItem = new List <UniversalTreeNode>();

            foreach (var item in node.Children)
            {
                var pathFragment = path[i].Split('[');
                if (pathFragment.Length > 1)
                {
                    pathFragment[1] = pathFragment[1].Substring(0, pathFragment[1].Length - 1);
                }
                var uitem = (UniversalTreeNode)item;
                if (uitem.Name == pathFragment[0])
                {
                    selectedItem.Add(uitem);
                    uitem.IsExpanded = true;
                    if (pathFragment.Length > 1 && pathFragment[1] != "*")
                    {
                        int idx = -1;

                        if (int.TryParse(pathFragment[1], out idx))
                        {
                            for (var t = 0; t < uitem.Children.Count; t++)
                            {
                                if (t == idx)
                                {
                                    uitem.Children[t].IsExpanded = true;
                                    uitem.Children[t].IsSelected = true;
                                    MainViewModel.SelectedNode   = uitem.Children[t];

                                    if (label != null)
                                    {
                                        for (int ii = 0; ii < uitem.Children[t].Children.Count; ii++)
                                        {
                                            if (uitem.Children[t].Children[ii].Name == label)
                                            {
                                                uitem.Children[t].Children[ii].IsSelected = true;
                                                MainViewModel.SelectedNode = uitem.Children[t].Children[ii];
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    uitem.Children[t].IsExpanded = false;
                                }
                            }

                            if (i < path.Length - 1)
                            {
                                return(FindTreeViewNode(uitem.Children[idx], path, index, label, name, i + 1));
                            }
                        }
                    }

                    if (i == path.Length - 2)
                    {
                        for (var t = 0; t < uitem.Children.Count; t++)
                        {
                            if (t == index)
                            {
                                uitem.Children[t].IsExpanded = true;

                                for (var p = 0; p < uitem.Children[t].Children.Count; p++)
                                {
                                    if (uitem.Children[t].Children[p].Name == path[path.Length - 1])
                                    {
                                        uitem.Children[t].Children[p].IsSelected = true;
                                        MainViewModel.SelectedNode = uitem.Children[t].Children[p];
                                    }
                                }
                            }
                            else
                            {
                                uitem.Children[t].IsExpanded = false;
                            }
                        }
                    }
                    else if (path.Length == 2 && label.StartsWith("."))
                    {
                        uitem.IsSelected           = true;
                        MainViewModel.SelectedNode = uitem;
                    }
                }
                else
                {
                    uitem.IsExpanded = false;
                }
            }
            return(selectedItem);
        }