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

namespace MyTunes.Cached
{
    public partial class AlbumsCached : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(AlbumsCachedGridView.SortExpression))
                AlbumsCachedGridView.Sort("Artist", SortDirection.Ascending);

            if (!IsPostBack)
            {
                Global g = (Global)HttpContext.Current.ApplicationInstance;
                lblQuickLinks.Text = g.AlbumQuickLinks;
            }

            if (Request.Params["letter"] == null)
                AlbumCachedDataSource.FilterExpression = "";
            else
            {
                AlbumsCachedGridView.PageIndex = 0;
                AlbumCachedDataSource.FilterExpression = "album LIKE '" + Request.Params["letter"] + "%'";
            }
        }

        protected void AlbumsCachedGridView_DataBound(object sender, EventArgs e)
        {
            lblPageInfoCached.Text = string.Format("You are viewing page {0} of {1}...",
                AlbumsCachedGridView.PageIndex + 1, AlbumsCachedGridView.PageCount);

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

        protected void txtAlbumFilterCached_TextChanged(object sender, EventArgs e)
        {
            UpdateFilter();
        }

        protected void btFilterGoCached_Click(object sender, ImageClickEventArgs e)
        {
            UpdateFilter();
        }

        private void UpdateFilter()
        {
            if (!string.IsNullOrEmpty(txtAlbumFilterCached.Text))
                AlbumCachedDataSource.FilterExpression = "artist LIKE '%" + txtAlbumFilterCached.Text + "%'";
            else
                AlbumCachedDataSource.FilterExpression = null;

            AlbumsCachedGridView.DataBind();
        }

        protected void lstPageSelectCached_SelectedIndexChanged(object sender, EventArgs e)
        {
            AlbumsCachedGridView.PageIndex = Convert.ToInt32(lstPageSelectCached.SelectedValue);
        }
    }
}