using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace MyTunes.Library
{
    /// <summary>
    /// Provides a way to pass the current filter value during the event.
    /// </summary>
    public class SearchEventArgs
    {
        private string _filter;

        public SearchEventArgs(string filter)
        {
            _filter = filter;
        }

        public string Filter
        {
            get { return _filter; }
            set { _filter = value; }
        }
    }

    [Bindable(true),Browsable(true),Category("UserControls")]
    public partial class SearchPagerControl : System.Web.UI.UserControl, INotifyPropertyChanged
    {
        private GridView _gridView = null;

        /// <summary>
        /// Gets or sets the GridView the pager is controlling/monitoring.
        /// </summary>
        public GridView GridView
        {
            get { return _gridView; }
            set
            {
                if (_gridView != null)
                    ReleaseEventHandlers();
                _gridView = value;
                if (_gridView != null)
                    SetupEventHandlers();
            }
        }

        /// <summary>
        /// Gets or sets the current search value.
        /// </summary>
        public string Filter
        {
            get { return txtFilter.Text; }
            set
            {
                txtFilter.Text = value;
                NotifyPropertyChanged("Filter");
            }
        }

        /// <summary>
        /// Event handler delegates describe a method.
        /// </summary>
        /// <param name="sender">the object that generated the event</param>
        /// <param name="e">a SearchEventArgs object with Filter property</param>
        public delegate void SearchEventHandler(object sender, SearchEventArgs e);
        public event SearchEventHandler Search;

        protected void btnFilterGo_Click(object sender, ImageClickEventArgs e)
        {
            if (Search != null)
                Search(this, new SearchEventArgs(txtFilter.Text));
        }

        private void SetupEventHandlers()
        {
            _gridView.DataBound += new EventHandler(_gridView_DataBound);
        }

        private void ReleaseEventHandlers()
        {
            _gridView.DataBound -= new EventHandler(_gridView_DataBound);
        }

        void _gridView_DataBound(object sender, EventArgs e)
        {
            lblPageInfo.Text = string.Format("Page {0} of {1}",
                _gridView.PageIndex + 1, _gridView.PageCount);

            lstPageSelect.Items.Clear();
            for (int i = 0; i < _gridView.PageCount; i++)
            {
                ListItem pageItem = new ListItem(string.Concat("Page ", i + 1), i.ToString());
                lstPageSelect.Items.Add(pageItem);
                if (i == _gridView.PageIndex)
                    pageItem.Selected = true;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void lstPageSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            _gridView.PageIndex = Convert.ToInt32(lstPageSelect.SelectedValue);
        }

        protected void txtFilter_TextChanged(object sender, EventArgs e)
        {
            if (Search != null)
                Search(this, new SearchEventArgs(txtFilter.Text));
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
        #endregion
    }
}