private T GetSelectedvalue <T>(ComboBox cb, Dictionary <string, T> datas) where T : Data.ILocalizable
        {
            string value;
            object selectedValue = cb.SelectedValue;

            if (selectedValue.GetType() == typeof(ComboBoxItem))
            {
                value = (string)((ComboBoxItem)selectedValue).Content;
                foreach (KeyValuePair <string, T> pair in datas)
                {
                    T r = pair.Value;
                    if (value == Config.GetLocalizedName(r))
                    {
                        return(r);
                    }
                }
            }
            else if (selectedValue.GetType() == typeof(DisciplineItem))
            {
                Data.Discipline selection = ((DisciplineItem)selectedValue).Discipline;
                foreach (KeyValuePair <string, T> pair in datas)
                {
                    T r = pair.Value;
                    if (Config.GetLocalizedName(selection) == Config.GetLocalizedName(r))
                    {
                        return(r);
                    }
                }
            }
            return(default(T));
        }
 private void cbDisciplines_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     Data.RecipeType _selectedType = null;
     if (this.cbRecipeTypes.Items.Count > 0)
     {
         _selectedType = this.GetSelectedvalue(this.cbRecipeTypes, Data.RecipeType.RecipeTypes);
     }
     Data.Discipline discipline = this.GetSelectedvalue(this.cbDisciplines, Data.Discipline.Disciplines);
     LoadRecipeTypes(discipline);
     if (_selectedType != null)
     {
         if (discipline == null || discipline.RecipeTypes.Values.Contains(_selectedType))
         {
             foreach (object cbi in this.cbRecipeTypes.Items)
             {
                 if (cbi.GetType() == typeof(ComboBoxItem))
                 {
                     if (((ComboBoxItem)cbi).Content.ToString() == Config.GetLocalizedName(_selectedType))
                     {
                         this.cbRecipeTypes.SelectedItem = cbi;
                         break;
                     }
                 }
             }
         }
     }
 }
        private void LoadRecipeTypes(Data.Discipline discipline = null)
        {
            Action <KeyValuePair <string, Data.RecipeType> > addRecipeType = delegate(KeyValuePair <string, Data.RecipeType> pair)
            {
                System.Windows.Media.SolidColorBrush fontBrush = new System.Windows.Media.SolidColorBrush(Colors.Black);
                ComboBoxItem cbi = new ComboBoxItem();
                cbi.Foreground = fontBrush;
                Data.RecipeType ot = pair.Value;
                cbi.Content = Config.GetLocalizedName(ot);
                this.cbRecipeTypes.Items.Add(cbi);
            };

            cbRecipeTypes.Items.Clear();
            cbRecipeTypes.Items.Add(Properties.Resources.ComboBoxRecipeTypeFirstEntry);
            if (discipline != null)
            {
                foreach (KeyValuePair <string, Data.RecipeType> pair in
                         discipline.RecipeTypes.OrderBy <KeyValuePair <string, Data.RecipeType>, string>(p => Config.GetLocalizedName(p.Value)))
                {
                    addRecipeType(pair);
                }
            }
            else
            {
                foreach (KeyValuePair <string, Data.RecipeType> pair in
                         Data.RecipeType.RecipeTypes.OrderBy <KeyValuePair <string, Data.RecipeType>, string>(p => Config.GetLocalizedName(p.Value)))
                {
                    addRecipeType(pair);
                }
            }
            this.cbRecipeTypes.SelectedIndex = 0;
        }
 private bool CheckSearchParameters(Data.Discipline selectedDiscipline, Data.Rarity selectedRarity, Data.RecipeType selectedRecipeType, int?minRate, int?MaxRate, string _txtOnFilter)
 {
     if (selectedDiscipline == null &&
         selectedRarity == null &&
         selectedRecipeType == null &&
         minRate == null &&
         MaxRate == null &&
         (String.IsNullOrEmpty(_txtOnFilter) || _txtOnFilter.Length < 2))
     {
         return(false);
     }
     return(true);
 }
        public DisciplineItem(Data.Discipline discipline)
        {
            InitializeComponent();
            this._discipline = discipline;

            this.tbDisciplineName.Text = Config.GetLocalizedName(discipline);

            if (!String.IsNullOrEmpty(discipline.Path))
            {
                BitmapImage bi = new BitmapImage(new Uri(discipline.Path));
                this.iDisciplineIcon.Source = bi;
            }
        }
 private void LoadDisciplines()
 {
     cbDisciplines.Items.Add(Properties.Resources.ComboBoxDisciplineFirstEntry);
     foreach (KeyValuePair <string, Data.Discipline> pair in
              Data.Discipline.Disciplines.OrderBy <KeyValuePair <string, Data.Discipline>, string>(p => Config.GetLocalizedName(p.Value)))
     {
         Data.Discipline r = pair.Value;
         //ComboBoxItem cbi = new ComboBoxItem();
         //cbi.Content = Config.GetLocalizedName(r);
         DisciplineItem cbi = new DisciplineItem(r);
         this.cbDisciplines.Items.Add(cbi);
     }
     this.cbDisciplines.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;
            }
        }