using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace CustomColumnNamespace
{
    public class MyCustomFilteringColumnCS : GridBoundColumn
    {
        //RadGrid will call this method when it initializes the controls inside the filtering item cells
        protected override void SetupFilterControls(TableCell cell)
        {
            base.SetupFilterControls(cell);
            cell.Controls.Clear();

            RadComboBox combo = new RadComboBox()
            {
                ID = "RadComboBox1" + this.UniqueName,
                DataSourceID = "SqlDataSource2",
                DataTextField = "ShipCountry",
                DataValueField = "ShipCountry",
                //CheckBoxes = true,
                AutoPostBack = true,
                EmptyMessage = "Select Countries",
                Skin = "Metro",
                AppendDataBoundItems = true
            };
            combo.Items.Add(new RadComboBoxItem("All"));

            combo.SelectedIndexChanged += Combo_SelectedIndexChanged;

            cell.Controls.Add(combo);

            ImageButton button = new ImageButton()
            {
                ID = "ImageButton1" + this.UniqueName,
                ImageUrl = "~/clear_filter.png",
                AlternateText = "Search"
            };
            button.Click += button_Click;
            cell.Controls.Add(button);
        }

        private void Combo_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            GridFilteringItem filterItem = (GridFilteringItem)((RadComboBox)sender).NamingContainer;
            filterItem.FireCommandEvent("Filter", new Pair("Contains", this.UniqueName));
        }

        protected void button_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            GridFilteringItem filterItem = (GridFilteringItem)((ImageButton)sender).NamingContainer;
            (filterItem[this.UniqueName].FindControl("RadComboBox1" + this.UniqueName) as RadComboBox).ClearSelection();
            filterItem.FireCommandEvent("Filter", new Pair("NoFilter", this.UniqueName));
        }
        //RadGrid will call this method when the value should be set to the filtering input control(s)
        protected override void SetCurrentFilterValueToControl(TableCell cell)
        {
            base.SetCurrentFilterValueToControl(cell);
            RadComboBox combo = (RadComboBox)cell.Controls[0];
            combo.SelectedValue = this.CurrentFilterValue;
        }
        //RadGrid will call this method when the filtering value should be extracted from the filtering input control(s)
        protected override string GetCurrentFilterValueFromControl(TableCell cell)
        {
            RadComboBox combo = (RadComboBox)cell.Controls[0];
            return combo.SelectedValue;
        }
        // Building the filter expression for the custom column, which will be added to the final FilterExpression of the GridTableView
        public override string EvaluateFilterExpression(GridFilteringItem filteringItem)
        {
            if (!this.SupportsFiltering())
            {
                return string.Empty;
            }

            TableCell cell = filteringItem[this.UniqueName];
            string filterValue = this.GetCurrentFilterValueFromControl(cell);
            if (String.IsNullOrEmpty(filterValue))
            {
                return "";
            }
            else
            {
                RadComboBox combo = (RadComboBox)filteringItem[this.UniqueName].Controls[0];

                return "(it[\"" + this.UniqueName + "\"].ToString().Contains(\"" + combo.SelectedValue + "\"))";
            }
        }
    }
}