public static ViewAnalogousVM GenerateNewAnalogousReactions(
            ReactionVM SelectedReaction,
            bool IsReactantsSelected,
            bool IsSolventsSelected,
            bool IsAgentsSelected,
            bool IsCatalystSelected,
            bool IspHSelected,
            bool IsTempSelected,
            bool IsTimeSelected,
            bool IsPressureSelected,
            bool IsRSNSelected,
            int ReactionsCountToCopy, int IndexToAdd
            )
        {
            ViewAnalogousVM vm            = new ViewAnalogousVM();
            var             mainViewModel = ((MainWindow)(App.Current.MainWindow)).DataContext as MainVM;

            mainViewModel.ProgressText = "Creating Analogous Reacions . .";
            try
            {
                vm.SelectedMasterReaction = SelectedReaction;
                List <ReactionParticipantVM> allParticipants = new List <ReactionParticipantVM>();
                List <RsnVM>      tanRsns      = new List <RsnVM>();
                List <ReactionVM> tanReactions = new List <ReactionVM>();
                var tanParticipants            = new List <ReactionParticipantVM>();
                if (IsReactantsSelected)
                {
                    var reactants = mainViewModel.TanVM.ReactionParticipants.OfType(ParticipantType.Reactant);
                    allParticipants.AddRange(reactants);
                }
                if (IsSolventsSelected)
                {
                    var solvents = mainViewModel.TanVM.ReactionParticipants.OfType(ParticipantType.Solvent);
                    allParticipants.AddRange(solvents);
                }

                if (IsAgentsSelected)
                {
                    var agents = mainViewModel.TanVM.ReactionParticipants.OfType(ParticipantType.Agent);
                    allParticipants.AddRange(agents);
                }

                if (IsCatalystSelected)
                {
                    var catalysts = mainViewModel.TanVM.ReactionParticipants.OfType(ParticipantType.Catalyst);
                    allParticipants.AddRange(catalysts);
                }
                for (int i = 0; i < ReactionsCountToCopy; i++)
                {
                    var reaction = new ReactionVM
                    {
                        DisplayOrder  = ++IndexToAdd,
                        Id            = Guid.NewGuid(),
                        TanVM         = SelectedReaction.TanVM,
                        AnalogousVMId = SelectedReaction.Id
                    };
                    #region Stages
                    var stages = new List <StageVM>();
                    foreach (var masterStage in SelectedReaction.Stages)
                    {
                        bool ValidStage     = true;
                        var  analogousStage = new StageVM {
                            Id = Guid.NewGuid(), ReactionVm = reaction
                        };
                        var Conditions = new List <StageConditionVM>();
                        if (masterStage.Conditions != null)
                        {
                            foreach (var selectedCondition in masterStage.Conditions)
                            {
                                var condition = new StageConditionVM
                                {
                                    DisplayOrder  = selectedCondition.DisplayOrder,
                                    Id            = Guid.NewGuid(),
                                    PH            = IspHSelected ? selectedCondition.PH : "",
                                    Pressure      = IsPressureSelected ? selectedCondition.Pressure : "",
                                    StageId       = analogousStage.Id,
                                    Temperature   = IsTempSelected ? selectedCondition.Temperature : "",
                                    Time          = IsTimeSelected ? selectedCondition.Time : "",
                                    TIME_TYPE     = IsTimeSelected ? selectedCondition.TIME_TYPE : "",
                                    PRESSURE_TYPE = IsPressureSelected ? selectedCondition.PRESSURE_TYPE : "",
                                    TEMP_TYPE     = IsTempSelected ? selectedCondition.TEMP_TYPE : "",
                                    PH_TYPE       = IspHSelected ? selectedCondition.PH_TYPE : "",
                                };
                                if ((IspHSelected && !string.IsNullOrEmpty(selectedCondition.PH)) || (IsPressureSelected && !string.IsNullOrEmpty(selectedCondition.Pressure)) ||
                                    (IsTempSelected && !string.IsNullOrEmpty(selectedCondition.Temperature)) || (IsTimeSelected && !string.IsNullOrEmpty(selectedCondition.Time)))
                                {
                                    Conditions.Add(condition);
                                }
                            }
                            analogousStage.SetConditions(Conditions);
                        }
                        var stageParticipants = (from sp in allParticipants where sp.StageVM.Id == masterStage.Id select sp).ToList();
                        foreach (var stageParticipant in stageParticipants)
                        {
                            var newParticipant = new ReactionParticipantVM
                            {
                                ChemicalType    = stageParticipant.ChemicalType,
                                DisplayOrder    = stageParticipant.DisplayOrder,
                                Name            = stageParticipant.Name,
                                Num             = stageParticipant.Num,
                                ParticipantType = stageParticipant.ParticipantType,
                                ReactionVM      = reaction,
                                Reg             = stageParticipant.Reg,
                                StageVM         = analogousStage,
                                TanChemicalId   = stageParticipant.TanChemicalId,
                                Yield           = stageParticipant.Yield,
                                Id = Guid.NewGuid()
                            };
                            tanParticipants.Add(newParticipant);
                        }
                        if (IsRSNSelected)
                        {
                            var stagersnList = (from rsn in mainViewModel.TanVM.Rsns where rsn.Reaction.Id == SelectedReaction.Id && rsn.Stage != null && rsn.Stage.Id == masterStage.Id select rsn).ToList();
                            foreach (var rsn in stagersnList)
                            {
                                var newRsn = new RsnVM
                                {
                                    CvtText  = rsn.CvtText,
                                    Reaction = reaction,
                                    IsRXN    = rsn.IsRXN,
                                    Stage    = analogousStage,
                                    FreeText = rsn.FreeText,
                                    Id       = Guid.NewGuid(),
                                    IsIgnorableInDelivery = rsn.IsIgnorableInDelivery,
                                    DisplayOrder          = rsn.DisplayOrder,
                                    ReactionParticipantId = rsn.ReactionParticipantId,
                                    SelectedChemical      = rsn.SelectedChemical
                                };
                                tanRsns.Add(newRsn);
                            }
                        }
                        if ((analogousStage.Conditions == null || !analogousStage.Conditions.Any()) &&
                            (tanParticipants == null || !tanParticipants.Where(tp => tp.StageVM != null && tp.StageVM.Id == analogousStage.Id).Any()) &&
                            (tanRsns == null || !tanRsns.Where(tp => tp.Stage != null && tp.Stage.Id == analogousStage.Id).Any()) && !mainViewModel.TanVM.ReactionParticipants.OfReactionAndStage(SelectedReaction.Id, masterStage.Id).OfType(ParticipantType.Reactant).Any())
                        {
                            ValidStage = false;
                        }
                        if (ValidStage)
                        {
                            stages.Add(analogousStage);
                        }
                    }
                    #endregion
                    reaction.SetStages(stages, 0, false, true);
                    tanReactions.Add(reaction);
                    var reactionParticipants = (from sp in allParticipants where sp.ReactionVM.Id == SelectedReaction.Id && sp.StageVM == null select sp).ToList();
                    foreach (var participant in reactionParticipants)
                    {
                        var newParticipant = new ReactionParticipantVM
                        {
                            ChemicalType    = participant.ChemicalType,
                            DisplayOrder    = participant.DisplayOrder,
                            Name            = participant.Name,
                            Num             = participant.Num,
                            ParticipantType = participant.ParticipantType,
                            ReactionVM      = reaction,
                            Reg             = participant.Reg,
                            StageVM         = null,
                            TanChemicalId   = participant.TanChemicalId,
                            Yield           = participant.Yield,
                            Id = Guid.NewGuid()
                        };
                        tanParticipants.Add(newParticipant);
                    }

                    if (IsRSNSelected)
                    {
                        var reationrsnList = (from rsn in mainViewModel.TanVM.Rsns where rsn.Reaction.Id == SelectedReaction.Id && rsn.Stage == null select rsn).ToList();
                        foreach (var rsn in reationrsnList)
                        {
                            var newRsn = new RsnVM
                            {
                                CvtText               = rsn.CvtText,
                                Reaction              = reaction,
                                FreeText              = rsn.FreeText,
                                IsRXN                 = rsn.IsRXN,
                                Stage                 = null,
                                Id                    = Guid.NewGuid(),
                                SelectedChemical      = rsn.SelectedChemical,
                                ReactionParticipantId = rsn.ReactionParticipantId,
                                DisplayOrder          = rsn.DisplayOrder,
                                IsIgnorableInDelivery = rsn.IsIgnorableInDelivery
                            };
                            tanRsns.Add(newRsn);
                        }
                    }
                }
                foreach (var participant in tanParticipants)
                {
                    vm.AllParticipants.Add(participant);
                    vm.ReactionParticipants.Add(participant);
                }
                vm.Rsns.Clear();
                foreach (var rsn in tanRsns)
                {
                    vm.Rsns.Add(rsn);
                }
                foreach (var reaction in tanReactions)
                {
                    vm.AnalogousReactions.Add(reaction);
                }
            }
            catch (Exception ex)
            {
                AppErrorBox.ShowErrorMessage("Can't Create Analogous Reactions", ex.Message);
            }
            return(vm);
        }
        public string GetTemperatureAndUnitsFromString(string _temp, out string _temp_units, StageConditionVM condition = null)
        {
            string strTemp      = _temp;
            string strTempUnits = "c";

            try
            {
                if (condition != null)
                {
                    if (_temp.Contains("]") || _temp.Contains("-"))
                    {
                        string[] tempValues = _temp.Split(new string[] { "-", "]", "+/-" }, StringSplitOptions.RemoveEmptyEntries);
                        if (condition.TEMP_TYPE == TemperatureEnum.Refluxto.ToString())
                        {
                            _temp = tempValues[1];
                        }
                        else if (condition.TEMP_TYPE == TemperatureEnum.toReflux.ToString())
                        {
                            _temp = tempValues[0];
                        }
                        else if (condition.TEMP_TYPE == TemperatureEnum.Roomtempto.ToString())
                        {
                            _temp = tempValues[1];
                        }
                        else if (condition.TEMP_TYPE == TemperatureEnum.toRoomTemp.ToString())
                        {
                            _temp = tempValues[0];
                        }
                        else if (condition.TEMP_TYPE == TemperatureEnum.Directional.ToString())
                        {
                            _temp = tempValues[1];
                        }
                        else if (condition.TEMP_TYPE == TemperatureEnum.Rang.ToString())
                        {
                            _temp = tempValues[1];
                        }
                        else if (condition.TEMP_TYPE == TemperatureEnum.pluseDevminus.ToString())
                        {
                            _temp = tempValues[1];
                        }
                    }
                }
                string[] splitter    = { "f", "k", "r" };
                string[] strTempVals = strTemp.Split(splitter, StringSplitOptions.RemoveEmptyEntries);
                if (strTempVals != null)
                {
                    if (strTempVals.Length > 0)
                    {
                        strTempUnits = Regex.Replace(_temp.ToString(), "[0-9.\\-\\](<|>)]", "");
                        if (strTempUnits == "")
                        {
                            strTempUnits = "c";
                        }
                        else
                        {
                        }
                        strTemp = strTempVals[0];
                    }
                }
            }
            catch (Exception ex)
            {
                Log.This(ex);
            }
            _temp_units = strTempUnits;
            return(strTemp);
        }