using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyTunes.Common.Entity;

namespace MyTunes.EntityBinding
{
    public partial class Albums : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public string LookupArtistName(object artist1)
        {
            if (artist1 != null)
            {
                using (MyTunesDatabaseEntities myEntities = new MyTunesDatabaseEntities())
                {
                    var allArtists = from artist in myEntities.artists
                                     orderby artist.artist1 ascending
                                     where artist.artist_id == (int)artist1
                                     select new { artist.artist1 };
                    return allArtists.Single().artist1;
                }
            }
            else
                return string.Empty;
        }

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

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

        private void UpdateFilter()
        {
            if (!string.IsNullOrEmpty(txtAlbumFilterLinq.Text))
            {
                using (MyTunesDatabaseEntities myEntities = new MyTunesDatabaseEntities())
                {
                    var allArtists = from album in myEntities.albums
                                     orderby album.album1 ascending
                                     where album.album1.Contains(txtAlbumFilterLinq.Text)
                                     select new { album.album_id, album.artist_id, album.album1 };
                    AlbumGridView.DataSource = allArtists;
                }
            }
            else
                AlbumGridView.DataSource = EntityDataSource1;

            AlbumGridView.DataBind();
        }

        protected void lstPageSelectLinq_SelectedIndexChanged(object sender, EventArgs e)
        {
            AlbumGridView.PageIndex = Convert.ToInt32(lstPageSelectLinq.SelectedValue);
        }

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

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