using System; using System.Linq; using Sony.ChartBot.Entities.ChartApi; using Sony.ChartBot.Entities.Storage; using Sony.ChartBot.Repository; namespace Sony.ChartBot.Client.InputParsers { public class ArtistAndCountryInputParser : IInputParser> { private readonly ArtistInputParser _artistInputParser; private readonly CountryInputParser _countryInputParser; private readonly CountryRepository _countryRepository; public ArtistAndCountryInputParser( ArtistInputParser artistInputParser, CountryInputParser countryInputParser, CountryRepository countryRepository) { _artistInputParser = artistInputParser; _countryInputParser = countryInputParser; _countryRepository = countryRepository; } public Tuple ParseInput(string text) { text = text.Trim(); if (text.ToLowerInvariant().EndsWith(" worldwide")) { var presumedAristName = text.Substring(0, text.ToLowerInvariant().LastIndexOf(" worldwide", StringComparison.Ordinal)); var artist = _artistInputParser.ParseInput(presumedAristName); var country = _countryRepository.GetGlobalCountry(); return Tuple.Create(artist, country); } var words = text.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries); var lastOccuranceOfWordIn = words.Select(w => w.ToLowerInvariant()).ToList().LastIndexOf("in"); var lastOccuranceOfAt = words.Select(w => w.ToLowerInvariant()).ToList().LastIndexOf("@"); // Both artist and country if (lastOccuranceOfWordIn > 0 || lastOccuranceOfAt > 0) { var indexOfSeparator = lastOccuranceOfWordIn > 0 ? lastOccuranceOfWordIn : lastOccuranceOfAt; var wordsBeforeSeparator = words.Take(indexOfSeparator).ToList(); var wordsAfterSeparator = words.Skip(indexOfSeparator + 1).ToList(); if (wordsBeforeSeparator.Any() && wordsAfterSeparator.Any()) { // Ignore "the" in country names if (wordsAfterSeparator.First().ToLowerInvariant() == "the") { wordsAfterSeparator = wordsAfterSeparator.Skip(1).ToList(); } var presumedAristName = string.Join(" ", wordsBeforeSeparator); var presumedCountryName = string.Join(" ", wordsAfterSeparator); var artist = _artistInputParser.ParseInput(presumedAristName); var country = _countryInputParser.ParseInput(presumedCountryName); return Tuple.Create(artist, country); } } // Just artist return Tuple.Create(_artistInputParser.ParseInput(text), (SupportedCountry)null); } } }