using System.Collections.Generic; using System.Linq; using NzbDrone.Core.DecisionEngine.Specifications.Search; using NzbDrone.Core.IndexerSearch.Definitions; using NzbDrone.Core.Parser; using NzbDrone.Core.Parser.Model; namespace NzbDrone.Core.DecisionEngine { public interface IMakeDownloadDecision { IEnumerable GetRssDecision(IEnumerable reports); IEnumerable GetSearchDecision(IEnumerable reports, SearchDefinitionBase searchDefinitionBase); } public class DownloadDecisionMaker : IMakeDownloadDecision { private readonly IEnumerable _specifications; private readonly IParsingService _parsingService; public DownloadDecisionMaker(IEnumerable specifications, IParsingService parsingService) { _specifications = specifications; _parsingService = parsingService; } public IEnumerable GetRssDecision(IEnumerable reports) { foreach (var report in reports) { var parseResult = _parsingService.Map(report); yield return new DownloadDecision(parseResult, GetGeneralRejectionReasons(parseResult).ToArray()); } } public IEnumerable GetSearchDecision(IEnumerable reports, SearchDefinitionBase searchDefinitionBase) { foreach (var report in reports) { var parseResult = _parsingService.Map(report); var generalReasons = GetGeneralRejectionReasons(parseResult); var searchReasons = GetSearchRejectionReasons(parseResult, searchDefinitionBase); yield return new DownloadDecision(parseResult, generalReasons.Union(searchReasons).ToArray()); } } private IEnumerable GetGeneralRejectionReasons(RemoteEpisode report) { return _specifications .OfType() .Where(spec => !spec.IsSatisfiedBy(report)) .Select(spec => spec.RejectionReason); } private IEnumerable GetSearchRejectionReasons(RemoteEpisode report, SearchDefinitionBase searchDefinitionBase) { return _specifications .OfType() .Where(spec => !spec.IsSatisfiedBy(report, searchDefinitionBase)) .Select(spec => spec.RejectionReason); } } }