using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Telerik.Windows.Data;

namespace ProgrammaticFilter
{
    public class StringContainsFilterDescription : FilterDescription
    {
        public StringContainsFilterDescription()
        {
        }

        private string filterValue;

        public string FilterValue
        {
            get { return filterValue; }
            set 
            {
                if (this.filterValue != value)
                {
                    filterValue = value;
                    OnPropertyChanged("FilterValue");
                }
            }
        }

        private string propertyName;

        public string PropertyName
        {
            get 
            { 
                return propertyName; 
            }
            set 
            {
                if (this.propertyName != value)
                {
                    propertyName = value;
                    OnPropertyChanged("PropertyName");
                }
            }
        }

        public override bool SatisfiesFilter(object dataItem)
        {
            //the line below needs better error checking and works for string properties only
            var value = (string) TypeDescriptor.GetProperties(dataItem)[this.PropertyName].GetValue(dataItem);

            if (!string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(this.FilterValue))
            {
                return value.ToUpper().Contains(this.FilterValue.ToUpper());
            }
            else
            {
                return string.IsNullOrEmpty(this.FilterValue);
            }
        }
    }
}