//
// Author: Eoin Hennessy <eoin@randomrules.org>
//
// Test to demonstrate Banshee OSX issue #559654. This shows how setting Xspf.Playlist.DefaultBaseUri 
// to CurrentDirectory breaks on OSX when running from a bundle. When running from a bundle 
// Environment.CurrentDirectory is '/' which fails URI validation during parsing of the playlist.
//
// Note: Compile and run as follows to simulate running from a bundle:
//   pushd / && mono --debug /path/to/PlaylistTest.exe [lastfm-username] [lastfm-password] && popd
//

using System;
using System.IO;
using System.Threading;
using Mono.Unix;

using Hyena;
using Lastfm;
using Media.Playlists.Xspf;

namespace PlaylistTest
{
    public class PlaylistTest
    {
        public static void Main (string [] args)
        {
            if (args.Length != 2) {
                System.Console.Write("  Usage: PlaylistTest.exe [lastfm-username] [lastfm-password]\n");
                return;
            }

            // args
            string username = args[0];
            string password = args[1];

            Log.Debugging = true;
            try {
                
                // Setup connection
                RadioConnection connection = LastfmCore.Radio;

                Account account = LastfmCore.Account;
                account.UserName = username;
                account.Password = password;

                // Simulate network connect
                connection.UpdateNetworkState(true);
                connection.Connect();

                // Run connection loop
                while (!connection.Connected) {
                    Thread.Sleep(100);
                }

                string station = String.Format("lastfm://user/{0}/loved", account.UserName);
                if (connection.ChangeStationTo(station) != StationError.None) {
                    Log.Error("Failed to change station.");
                    return;
                }

                // Create a playlist.. should throw if app was run from bundle or currentdir is /
                connection.LoadPlaylistFor(connection.Station);
            }
            catch (Exception ex) {
                Log.Debug("Exception: " + ex.Message);
            }
        }
    }
}
