using System;
using System.Reflection;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Collections.ObjectModel;
using C1.Xaml.FlexGrid;
using C1.Xaml;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace FlexGridSamples
{
    public sealed partial class CollectionView : UserControl
    {
        C1CollectionView _c1CollectionView;

        public CollectionView()
        {
            this.InitializeComponent();
           
            IList<string> fieldNames = new string[] { "Line", "Color", "Price", "Weight", "Cost", "Volume", "Discontinued", "Rating" };
            List<string> groupFields = new List<string>(fieldNames);
            groupFields.Sort();
            List<string> filterFields = new List<string>(groupFields);
            groupFields.Insert(0, "(None)");
            groupComboBox.ItemsSource = groupFields;
            groupComboBox.SelectedIndex = 1;
            filterComboBox.ItemsSource = filterFields;
            filterComboBox.SelectedIndex = 0;

            _c1CollectionView = new C1CollectionView();
            _c1CollectionView.SourceCollection = Product.GetProducts(100);
            _c1CollectionView.GroupDescriptions.Add(new C1.Xaml.PropertyGroupDescription("Color"));

            c1FlexGrid1.ItemsSource = _c1CollectionView;
        }

        void UpdateGrouping()
        {
            if (_c1CollectionView != null)
            {
                using (_c1CollectionView.DeferRefresh())
                {
                    _c1CollectionView.GroupDescriptions.Clear();
                    if (groupComboBox.SelectedIndex > 0)
                    {
                        _c1CollectionView.GroupDescriptions.Add(new PropertyGroupDescription((string)groupComboBox.SelectedItem));
                    }
                }
            }
        }

        void UpdateFiltering()
        {
            if (filterTextBox.Text.Length == 0)
            {
                _c1CollectionView.Filter = null;
            }
            else
            {
                if (_c1CollectionView.Filter == null)
                {
                    _c1CollectionView.Filter = FilterFunction;
                }
                else
                {
                    _c1CollectionView.Refresh();
                }
            }
        }

        bool FilterFunction(object product)
        {
            // get customer to test
            Product c = product as Product;
            if (c == null)
            {
                return false;
            }

            // get value of the property we're filtering on
            var pi = typeof(Product).GetRuntimeProperty(filterComboBox.SelectedItem.ToString());
            object propValue = pi.GetValue(c, null);
            if (propValue == null)
            {
                return false;
            }

            // check if the property contains the filter string
            var text = propValue.ToString();
            return text.IndexOf(filterTextBox.Text, StringComparison.CurrentCultureIgnoreCase) > -1;
        }

        void groupComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            UpdateGrouping();
        }
        void filterTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            UpdateFiltering();
        }
        void filterComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            filterTextBox.Text = string.Empty;
        }
    }
}
