public void Initialize()
        {
            TextOFNewType = "Новый БПВ ход";
            MessageBus.Default.Subscribe("SetAdditionalStructDefault", SetAdditionalStructDefault);


            MessageBus.Default.Subscribe("RebuildFirstBPV", RebuildFirst);
            MessageBus.Default.Subscribe("RebuildLegSectionViewModel", Rebuild);
            BpvWayType = new ObservableCollection <BPVHipWay>();

            SavePanelCommand = new DelegateCommand(() =>
            {
                var panel = CurrentPanelViewModel;
                if (!string.IsNullOrWhiteSpace(panel.Text1) || !string.IsNullOrWhiteSpace(panel.Text2))
                {
                    CurrentLegSide = CurrentLegSide;

                    if (IsStructEdited(CurrentPanelViewModel.LegPrt))
                    {
                        if ((LevelSelected != 0 && testOnUnique(CurrentPanelViewModel.LegPrt)) || (LevelSelected == 0 && testAdditionalStructOnUnique(CurrentPanelViewModel.LegPrt)))
                        {
                            var newStruct    = GetPanelStructure();
                            newStruct.Custom = false;
                            Data.BPVHips.Add((BPVHipStructure)newStruct);
                            Data.Complete();
                            _lastSender.StructureSource.Add(newStruct);
                            _lastSender.SelectedValue         = newStruct;
                            CurrentPanelViewModel.PanelOpened = false;
                            handled = false;
                        }
                    }
                    if (!IsStructEdited(CurrentPanelViewModel.LegPrt))
                    {
                        CurrentPanelViewModel.PanelOpened = false;
                        handled = false;
                    }
                }
                else
                {
                    MessageBox.Show("Не все поля заполнены");
                }

                //_lastSender.DeleteCustom();
            });

            CurrentPanelViewModelWaySelect = new BPVVayTypePanelViewModel(this);
            OpenAddtWayCommand             = new DelegateCommand(() =>
            {
                ((BPVVayTypePanelViewModel)CurrentPanelViewModelWaySelect).ClearPanel();
                ((BPVVayTypePanelViewModel)CurrentPanelViewModelWaySelect).PanelOpened = true;
            });

            SavetWayCommand = new DelegateCommand(() =>
            {
                var newType = ((BPVVayTypePanelViewModel)CurrentPanelViewModelWaySelect).GetPanelType();
                if (!string.IsNullOrWhiteSpace(newType.Name))
                {
                    ((BPVVayTypePanelViewModel)CurrentPanelViewModelWaySelect).PanelOpened = false;

                    //Handled = false;

                    Data.BPVHipWay.Add((newType));

                    Data.Complete();
                    PanelOpened           = true;
                    var DataSourceListbuf = BpvWayType;
                    BpvWayType            = new ObservableCollection <BPVHipWay>();

                    foreach (var Scintific in Data.BPVHipWay.GetAll)
                    {
                        BpvWayType.Add(Scintific);
                    }
                    BPVHipWay emptyWay1 = new BPVHipWay();
                    emptyWay1.Name      = "";
                    BpvWayType.Add(emptyWay1);
                    SelectedBpvWayTypeId = BpvWayType.Count - 1;
                    // Controller.NavigateTo<BPVHipViewModel>();

                    PanelOpened = false;
                }
                else
                {
                    MessageBox.Show("Не все поля заполнены");
                }
            });
            RevertWayCommand = new DelegateCommand(() =>
            {
                ((BPVVayTypePanelViewModel)CurrentPanelViewModelWaySelect).PanelOpened = false;
                PanelOpened = false;
            });

            SaveCommand = new DelegateCommand(
                () =>
            {
                if (LegSections[0].SelectedValue != null)
                {
                    bool isValid = Validate();
                    if (isValid)
                    {
                        IsEmpty = false;

                        List <int?> ids = new List <int?>();

                        foreach (var leg in LegSections)
                        {
                            //никогда так не делайте
                            if (leg.IsVisible == Visibility.Visible && leg.ListNumber != 1 && leg.SelectedValue != null && leg.SelectedValue.Id != 0)
                            {
                                ids.Add(leg.SelectedValue.Id);
                            }
                        }

                        var combo = Data.BPVCombos.FindCombo(LegSections[0].SelectedValue.Id, ids);
                        //если комбо не нашлось - значит оно кастомное, мы его запомним и отправим в базу на радость будущим пользователям
                        if (combo == null)
                        {
                            var newCombo = new BPVHipCombo();

                            commentSave = Comment; for (int i = 0; i < LegSections.Count; i++)
                            {
                                var currentStructure = LegSections[i].SelectedValue;
                                //ничего не было выбрано
                                if (currentStructure == null)
                                {
                                    continue;
                                }
                                //добавляем структуры, которые встретились впервые, чтобы потом добавить комбо
                                if (currentStructure.Id == 0
                                    //потому что переход к след.разделу в комбо добавлять не надо, это излишняя информация
                                    && !currentStructure.ToNextPart)
                                {
                                    //currentStructure.Level = i + 1;
                                    //Data.BPVHips.Add((BPVHipStructure)currentStructure);
                                    //Data.Complete();
                                    //((BPVHipEntry)LegSections[i].CurrentEntry).Structure = (BPVHipStructure)currentStructure;
                                    //(LegSections[i].CurrentEntry).StructureID = currentStructure.Id;
                                    //Data.BPVHipEntries.Add((BPVHipEntry)LegSections[i].CurrentEntry);
                                    //Data.Complete();
                                    //if (i == 0) newCombo.IdStr1 = currentStructure.Id;
                                    ////там гда раньше был ноль теперь будет актуальный айдишник
                                    //else ids[i - 2] = currentStructure.Id;
                                }
                            }

                            newCombo.IdStr1 = LegSections[0].SelectedValue.Id;
                            //заполняем комбо
                            Data.BPVCombos.AddCombo(newCombo, ids);
                            Data.Complete();
                            MessageBus.Default.Call("RebuildFirstBPV", this, LegSections[0]);
                            MessageBus.Default.Call("RebuildLegSectionViewModel", this, LegSections[0]);
                            MessageBus.Default.Call("RebuildLegSectionViewModel", this, LegSections[1]);
                            MessageBus.Default.Call("RebuildLegSectionViewModel", this, LegSections[2]);
                            MessageBus.Default.Call("RebuildLegSectionViewModel", this, LegSections[3]);
                            MessageBus.Default.Call("RebuildLegSectionViewModel", this, LegSections[4]);
                        }

                        MessageBus.Default.Call("LegDataSaved", this, this.GetType());
                        Controller.NavigateTo <ViewModelAddPhysical>();
                    }
                }
                else
                {
                    MessageBus.Default.Call("LegDataSaved", this, this.GetType());
                    Controller.NavigateTo <ViewModelAddPhysical>();
                }
            }
                );

            AdditionalStructure = new BPVHipAdditionalSectionViewModel(Controller, null, 0);
            foreach (var way in Data.BPVHipWay.GetAll)
            {
                BpvWayType.Add(way);
            }
            BPVHipWay emptyWay = new BPVHipWay();

            emptyWay.Name = "";
            BpvWayType.Add(emptyWay);
            SelectedBpvWayTypeId = 0;
            LevelCount           = 5;
            _sections            = new ObservableCollection <LegSectionViewModel>();
            for (int i = 0; i < LevelCount; i++)
            {
                if (i != 0)
                {
                    LegSections.Add(new BPVHipSectionViewModel(Controller, _sections[i - 1], i + 1));
                }
                else
                {
                    LegSections.Add(new BPVHipSectionViewModel(Controller, null, i + 1));
                }
            }
            _title = "Большая подкожная вена на бедре";
        }
        private void RebuildFirst(object sender, object data)
        {
            if (((LegPartViewModel)Controller.CurrentViewModel.Controller.LegViewModel).CurrentLegSide != this.CurrentLegSide)
            {
                return;
            }
            var   bufSaveLegSection           = new List <int?>();
            float savedValue                  = -1;
            int   bufSaveLegAdditionalSection = -1;

            if (AdditionalStructure.SelectedValue != null)
            {
                bufSaveLegAdditionalSection = AdditionalStructure.SelectedValue.Id;
                if (AdditionalStructure.CurrentEntry != null)
                {
                    savedValue = AdditionalStructure.CurrentEntry.Size;
                }
            }
            AdditionalStructure = new BPVHipAdditionalSectionViewModel(Controller, null, 0);
            if (bufSaveLegAdditionalSection != -1)
            {
                for (int j = 0; j < AdditionalStructure.StructureSource.Count; ++j)
                {
                    //    if(bufSaveLegAdditionalSection == 0 && AdditionalStructure.StructureSource[j].Text1 == " " && AdditionalStructure.StructureSource[j].Text2 == " ")
                    //    {
                    //        bufSaveLegAdditionalSection = j;
                    //        break;
                    //    }
                    if (AdditionalStructure.StructureSource[j].Id == bufSaveLegAdditionalSection && AdditionalStructure.StructureSource[j].Text1 != "Свой вариант ответа")
                    {
                        bufSaveLegAdditionalSection = j;
                        break;
                    }
                }

                //   if (bufSaveLegAdditionalSection != -1)
                AdditionalStructure.SelectedValue = AdditionalStructure.StructureSource[bufSaveLegAdditionalSection];
                if (savedValue != -1)
                {
                    AdditionalStructure.CurrentEntry.Size = savedValue;
                }
            }
            foreach (var x in LegSections)
            {
                if (x.SelectedValue != null)
                {
                    bufSaveLegSection.Add(x.SelectedValue.Id);
                }
                else
                {
                    bufSaveLegSection.Add(null);
                }
            }



            for (int i = 0; i < LegSections.Count; ++i)
            {
                var bufSave = new ObservableCollection <LegPartDbStructure>();
                bufSave = LegSections[i].StructureSource;

                LegSections[i].StructureSource = new ObservableCollection <LegPartDbStructure>(Data.BPVHips.LevelStructures(i + 1).ToList());



                int selectedIndex = -1;
                if (bufSaveLegSection[i] != null)
                {
                    for (int j = 0; j < LegSections[i].StructureSource.Count; ++j)
                    {
                        if (LegSections[i].StructureSource[j].Id == bufSaveLegSection[i])
                        {
                            selectedIndex = j;
                        }
                    }
                }



                if (selectedIndex != -1)
                {
                    LegSections[i].SelectedValue = LegSections[i].StructureSource[selectedIndex];
                }


                foreach (var variant in bufSave)
                {
                    if (variant.Text1 == "Свой вариант ответа" || variant.Text1 == "Переход к следующему разделу")
                    {
                        if (variant.Text1 == "Переход к следующему разделу" && i == 0)
                        {
                        }
                        else
                        {
                            LegSections[i].StructureSource.Add(variant);
                        }
                    }
                    else if (variant.Text1 == "" && variant.Text2 == "")
                    {
                        LegSections[i].StructureSource.Add(variant);
                    }
                }
                foreach (var structure in LegSections[i].StructureSource)
                {
                    structure.Metrics = Data.Metrics.GetStr(structure.Size);
                }
            }
            MessageBus.Default.Call("LegDataSaved", this, this.GetType());
            FF_lengthSave       = FF_length;
            SelectedWayTypeSave = SelectedWayType;



            LegSectionsSaved = new List <LegSectionViewModel>();
            for (int i = 0; i < LevelCount; i++)
            {
                if (i != 0)
                {
                    LegSectionsSaved.Add(new BPVHipSectionViewModel(Controller, _sections[i - 1], i + 1));
                }
                else
                {
                    LegSectionsSaved.Add(new BPVHipSectionViewModel(Controller, null, i + 1));
                }
            }

            commentSave = Comment; for (int i = 0; i < LegSections.Count; i++)
            {
                LegSectionsSaved[i].Comment       = LegSections[i].Comment;
                LegSectionsSaved[i].Size          = LegSections[i].Size;
                LegSectionsSaved[i].Size2         = LegSections[i].Size2;
                LegSectionsSaved[i].Text1         = LegSections[i].Text1;
                LegSectionsSaved[i].Text2         = LegSections[i].Text2;
                LegSectionsSaved[i].SelectedValue = LegSections[i].SelectedValue;
                LegSectionsSaved[i].CurrentEntry  = LegSections[i].CurrentEntry;
            }

            BpvWayType = new ObservableCollection <BPVHipWay>();

            foreach (var Scintific in Data.BPVHipWay.GetAll)
            {
                BpvWayType.Add(Scintific);
                //if (Scintific.Id == SelectedWayTypeSave.Id)
                //{
                //    SelectedBpvWayTypeId = BpvWayType.IndexOf(SelectedWayTypeSave as BPVHipWay);
                //}
            }
            BPVHipWay emptyWay = new BPVHipWay();

            emptyWay.Name = "";
            BpvWayType.Add(emptyWay);
            SelectedWayType = SelectedWayTypeSave;
        }