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 PlaylistItemBLL
    {
        private PlaylistItemTableAdapter _playlistItemAdapter;

        protected PlaylistItemTableAdapter Adapter
        {
            get
            {
                if (_playlistItemAdapter == null)
                    _playlistItemAdapter = new PlaylistItemTableAdapter();
                return _playlistItemAdapter;
            }
        }

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

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public bool AddPlaylistItem(int playlistID, int songID)
        {
            MyTunesDataSet.PlaylistItemDataTable playlistItems = new MyTunesDataSet.PlaylistItemDataTable();

            MyTunesDataSet.PlaylistItemRow playlistItem = playlistItems.NewPlaylistItemRow();
            playlistItem.playlist_id = playlistID;
            playlistItem.song_id = songID;

            playlistItems.AddPlaylistItemRow(playlistItem);

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

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public bool UpdatePlaylistItem(int playlistItemID, int songID)
        {
            MyTunesDataSet.PlaylistItemDataTable playlistItems = Adapter.GetPlaylistItemsByPlaylistItemID(playlistItemID);
            if (playlistItems.Count == 0)
                return false;

            MyTunesDataSet.PlaylistItemRow playlistItem = playlistItems[0];

            playlistItem.song_id = songID;

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

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

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public bool DeletePlaylistItem(int playlistItemID)
        {
            MyTunesDataSet.PlaylistItemDataTable playlistItems = Adapter.GetPlaylistItemsByPlaylistItemID(playlistItemID);
            if (playlistItems.Count == 0)
                return false;
            return DeletePlaylistItem(playlistItemID, playlistItems[0].changed);
        }
    }
}