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
{
    public partial class ArtistsWebForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ArtistGridView.PageIndex = 0;
                UpdateFilter();
            }
        }

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

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

        private void UpdateFilter()
        {
            using (MyTunesDatabaseEntities myEntities = new MyTunesDatabaseEntities())
            {
                if (!string.IsNullOrEmpty(txtArtistLinqFilter.Text))
                {
                    var allArtists = from artist in myEntities.artists
                                     orderby artist.artist1 ascending
                                     where artist.artist1.Contains(txtArtistLinqFilter.Text)
                                     select new { artist.artist_id, artist.artist1, artist.albums.Count };
                    ArtistGridView.DataSource = allArtists;
                }
                else
                {
                    var allArtists = from artist in myEntities.artists
                                     orderby artist.artist1 ascending
                                     where artist.artist1.Contains(txtArtistLinqFilter.Text)
                                     select new { artist.artist_id, artist.artist1, artist.albums.Count };
                    ArtistGridView.DataSource = allArtists;
                }

                ArtistGridView.DataBind();
            }
        }
    }
}