using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Data;

using C1.Silverlight.DataGrid;

namespace C1DataGrid_Demo
{
    public partial class TextBoxesFilter : UserControl
    {        
        public TextBoxesFilter()
        {
            InitializeComponent();

            // locates the filter at the same horizontal offset
            UpdateFilterOffset(grid);

            // handle colums loaded to update textboxes width and location
            grid.ColumnsLoaded += (s, a) => { UpdateFilterWidths(grid); };

            // handle reordering to update textboxes position
            grid.ColumnReordered += (s, a) => { UpdateFiltersOrder(grid); };

            grid.ItemsSource = Data.GetProducts();
        }


        private void grid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            // invoke common autogeneration handling for all the samples
            //Common.HandleColumnAutoGeneration(_ds, e);

            // if the column is going to be added
            // add the required TextBox to our container
            if (!e.Cancel)
            {
                // disable user-made Filter
                e.Column.CanUserFilter = true;

                // Add filter to the container
                var filterTextBox = AddTextBoxFilter(e.Column);

                // handle TextChanged to apply the filter
                filterTextBox.TextChanged += (s, a) =>
                {
                    var state = TextFilterHelper.BuildFilterState(e.Column, filterTextBox.Text);

                    // apply filter
                    e.Column.FilterState = state;
                };
            }
        }


        #region Filter TextBox Container Control

        // Creates a new TextBox filter for a specific column
        private TextBox AddTextBoxFilter(DataGridColumn column)
        {
            // add filter to the UI
            var filterTextBox = new C1SearchBox();
            filterTextBox.Tag = column;
            filterContainer.Children.Add(filterTextBox);

            return filterTextBox;
        }

        // Aligns filter textboxes with the colums
        private void UpdateFilterOffset(C1DataGrid dataGrid)
        {
            filterContainer.Margin = new Thickness(dataGrid.RowHeaderWidth, 0, 0, 5);
        }

        // Updates the order of the TextBoxes in the container to 
        // macth the order of the columns
        private void UpdateFiltersOrder(C1DataGrid dataGrid)
        {
            var txtBoxes = new List<TextBox>(filterContainer.Children.Cast<TextBox>());

            filterContainer.Children.Clear();
            foreach (var col in dataGrid.VisibleColumns)
            {
                var txt = GetFilterTextBox(col, txtBoxes);
                filterContainer.Children.Add(txt);
            }
        }


        // Updates the width of the TextBoxes in the container to 
        // match the actualwidth of the columns
        private void UpdateFilterWidths(C1DataGrid dataGrid)
        {
            foreach (var col in dataGrid.Columns)
            {
                // save variables to use inside the lambda
                var tmpCol = col;
                var textbox = GetFilterTextBox(tmpCol);

                // update the width everytime it's drawn
                tmpCol.HeaderPresenter.LayoutUpdated += (s1, a1) =>
                {
                    textbox.Width = Math.Max(0, tmpCol.HeaderPresenter.ActualWidth - textbox.Margin.Left - textbox.Margin.Right);
                };
            }
        }

        private TextBox GetFilterTextBox(DataGridColumn col)
        {
            return GetFilterTextBox(col, new List<TextBox>(filterContainer.Children.Cast<TextBox>()));
        }
        private TextBox GetFilterTextBox(DataGridColumn col, List<TextBox> txtBoxes)
        {
            foreach (TextBox txt in txtBoxes)
            {
                if (txt.Tag == col)
                    return txt;
            }
            return null;
        }

        #endregion
    }
}