using System.Text.RegularExpressions;

namespace MileageTraker.Web.Utility
{
	public static class NameNormalizer
	{
		private const string NamePattern = @"(?<Last>\w+),\s*(?<First>\w+)(?:\s*)?(?<Suffix>(jr|sr|iii))?";
		private static readonly Regex NameRegex = new Regex(NamePattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled);

		public static string Normalize(string name)
		{
			var match = NameRegex.Match(name);
			if (!match.Success)
				return name;
			var normalizedName = string.Format("{0} {1}", match.Groups["First"], match.Groups["Last"]);
			if (match.Groups["Suffix"].Success)
			{
				var suffix = match.Groups["Suffix"].Value;
				normalizedName = string.Format("{0} {1}.", normalizedName, suffix);
			}
			return normalizedName.TrimEnd();
		}
	}
}