using DateTimeFiltering;
using System;
using System.Collections;
using System.Linq;
using System.Linq.Expressions;
using Telerik.Windows.Controls;
using Telerik.Windows.Data;

namespace DateTimeFiltering
{
    public class SearchViewModel : ViewModelBase
    {
        private string searchText;
        private readonly QueryableCollectionView items;
        private readonly PredicateFilterDescriptor<Club> filter;

        public SearchViewModel()
        {
            this.filter = new PredicateFilterDescriptor<Club>();
            this.UpdateSearchPredicate();

            this.items = new QueryableCollectionView(Club.GetClubs());
            this.items.FilterDescriptors.Add(this.filter);
        }

        public string SearchText
        {
            get
            {
                return this.searchText;
            }
            set
            {
                if (this.searchText != value)
                {
                    this.searchText = value;
                    this.OnPropertyChanged("SearchText");
                    this.UpdateSearchPredicate();
                }
            }
        }

        public IEnumerable Items
        {
            get
            {
                return this.items;
            }
        }

private void UpdateSearchPredicate()
{
    string text = this.SearchText;

    Expression<Func<Club, bool>> predicate =
        c => string.IsNullOrEmpty(text)
                 ? true
                 : c.Name.ToString().ToLower().Contains(text) ||
                   c.Established.ToLongDateString().ToLower().Contains(text) ||
                   c.StadiumCapacity.ToString().ToLower().Contains(text);

    this.filter.Predicate = predicate;
}
    }
}
