private async void UpdateSelectedAyah(QuranAyah ayahInfo)
        {
            PageViewModel page = DataContext as PageViewModel;

            if (ayahInfo != null && page != null)
            {
                // Wait for translations to load
                await _listLoaded.WaitAsync();

                VerseViewModel selectedTranslation = page.Translations.FirstOrDefault(t => t.Surah == SelectedAyah.Surah && t.Ayah == SelectedAyah.Ayah);
                if (selectedTranslation != null)
                {
                    TranslationListBox.ScrollIntoView(selectedTranslation);
                    SelectedAyahDelayed = ayahInfo;
                }
            }
        }
        private VerseViewModel CreateVerseViewModel(
            VerseAnalysis verseAnalysis,
            VerseAnalysisWord analysisWord,
            VerseAnalysisWordPart analysisWordPart)
        {
            const int WordsBeforeAndAfter = 3;
            int       lower = Math.Max(1, analysisWord.WordNumber - WordsBeforeAndAfter);
            int       upper = Math.Min(verseAnalysis.Words.Count, analysisWord.WordNumber + WordsBeforeAndAfter);
            var       words = new List <VerseAnalysisWord>();

            for (int index = lower - 1; index < upper; index++)
            {
                words.Add(verseAnalysis.Words[index]);
            }

            var result = new VerseViewModel(
                chapterNumber: verseAnalysis.ChapterNumber,
                verseNumber: verseAnalysis.VerseNumber,
                selectedWord: analysisWord,
                selectedWordPart: analysisWordPart,
                words: words);

            return(result);
        }
        public ActionResult Index(string rootLetterNames)
        {
            string root = ArabicHelper.LetterNamesToArabic(rootLetterNames);
            IEnumerable <VerseAnalysis> verses =
                VerseAnalysisRepository.GetForRoot(root)
                .OrderBy(x => x.ChapterNumber)
                .ThenBy(x => x.VerseNumber);
            var extracts = new List <VerseViewModel>();

            foreach (VerseAnalysis verseAnalysis in verses)
            {
                foreach (VerseAnalysisWord analysisWord in verseAnalysis.Words)
                {
                    IEnumerable <VerseAnalysisWordPart> wordParts =
                        analysisWord.WordParts.Where(x => x.Root == root);
                    foreach (VerseAnalysisWordPart wordPart in wordParts)
                    {
                        VerseViewModel extract =
                            CreateVerseViewModel(verseAnalysis, analysisWord, wordPart);
                        extracts.Add(extract);
                    }
                }
            }

            var romanNumerals = new List <string> {
                "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"
            };
            var wordFormsData = extracts
                                .GroupBy(x => new
            {
                Type = x.SelectedWordPart.Description,
                x.SelectedWordPart.Form
            })
                                .OrderBy(x => romanNumerals.IndexOf(x.Key.Form));

            var wordTypesData = wordFormsData
                                .GroupBy(x => x.Key.Type)
                                .OrderBy(x => x.Key);

            var wordTypesViewModel = wordTypesData
                                     .Select(t =>
                                             new WordTypeViewModel(
                                                 type: t.Key,
                                                 wordForms: t.Select(f =>
                                                                     new WordFormViewModel(
                                                                         form: f.Key.Form,
                                                                         extracts: f
                                                                         )
                                                                     )
                                                 )
                                             );

            IEnumerable <DictionaryEntry> dictionaryEntries =
                DictionaryEntryRepository.Get(root);
            IEnumerable <Dictionary> dictionaries =
                dictionaryEntries
                .Select(x => x.DictionaryCode)
                .Distinct()
                .Select(DictionaryRepository.Get)
                .OrderBy(x => x.Name);

            var viewModel = new ViewModel(
                arabicRoot: root,
                rootLetterNames: rootLetterNames,
                dictionaries: dictionaries,
                types: wordTypesViewModel);

            return(View("RootAnalysis", viewModel));
        }