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 SongBLL
    {
        private SongTableAdapter _songAdapter;

        protected SongTableAdapter Adapter
        {
            get
            {
                if (_songAdapter == null)
                    _songAdapter = new SongTableAdapter();
                return _songAdapter;
            }
        }

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

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public bool AddSong(int artistID, int albumID, string title, string genre, string length, string track, string url)
        {
            MyTunesDataSet.SongDataTable songs = new MyTunesDataSet.SongDataTable();
            
            MyTunesDataSet.SongRow song = songs.NewSongRow();
            song.artist_id = artistID;
            song.album_id = albumID;
            song.title = title;
            song.genre = genre;
            song.length = length;
            song.track = track;
            song.url = url;

            songs.AddSongRow(song);

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

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public bool UpdateSong(int songID, string title, string genre, string length, string track, string url)
        {
            MyTunesDataSet.SongDataTable songs = Adapter.GetSongsBySongID(songID);
            if (songs.Count == 0)
                return false;
            
            MyTunesDataSet.SongRow song = songs[0];

            song.title = title;
            song.genre = genre;
            song.length = length;
            song.track = track;
            song.url = url;
            
            int rowsAffected = Adapter.Update(song);
            return (rowsAffected == 1);
        }

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

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public bool DeleteSong(int songID)
        {
            MyTunesDataSet.SongDataTable songs = Adapter.GetSongsBySongID(songID);
            if (songs.Count == 0)
                return false;
            return DeleteSong(songID, songs[0].changed);
        }
    }
}