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

namespace MusicMetaTagger.Client.AllMusicGuide.RemoteDataAccess.Parser
{
	public class ArtistResultParser : ParserBase<ArtistResult>
	{
		public override ArtistResult Parse(XmlDocument xml)
		{
			var artistResult = new ArtistResult();
			var settings = ArtistResultScraper.Default;

			var artistId = XpathMatchString(xml, settings.ArtistUrlXpath);
			artistResult.ArtistId = RegexCapture(artistId, settings.ArtistUrIdRegex);

			artistResult.ArtistName = XpathMatchString(xml, settings.ArtistXpath);
			if (XpathMatch(xml, settings.InfoXpath))
			{
				var infoRaw = XpathMatchString(xml, settings.InfoXpath);
				if (RegexMatch(infoRaw, settings.YearsActiveRegex))
				{
					var yearsActive = RegexCapture(infoRaw, settings.YearsActiveRegex);
					artistResult.YearsActive = yearsActive.Trim();
				}
				if (RegexMatch(infoRaw, settings.GenreRegex))
				{
					var genre = RegexCapture(infoRaw, settings.GenreRegex);
					artistResult.Genre = genre.Trim();
				}
			}
			return artistResult;
		}
	}
}