using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Controls;
using Telerik.Windows.Data;

namespace RadGridView_WPF_AR_7
{
    /// <summary>
    /// FromDateToDateFilterControl
    /// </summary>
    public partial class FromDateToDateFilterControl : UserControl, IFilteringControl
    {
        private GridViewBoundColumnBase column;
        private CompositeFilterDescriptor compositeFilter;
        private Telerik.Windows.Data.FilterDescriptor fromFilter;
        private Telerik.Windows.Data.FilterDescriptor toFilter;

        /// <summary>
        /// Gets or sets a value indicating whether the filtering is active.
        /// </summary>
        public bool IsActive
        {
            get { return (bool)GetValue(IsActiveProperty); }
            set { SetValue(IsActiveProperty, value); }
        }

        /// <summary>
        /// Identifies the <see cref="IsActive"/> dependency property.
        /// </summary>
        public static readonly DependencyProperty IsActiveProperty =
            DependencyProperty.Register(
                "IsActive",
                typeof(bool),
                typeof(FromDateToDateFilterControl),
                new System.Windows.PropertyMetadata(false));

        public DateTime FromDate
        {
            get { return this.fromDatePicker.SelectedDate.GetValueOrDefault(DateTime.MinValue); }
            set { this.fromDatePicker.SelectedDate = value; }
        }

        public DateTime ToDate
        {
            get { return this.toDatePicker.SelectedDate.GetValueOrDefault(DateTime.MaxValue); }
            set { this.toDatePicker.SelectedDate = value; }
        }

        public RadDatePicker FromPicker
        {
            get { return this.fromDatePicker; }
        }

        public RadDatePicker ToPicker
        {
            get { return this.toDatePicker; }
        }

        public FromDateToDateFilterControl()
        {
            InitializeComponent();
        }

        public void Prepare(GridViewBoundColumnBase column)
        {
            this.column = column;

            if (this.compositeFilter == null)
            {
                this.CreateFilters();
            }

            this.fromFilter.Value = this.FromDate;
            this.toFilter.Value = this.ToDate;
        }

        private void CreateFilters()
        {
            string dataMember = this.column.DataMemberBinding.Path.Path;

            this.compositeFilter = new CompositeFilterDescriptor();

            this.fromFilter = new Telerik.Windows.Data.FilterDescriptor(dataMember
                , Telerik.Windows.Data.FilterOperator.IsGreaterThanOrEqualTo
                , null);
            this.compositeFilter.FilterDescriptors.Add(this.fromFilter);

            this.toFilter = new Telerik.Windows.Data.FilterDescriptor(dataMember
                , Telerik.Windows.Data.FilterOperator.IsLessThanOrEqualTo
                , null);
            this.compositeFilter.FilterDescriptors.Add(this.toFilter);
        }

        private void OnFilter(object sender, RoutedEventArgs e)
        {
            this.fromFilter.Value = this.FromDate;
            this.toFilter.Value = this.ToDate;

            if (!this.column.DataControl.FilterDescriptors.Contains(this.compositeFilter))
            {
                this.column.DataControl.FilterDescriptors.Add(this.compositeFilter);
            }

            this.IsActive = true;
        }

        private void OnClear(object sender, RoutedEventArgs e)
        {
            if (this.column.DataControl.FilterDescriptors.Contains(this.compositeFilter))
            {
                this.column.DataControl.FilterDescriptors.Remove(this.compositeFilter);
            }

            this.FromDate = new DateTime(1863, 1, 1);
            this.ToDate = new DateTime(1933, 1, 1);

            this.IsActive = false;
        }
    }
}
