private WebElementInfoViewModel CreateReference()
        {
            var blockedTypesToPick = WebElementsViewModelsHelper.GetBlockedElementTypesForElementType(_elementType);

            var picker = new WebElementPickerDialog(_webElementsTreeUserControl.WebElements.ToList(),
                                                    false,
                                                    null,
                                                    null,
                                                    blockedTypesToPick);

            if (picker.ShowDialog() != true)
            {
                return(null);
            }
            var referenceTreePath = picker.SelectedWebElementTreePath;
            var referencedElement = (WebElementInfoViewModel)_webElementsTreeUserControl.WebElements.FindNodeByTreePath(referenceTreePath);
            var referenceCopy     = WebElementsViewModelsHelper.CreateFullModelCopy(referencedElement);

            var templateInfo = WebElementsViewModelsHelper.CreateModelFromWebElementType(WebElementTypes.Reference)
                               as WebElementWithReferenceViewModel;

            WebElementsViewModelsHelper.FillModelWithBaseInfo(templateInfo, referencedElement);
            templateInfo.Locator              = null;
            templateInfo.ElementType          = WebElementTypes.Reference;
            templateInfo.ReferenceBreadString = referenceTreePath;
            templateInfo.ReferencedWebElement = referenceCopy;

            var createdElement = CreateWebElementInfo(null, templateInfo) as WebElementWithReferenceViewModel;

            if (createdElement == null)
            {
                return(null);
            }

            createdElement.ReferencedWebElement = referenceCopy;
            if (createdElement.Elements == null)
            {
                createdElement.Elements = new ObservableCollection <WebElementInfoViewModel>();
            }
            referenceCopy.Parent = createdElement;

            return(createdElement);
        }
        private void EditReferencedElementButton_Click(object sender, RoutedEventArgs e)
        {
            if (WebElements == null ||
                !(WebElement is WebElementWithReferenceViewModel rm))
            {
                MessageBox.Show("Edit operaion is not supported. WebElements source is not specified or element doesn't support references");
                return;
            }

            var blockedTreePath = new List <string>();

            var curParent = WebElement;

            while (!(curParent?.Parent == null ||
                     curParent.Parent.ElementType == WebElementTypes.Directory))
            {
                curParent = curParent.Parent;
            }

            blockedTreePath.Clear();
            blockedTreePath.Add(curParent.GetTreePath());

            var picker = new WebElementPickerDialog(WebElements.ToList(),
                                                    WebElement.ElementType == WebElementTypes.Frame,
                                                    rm.ReferenceBreadString,
                                                    blockedTreePath,
                                                    WebElementsViewModelsHelper.GetBlockedElementTypesForElementType(rm.ElementType)
                                                    );

            if (picker.ShowDialog() != true)
            {
                return;
            }

            if (rm.ReferenceBreadString == picker.SelectedWebElementTreePath)
            {
                return;
            }

            rm.ReferenceBreadString = picker.SelectedWebElementTreePath;

            var r    = (WebElementInfoViewModel)WebElements.FindNodeByTreePath(rm.ReferenceBreadString);
            var copy = WebElementsViewModelsHelper.CreateFullModelCopy(r);

            if (rm.ElementType == WebElementTypes.Reference)
            {
                var baseInfo = new WebElementInfoViewModel
                {
                    Name        = copy.Name,
                    Description = copy.Description,
                    Tags        = copy.Tags,
                    ElementType = null,
                    Locator     = null,
                    InnerKey    = null,
                };

                WebElementsViewModelsHelper.FillModelWithBaseInfo(rm, baseInfo, true);
            }

            rm.Elements?.Clear();
            if (rm.Elements == null)
            {
                rm.Elements = new ObservableCollection <WebElementInfoViewModel>();
            }

            if (rm.ElementType == WebElementTypes.Reference)
            {
                if (copy is CombinedWebElementInfoViewModel combinedRef)
                {
                    if (combinedRef.Elements != null)
                    {
                        foreach (var c in combinedRef.Elements)
                        {
                            rm.Elements.Add(c);
                            c.Parent = rm;
                        }
                    }
                }
            }
            else
            {
                if (rm.ReferencedWebElement != null)
                {
                    rm.ReferencedWebElement.Parent = null;
                    rm.ReferencedWebElement        = null;
                }

                rm.Elements.Add(copy);
            }

            rm.ReferencedWebElement = copy;
            copy.Parent             = rm;
            rm.HasLocator           = false;

            WebElement = WebElementsViewModelsHelper.CreateFullModelCopy(rm);
            UpdateLayout();
            WebElement = rm;
        }
        protected override void ExecuteCommand()
        {
            WebElementInfoViewModel createdElement = null;

            if (_elementType == WebElementTypes.Reference)
            {
                createdElement = CreateReference();
                if (createdElement == null)
                {
                    return;
                }
            }
            else
            {
                createdElement = CreateWebElementInfo();

                if (createdElement == null)
                {
                    return;
                }

                if (_elementType == WebElementTypes.DropDown ||
                    _elementType == WebElementTypes.RadioGroup)
                {
                    var combinedCreatedElement = createdElement as CombinedWebElementInfoViewModel;

                    if (combinedCreatedElement.Elements == null)
                    {
                        combinedCreatedElement.Elements = new ObservableCollection <WebElementInfoViewModel>();
                    }

                    if (_elementType == WebElementTypes.DropDown)
                    {
                        var inputTemplate = new WebElementInfoViewModel
                        {
                            Name        = $"{createdElement.Name} Input",
                            Description = $"Input for {createdElement.Name}",
                            InnerKey    = DropDownElementInfo.Keys.Input,
                            ElementType = WebElementTypes.Element,
                            IsKey       = true,
                            Locator     = new WebLocatorInfoViewModel
                            {
                                IsRelative   = true,
                                LocatorType  = WebLocatorType.XPath,
                                LocatorValue = ".//"
                            }
                        };

                        var inputElement = CreateWebElementInfo(
                            combinedCreatedElement,
                            inputTemplate);
                        if (inputElement == null)
                        {
                            return;
                        }

                        inputElement.Parent = combinedCreatedElement;
                        combinedCreatedElement.Elements.Add(inputElement);
                    }

                    var optionTemplate = new WebElementInfoViewModel
                    {
                        Name        = $"{createdElement.Name} Option",
                        Description = $"Option for {createdElement.Name}",
                        InnerKey    = _elementType == WebElementTypes.DropDown
                            ? DropDownElementInfo.Keys.Option
                            : RadioGroupElementInfo.Keys.Option,
                        ElementType = WebElementTypes.Element,
                        IsKey       = true,
                        Locator     = new WebLocatorInfoViewModel
                        {
                            IsRelative   = true,
                            LocatorType  = WebLocatorType.XPath,
                            LocatorValue = ".//"
                        }
                    };

                    var optionElement = CreateWebElementInfo(
                        combinedCreatedElement,
                        optionTemplate);
                    if (optionElement == null)
                    {
                        return;
                    }

                    optionElement.Parent = combinedCreatedElement;
                    combinedCreatedElement.Elements.Add(optionElement);
                }

                if (_elementType == WebElementTypes.Frame)
                {
                    var frameElement      = createdElement as WebElementWithReferenceViewModel;
                    var referenceTreePath = frameElement.ReferenceBreadString;
                    if (referenceTreePath == null)
                    {
                        var blockedTypesToPick = WebElementsViewModelsHelper.GetBlockedElementTypesForElementType(_elementType);

                        var picker = new WebElementPickerDialog(_webElementsTreeUserControl.WebElements.ToList(),
                                                                true,
                                                                null,
                                                                null,
                                                                blockedTypesToPick);


                        if (picker.ShowDialog() != true)
                        {
                            return;
                        }
                        referenceTreePath = picker.SelectedWebElementTreePath;
                    }

                    var referencedElement = (WebElementInfoViewModel)_webElementsTreeUserControl.WebElements.FindNodeByTreePath(referenceTreePath);

                    var copy = WebElementsViewModelsHelper.CreateFullModelCopy(referencedElement);
                    copy.Parent = frameElement;
                    if (frameElement.Elements == null)
                    {
                        frameElement.Elements = new ObservableCollection <WebElementInfoViewModel>();
                    }
                    frameElement.Elements.Clear();
                    frameElement.Elements.Add(copy);
                }
            }
            if (createdElement == null)
            {
                return;
            }

            if (Selected == null)
            {
                if (_webElementsTreeUserControl.WebElements == null)
                {
                    _webElementsTreeUserControl.WebElements = new ObservableCollection <CombinedWebElementInfoViewModel>();
                }
                _webElementsTreeUserControl.WebElements.Add(createdElement as CombinedWebElementInfoViewModel);
            }
            else
            {
                var comb = Selected as CombinedWebElementInfoViewModel;
                if (comb == null)
                {
                    MessageBox.Show("Magic error. WebElement was created not from CombinedWebElement");
                    return;
                }

                if (comb.Elements == null)
                {
                    comb.Elements = new ObservableCollection <WebElementInfoViewModel>();
                }

                comb.Elements.Add(createdElement);
                createdElement.Parent = comb;
            }

            return;
        }