using System.Collections.Generic;
using System.Linq;
using System.Xml;
using MusicMetaTagger.Client.AllMusicGuide.Properties;
using MusicMetaTagger.Core.Model;

namespace MusicMetaTagger.Client.AllMusicGuide.RemoteDataAccess.Parser
{
	public class AlbumResultPageParser : ParserBase<List<AlbumResult>>
	{
		public override List<AlbumResult> Parse(XmlDocument xml)
		{
			var albumResults = new List<AlbumResult>();
			var settings = AlbumResultPageScraper.Default;
			var albumResultScraper = new AlbumResultParser();

			if (XpathMatch(xml, settings.AlbumXpath))
			{
				var albumResultInfoXmlList = XpathMatchXmlList(xml, settings.AlbumXpath);
				albumResults.AddRange(albumResultInfoXmlList.Select(albumResultScraper.Parse));
				var i = 1;
				albumResults.ForEach(ar => ar.ResultOrder = i++);
			}
			return albumResults;
		}
	}
}