using System;
using System.Xml;
using MusicMetaTagger.Client.AllMusicGuide.RemoteDataAccess.Parser;
using NUnit.Framework;

namespace MusicMetaTagger.Client.AllMusicGuide.Tests.RemoteDataAccess.Parser
{
	[TestFixture]
	public class AlbumParserTests
	{
		[Test]
		public void IsSoundtrack()
		{
			var pf = new XmlDocument();
			pf.Load("Album-PulpFiction.xml");
			var pulpFiction = new AlbumParser().Parse(pf);

			Assert.IsTrue(pulpFiction.Soundtrack);
			Assert.That(pulpFiction.ArtistName, Is.EqualTo("Various Artists"));
			Assert.That(pulpFiction.ArtistId, Is.Null);
		}

		[Test]
		public void ParsesScore()
		{
			var mag = new XmlDocument();
			mag.Load("Album-MagnoliaScore.xml");
			var album = new AlbumParser().Parse(mag);
			Assert.IsNotNull(album);
		}

		[Test]
		public void ParsesMultipleArtists()
		{
			var xml = new XmlDocument();
			xml.Load("Album-Superwolf.xml");
			var superwolf = new AlbumParser().Parse(xml);

			Assert.That(superwolf.ArtistName, Is.EqualTo("Bonnie \"Prince\" Billy"));
			Assert.That(superwolf.ArtistId, Is.EqualTo("mn0000094764"));
		}

		[Test]
		public void ParsesTrueLoveWaits()
		{
			var xml = new XmlDocument();
			xml.Load("Album-TrueLoveWaits.xml");
			var trueLoveWaits = new AlbumParser().Parse(xml);

			Assert.That(trueLoveWaits.ArtistName, Is.EqualTo("Christopher O'Riley"));
			Assert.That(trueLoveWaits.ArtistId, Is.EqualTo("mn0000043765"));
			Assert.That(trueLoveWaits.Tracks.Count, Is.EqualTo(15));
		}

		[Test]
		public void PopulateTest()
		{
			var ar = new XmlDocument();
			ar.Load("Album-AbbeyRoad.xml");
			var abbeyRoad = new AlbumParser().Parse(ar);

			Assert.AreEqual("Abbey Road", abbeyRoad.Title);
			Assert.AreEqual("September 26, 1969", abbeyRoad.ReleaseDate);
			Assert.AreEqual("http://cps-static.rovicorp.com/3/JPG_250/MI0002/910/MI0002910443.jpg", abbeyRoad.CoverUrl);
			Assert.That(abbeyRoad.Styles.Count, Is.GreaterThan(1));
			Assert.IsTrue(abbeyRoad.Moods.Count > 0);
			Assert.IsTrue(abbeyRoad.Review.Length > 0);
			Assert.IsTrue(abbeyRoad.Themes.Count > 0);
			Assert.AreEqual("The Beatles", abbeyRoad.ArtistName);
			Assert.AreEqual("Richie Unterberger", abbeyRoad.Reviewer);
			Assert.AreEqual("mn0000754032", abbeyRoad.ArtistId);
			Assert.AreEqual("mw0000192938", abbeyRoad.AlbumId);
			Assert.AreEqual(10, abbeyRoad.Rating);
			Assert.Less(1, abbeyRoad.Review.Length);
			Assert.IsFalse(abbeyRoad.Soundtrack);

			// track check
			Assert.That(abbeyRoad.Tracks.Count, Is.EqualTo(17));
			var firstTrack = abbeyRoad.Tracks[0];

			Assert.AreEqual(1, firstTrack.TrackNumber);
			Assert.AreEqual("mt0010100291", firstTrack.TrackId);
			Assert.IsTrue(firstTrack.Pick);
			Assert.AreEqual(firstTrack.TrackLength, new TimeSpan(0, 4, 19));
			Assert.AreEqual(abbeyRoad.AlbumId, firstTrack.AlbumId);
			Assert.AreEqual("mn0000754032", firstTrack.PerformerIds[0]);


			var gct = new XmlDocument();
			gct.Load("Album-GirlsCanTell.xml");
			var girlsCanTell = new AlbumParser().Parse(gct);

			Assert.IsTrue(girlsCanTell.Pick);
		}
		
		[Test]
		public void SerializationTest()
		{
			var ar = new XmlDocument();
			ar.Load("Album-AbbeyRoad.xml");
			var abbeyRoad = new AlbumParser().Parse(ar);

			SerializationTester.AssertBinarySerializable(abbeyRoad);
		}
	}
}