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 PlaylistBLL
    {
        private PlaylistTableAdapter _PlaylistAdapter;

        protected PlaylistTableAdapter Adapter
        {
            get
            {
                if (_PlaylistAdapter == null)
                    _PlaylistAdapter = new PlaylistTableAdapter();
                return _PlaylistAdapter;
            }
        }

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

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public bool AddPlaylist(int customerID, string title)
        {
            MyTunesDataSet.PlaylistDataTable playlists = new MyTunesDataSet.PlaylistDataTable();

            MyTunesDataSet.PlaylistRow playlist = playlists.NewPlaylistRow();
            playlist.customer_id = customerID;
            playlist.title = title;

            playlists.AddPlaylistRow(playlist);

            int rowsAffected = Adapter.Update(playlists);
            return (rowsAffected == 1);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public bool UpdatePlaylist(int playlistID, string title)
        {
            MyTunesDataSet.PlaylistDataTable playlists = Adapter.GetPlaylistsByPlaylistID(playlistID);
            if (playlists.Count == 0)
                return false;

            MyTunesDataSet.PlaylistRow playlist = playlists[0];

            playlist.title = title;

            int rowsAffected = Adapter.Update(playlist);
            return (rowsAffected == 1);
        }

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

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public bool DeletePlaylist(int playlistID)
        {
            MyTunesDataSet.PlaylistDataTable playlists = Adapter.GetPlaylistsByPlaylistID(playlistID);
            if (playlists.Count == 0)
                return false;
            return DeletePlaylist(playlistID, playlists[0].changed);
        }
    }
}