private void LoadTextSearchModes()
 {
     foreach (KeyValuePair <string, Data.TextSearchOperator> pair in
              Data.TextSearchOperator.TextSearchModes.OrderBy <KeyValuePair <string, Data.TextSearchOperator>, string>(p => Config.GetLocalizedName(p.Value)))
     {
         Data.TextSearchOperator r   = pair.Value;
         ComboBoxItem            cbi = new ComboBoxItem();
         cbi.Content = Config.GetLocalizedName(r);
         this.cbTextSearchMode.Items.Add(cbi);
     }
     this.cbTextSearchMode.SelectedIndex = 0;
 }
        private void searchRecipes(SearchMode searchMode)
        {
            Mouse.OverrideCursor = Cursors.Wait;
            try
            {
                string txtOnFilter = this.tbFilter.Text;
                txtOnFilter = StringHelper.RemoveDiacritics(txtOnFilter);

                Data.Discipline         selectedDiscipline = this.GetSelectedvalue(this.cbDisciplines, Data.Discipline.Disciplines);
                Data.RecipeType         selectedRecipeType = this.GetSelectedvalue(this.cbRecipeTypes, Data.RecipeType.RecipeTypes);
                Data.Rarity             selectedRarity     = this.GetSelectedvalue(this.cbRarities, Data.Rarity.Rarities);
                Data.TextSearchOperator searchOperator     = this.GetSelectedvalue(this.cbTextSearchMode, Data.TextSearchOperator.TextSearchModes);
                bool searchAlsoIntoIngredients             = (bool)this.cbSearchAlsoIngredients.IsChecked;
                bool showCheckedRecipes = (bool)this.cbShowCheckedRecipes.IsChecked;

                long currentSessionId = Data.Cache.SessionID;
                int  takeNRows        = 300;

                int?minRate = this.GetMinRate();
                int?maxRate = this.GetMaxRate();

                if (searchMode == SearchMode.Normal &&
                    !CheckSearchParameters(selectedDiscipline, selectedRarity, selectedRecipeType, minRate, maxRate, txtOnFilter))
                {
                    this.tbResults.Foreground = new System.Windows.Media.SolidColorBrush(Colors.Red);
                    this.tbResults.Text       = Properties.Resources.TextBoxSearchResultAlterIfNoParameter;
                    return;
                }
                else
                {
                    this.tbResults.Foreground = new System.Windows.Media.SolidColorBrush(Colors.White);
                }

                // hide welcome msg
                this.gWelcomeMsg.Visibility = System.Windows.Visibility.Collapsed;

                this.recipeItems.Clear();
                this.RefreshView();
                this.RefreshDownloadResultsCount();

                // show search indicator
                this.tbResults.Text = Properties.Resources.TextBoxSearchResultInProgress;

                BackgroundWorker worker = new BackgroundWorker();
                worker.WorkerReportsProgress = true;

                worker.DoWork +=
                    (object sender, DoWorkEventArgs e) =>
                {
                    try
                    {
                        Func <string, string> convert = delegate(string s)
                        { return(s.ToUpper()); };
                        Func <Data.Recipe, Data.Discipline, bool> hasDiscipline = delegate(Data.Recipe recipe, Data.Discipline discipline)
                        {
                            if (recipe.Disciplines.Count == 0)
                            {
                                return(false);
                            }
                            return(recipe.Disciplines.Contains(discipline));
                        };

                        List <Data.Recipe> _temp = Data.Cache.GetCache().Recipes.ToList();


                        // if not params used to search, all recipes'll be shown with the checked one
                        if (!showCheckedRecipes)
                        {
                            _temp = _temp.Where((r) => {
                                return(!r.Is_checked);
                            }).ToList();
                        }

                        if (searchMode == SearchMode.LatestDownload)
                        {
                            _temp = _temp.Where((r) => {
                                return(r.SessionID == currentSessionId);
                            }).ToList();
                        }
                        else if (searchMode == SearchMode.RecipesInError)
                        {
                            _temp = Data.Cache.GetCache().GetRecipesInError();
                        }
                        else
                        {
                            // filter on recipe type
                            if (selectedRecipeType != null)
                            {
                                _temp = _temp.Where((r) => {
                                    return(r.RecipeType != null && r.RecipeType.Key == selectedRecipeType.Key);
                                }).ToList();
                            }

                            // filter on rarity, search deeply through rarities of ingredients too
                            if (selectedRarity != null)
                            {
                                // if mode is "text contains"
                                if (searchAlsoIntoIngredients)
                                {
                                    _temp = _temp.Where((r) => {
                                        return((r.Rarity != null && r.Rarity.Key == selectedRarity.Key) ||
                                               (r.Ingredients != null && r.Ingredients.Where((p) => {
                                            return p.Rarity != null && p.Rarity.Key == selectedRarity.Key;
                                        }).Count() > 0));
                                    }).ToList();
                                }
                                else
                                {
                                    _temp = _temp.Where((r) => {
                                        return(r.Rarity != null && r.Rarity.Key == selectedRarity.Key);
                                    }).ToList();
                                }
                            }

                            // filter on discipline
                            if (selectedDiscipline != null)
                            {
                                _temp = _temp.Where((r) => {
                                    return(r.Disciplines.Contains(selectedDiscipline));
                                }).ToList();
                            }

                            // filter on minRate
                            if (minRate != null)
                            {
                                _temp = _temp.Where((r) => {
                                    return(r.MinRate != null && int.Parse(r.MinRate) >= minRate);
                                }).ToList();
                            }

                            // filter on maxRate
                            if (maxRate != null)
                            {
                                _temp = _temp.Where((r) => {
                                    return(r.MinRate != null && int.Parse(r.MinRate) <= maxRate);
                                }).ToList();
                            }

                            // filter on keyword
                            if (!String.IsNullOrEmpty(txtOnFilter))
                            {
                                if (searchOperator.Mode == Data.TextSearchOperator.Modes.Contains)
                                {
                                    // if mode is "text contains"
                                    if (searchAlsoIntoIngredients)
                                    {
                                        _temp = _temp.Where((r) => {
                                            return(r.ContainsText(txtOnFilter) ||
                                                   r.IngredientContainsText(txtOnFilter));
                                        }).ToList();
                                    }
                                    else
                                    {
                                        _temp = _temp.Where((r) => {
                                            return(r.ContainsText(txtOnFilter));
                                        }).ToList();
                                    }
                                }
                                else
                                {
                                    // if mode is "text equals"
                                    if (searchAlsoIntoIngredients)
                                    {
                                        _temp = _temp.Where((r) => {
                                            return(r.EqualsText(txtOnFilter) ||
                                                   r.IngredientEqualsText(txtOnFilter));
                                        }).ToList();
                                    }
                                    else
                                    {
                                        _temp = _temp.Where((r) => {
                                            return(r.EqualsText(txtOnFilter));
                                        }).ToList();
                                    }
                                }
                            }
                        }

                        if (_temp.Count() > takeNRows)
                        {
                            _temp = _temp.Take(takeNRows).ToList();
                        }

                        for (int i = 0; i <= _temp.Count() - 1; i++)
                        {
                            worker.ReportProgress(i, new SearchResultSate()
                            {
                                Current = i + 1, Total = _temp.Count(), Recipe = _temp[i]
                            });
                        }
                    }
#if DEBUG
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
#else
                    catch
                    {
                        // Ignore exceptions at this point
                    }
#endif
                };
                worker.ProgressChanged +=
                    (object sender, ProgressChangedEventArgs e) =>
                {
                    SearchResultSate result = (SearchResultSate)e.UserState;
                    AddRecipeToView(result.Recipe);
                };
                worker.RunWorkerCompleted +=
                    (object sender, RunWorkerCompletedEventArgs e) =>
                {
                    this.RefreshView();
                    this.RefreshSearchResultsCount();
                    this.svScroller.ScrollToTop();
                };
                worker.RunWorkerAsync();
            }
            catch (Exception ex)
            {
                this.tbResults.Foreground = new SolidColorBrush(Colors.Red);
                this.tbResults.Text       = ex.Message;
            }
            finally
            {
                Mouse.OverrideCursor = null;
            }
        }