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 SongResultPageParser : ParserBase<List<SongResult>>
	{
		public override List<SongResult> Parse(XmlDocument xml)
		{
			var songResults = new List<SongResult>();
			var settings = SongResultPageScraper.Default;
			var songResultScraper = new SongResultParser();

			if (XpathMatch(xml, settings.SongXpath))
			{
				var songResultInfoXmlList = XpathMatchXmlList(xml, settings.SongXpath);
				songResults.AddRange(songResultInfoXmlList.Select(songResultScraper.Parse));
				var i = 1;
				songResults.ForEach(ar => ar.ResultOrder = i++);
			}
			return songResults;
		}
	}
}
