using System.Xml;
using MusicMetaTagger.Client.AllMusicGuide.Properties;
using MusicMetaTagger.Core.Model;

namespace MusicMetaTagger.Client.AllMusicGuide.RemoteDataAccess.Parser
{
	public class SongResultParser : ParserBase<SongResult>
	{
		public override SongResult Parse(XmlDocument xml)
		{
			var songResult = new SongResult();
			var settings = SongResultScraper.Default;

			songResult.SongTitle = XpathMatchString(xml, settings.SongTitleXpath);
			if (RegexMatch(songResult.SongTitle, settings.SongTitleRegex))
				songResult.SongTitle = RegexCapture(songResult.SongTitle, settings.SongTitleRegex);

			var songUrl = XpathMatchString(xml, settings.SongUrlIdXpath);
			songResult.SongId = RegexCapture(songUrl, settings.SongUrlIdRegex);

			if (XpathMatch(xml, settings.ArtistUrlXpath))
			{
				var artistId = XpathMatchString(xml, settings.ArtistUrlXpath);
				songResult.ArtistId = RegexCapture(artistId, settings.ArtistUrIdRegex);
				songResult.ArtistName = XpathMatchString(xml, settings.ArtistXpath);
			}

			return songResult;
		}
	}
}