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

namespace MyTunes.BLLObjectBinding
{
    public partial class AlbumsBLL : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["artist"] != null)
                MyTunesAlbumBLL.FilterExpression = "artist_id = '" + Request.Params["artist"] + "'";

            if (!string.IsNullOrEmpty(txtAlbumFilter.Text))
                MyTunesAlbumBLL.FilterExpression = "artist LIKE '%" + txtAlbumFilter.Text + "%'";
        }

        protected void btnFilterGo_Click(object sender, ImageClickEventArgs e)
        {
            if (!string.IsNullOrEmpty(txtAlbumFilter.Text))
                MyTunesAlbumBLL.FilterExpression = "album LIKE '%" + txtAlbumFilter.Text + "%'";
            else
                MyTunesAlbumBLL.FilterExpression = null;

            AlbumBLLGridView.DataBind();
        }

        protected void txtAlbumFilter_TextChanged(object sender, EventArgs e)
        {
            btnFilterGo_Click(null, null);
        }

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

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

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