using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyTunes.Common.DAL;
using MyTunes.Common.DAL.MyTunesDataSetTableAdapters;

namespace MyTunes.Common.BLL
{
    [System.ComponentModel.DataObject]
    public class AlbumBLL
    {
        private AlbumTableAdapter _albumAdapter = null;

        protected AlbumTableAdapter Adapter
        {
            get
            {
                if (_albumAdapter == null)
                    _albumAdapter = new AlbumTableAdapter();
                return _albumAdapter;
            }
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public MyTunesDataSet.AlbumDataTable GetAlbums()
        {
            return Adapter.GetAlbums();
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public MyTunesDataSet.AlbumDataTable GetAlbumsByAlbumID(int albumID)
        {
            return Adapter.GetAlbumsByAlbumID(albumID);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public MyTunesDataSet.AlbumDataTable GetAlbumsByArtistID(int artistID)
        {
            return Adapter.GetAlbumsByArtistID(artistID);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public MyTunesDataSet.AlbumDataTable GetAlbumsByArtistName(string artistName)
        {
            return Adapter.GetDataByArtistName(artistName);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public MyTunesDataSet.AlbumDataTable GetAlbumsByAlbumFirstLetter(string letter)
        {
            return Adapter.GetAlbumsByAlbumFirstLetter(letter);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public MyTunesDataSet.AlbumDataTable GetAlbumsByArtistFirstLetter(string letter)
        {
            return Adapter.GetAlbumsByArtistFirstLetter(letter);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public bool AddAlbum(int artistID, string albumName)
        {
            MyTunesDataSet.AlbumDataTable albums = new MyTunesDataSet.AlbumDataTable();
            MyTunesDataSet.AlbumRow album = albums.NewAlbumRow();
            album.artist_id = artistID;
            album.album = albumName;
            albums.AddAlbumRow(album);
            int rowsAffected = Adapter.Update(albums);
            return (rowsAffected == 1);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public bool UpdateAlbum(int albumID, string albumName)
        {
            MyTunesDataSet.AlbumDataTable albums = Adapter.GetAlbumsByAlbumID(albumID);
            if (albums.Count == 0)
                return false;
            MyTunesDataSet.AlbumRow album = albums[0];
            album.album = albumName;
            int rowsAffected = Adapter.Update(album);
            return (rowsAffected == 1);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public bool DeleteAlbum(int albumID, byte[] changed)
        {
            int rowsAffected = Adapter.Delete(albumID, changed);
            return (rowsAffected == 1);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public bool DeleteAlbum(int albumID)
        {
            MyTunesDataSet.AlbumDataTable albums = Adapter.GetAlbumsByAlbumID(albumID);
            if (albums.Count == 0)
                return false;
            return DeleteAlbum(albumID, albums[0].changed);
        }
    }
}