public Question PopQuestion(IQuestionKey keyToUseForReference) { ResolveDeletionsAndAdditions(); PhraseCustomization questionToInsert; try { questionToInsert = AdditionsAndInsertions.First(); } catch (Exception e) { throw new InvalidOperationException("PopQuestion should only be called for a customization that is not tied to any other question and has " + "at least one insertion or addition.", e); } AdditionsAndInsertions.RemoveAt(0); var newQ = new Question(keyToUseForReference.ScriptureReference, keyToUseForReference.StartRef, keyToUseForReference.EndRef, questionToInsert.ModifiedPhrase ?? questionToInsert.OriginalPhrase, questionToInsert.Answer); SetExcludedAndModified(newQ); if (AllAnswers != null && AllAnswers.Any()) // Note: If there are any, there are at least 2 { newQ.Answers = AllAnswers.ToArray(); } return(newQ); }
private void FinishResolvingIfNoMorePairsCanBeDeleted() { if (Deletions.Any() && AdditionsAndInsertions.Count > Deletions.Count) { return; } if (AdditionsAndInsertions.Count <= 1) { if (Deletions.Count > 1) { // This should probably be an exception, but maybe we can recover... Deletions.RemoveRange(1, Deletions.Count - 1); Debug.Fail($"There were more deletions than additions for {Deletions.Single().Key}."); } } else if (Deletions.Count <= 1) { // REVIEW: We're assuming that the first (remaining) addition is the "base" one (i.e., any other non-duplicates will // be hanging off of it as an insertion or addition). If this is not true, we'll need to look through the list to find // the first one whose OriginalPhrase is not the ModifiedPhrase of any other addition/insertion in the list. var baseAddition = AdditionsAndInsertions[0].ModifiedPhrase; var i = 0; while (i + 1 < AdditionsAndInsertions.Count) { // We assume that earlier ones in the list are older versions whose answers are less likely to be the most desirable // one, so we delete ealier ones first so that the last one survives (and its answer will be inserted first in the // list. Sadly, this is probably the best we can do. var iNewBase = AdditionsAndInsertions.FindIndex(i + 1, a => a.ModifiedPhrase == baseAddition); if (iNewBase < 0) { break; } RemoveAddition(i); i = iNewBase; } } else { return; } if (AllAnswers.Any()) { var bestAnswer = AdditionsAndInsertions.First().Answer; if (!String.IsNullOrWhiteSpace(bestAnswer) && !AllAnswers.Any(a => a.Contains(bestAnswer))) { AllAnswers.Insert(0, bestAnswer); } else if (AllAnswers.Count == 1) { AdditionsAndInsertions.First().Answer = AllAnswers[0]; AllAnswers = null; } } m_isResolved = true; }