private async Task WindowClosing()
        {
            await ThreadSwitcher.ResumeBackgroundAsync();

            if (!_hasUnsavedChangesToCheck.HasChanges())
            {
                _closeConfirmed = true;
                await ThreadSwitcher.ResumeForegroundAsync();

                _toClose.Close();
            }

            if (await StatusContext.ShowMessage("Unsaved Changes...",
                                                "There are unsaved changes - do you want to discard your changes?",
                                                new List <string> {
                "Yes - Close Window", "No"
            }) == "Yes - Close Window")
            {
                _closeConfirmed = true;
                await ThreadSwitcher.ResumeForegroundAsync();

                _toClose.Close();
            }
        }
        public static async Task PdfPageToImageWithPdfToCairo(StatusControlContext statusContext,
                                                              List <FileContent> selected, int pageNumber)
        {
            await ThreadSwitcher.ResumeBackgroundAsync();


            var pdfToCairoDirectoryString = UserSettingsSingleton.CurrentSettings().PdfToCairoExeDirectory;

            if (string.IsNullOrWhiteSpace(pdfToCairoDirectoryString))
            {
                statusContext.ToastError(
                    "Sorry - this function requires that pdftocairo.exe be on the system - please set the directory... ");
                return;
            }

            var pdfToCairoDirectory = new DirectoryInfo(pdfToCairoDirectoryString);

            if (!pdfToCairoDirectory.Exists)
            {
                statusContext.ToastError(
                    $"{pdfToCairoDirectory.FullName} doesn't exist? Check your pdftocairo bin directory setting.");
                return;
            }

            var pdfToCairoExe = new FileInfo(Path.Combine(pdfToCairoDirectory.FullName, "pdftocairo.exe"));

            if (!pdfToCairoExe.Exists)
            {
                statusContext.ToastError(
                    $"{pdfToCairoExe.FullName} doesn't exist? Check your pdftocairo bin directory setting.");
                return;
            }

            var toProcess = new List <(FileInfo targetFile, FileInfo destinationFile, FileContent content)>();

            foreach (var loopSelected in selected)
            {
                var targetFile = new FileInfo(Path.Combine(
                                                  UserSettingsSingleton.CurrentSettings().LocalSiteFileContentDirectory(loopSelected).FullName,
                                                  loopSelected.OriginalFileName));

                if (!targetFile.Exists)
                {
                    continue;
                }

                if (!targetFile.Extension.ToLower().Contains("pdf"))
                {
                    continue;
                }

                FileInfo destinationFile;
                if (pageNumber == 1)
                {
                    destinationFile = new FileInfo(Path.Combine(UserSettingsUtilities.TempStorageDirectory().FullName,
                                                                $"{Path.GetFileNameWithoutExtension(targetFile.Name)}-CoverPage.jpg"));

                    if (destinationFile.Exists)
                    {
                        destinationFile.Delete();
                        destinationFile.Refresh();
                    }
                }
                else
                {
                    destinationFile = new FileInfo(Path.Combine(UserSettingsUtilities.TempStorageDirectory().FullName,
                                                                $"{Path.GetFileNameWithoutExtension(targetFile.Name)}-Page.jpg"));
                }

                toProcess.Add((targetFile, destinationFile, loopSelected));
            }

            if (!toProcess.Any())
            {
                statusContext.ToastError("No PDFs found? This process can only generate PDF previews...");
                return;
            }

            foreach (var loopSelected in toProcess)
            {
                string executionParameters;


                if (pageNumber == 1)
                {
                    executionParameters =
                        $"-jpeg -singlefile \"{loopSelected.targetFile.FullName}\" \"{Path.Combine(loopSelected.destinationFile.Directory.FullName, Path.GetFileNameWithoutExtension(loopSelected.destinationFile.FullName))}\"";
                }
                else
                {
                    executionParameters =
                        $"-jpeg -f {pageNumber} -l {pageNumber} \"{loopSelected.targetFile.FullName}\" \"{Path.Combine(loopSelected.destinationFile.Directory.FullName, Path.GetFileNameWithoutExtension(loopSelected.destinationFile.FullName))}\"";
                }

                var(success, _, errorOutput) = Processes.ExecuteProcess(pdfToCairoExe.FullName, executionParameters,
                                                                        statusContext.ProgressTracker());

                if (!success)
                {
                    if (await statusContext.ShowMessage("PDF Generation Problem",
                                                        $"Execution Failed for {loopSelected.content.Title} - Continue??{Environment.NewLine}{errorOutput}",
                                                        new List <string> {
                        "Yes", "No"
                    }) == "No")
                    {
                        return;
                    }

                    continue;
                }

                FileInfo updatedDestination = null;

                if (pageNumber == 1)
                {
                    //With the singlefile option pdftocairo uses your filename directly
                    loopSelected.destinationFile.Refresh();
                    updatedDestination = loopSelected.destinationFile;
                }
                else
                {
                    var directoryToSearch = loopSelected.destinationFile.Directory;
                    var baseName          = loopSelected.targetFile.Name;

                    var possibleFiles = directoryToSearch
                                        .EnumerateFiles($"{Path.GetFileNameWithoutExtension(loopSelected.destinationFile.Name)}-*.jpg")
                                        .ToList();

                    foreach (var loopFiles in possibleFiles)
                    {
                        var fileNamePageNumber = loopFiles.Name.Split("-Page-").ToList().Last().Replace(".jpg", "");

                        if (int.TryParse(fileNamePageNumber, out var possiblePageNumber) &&
                            possiblePageNumber == pageNumber)
                        {
                            updatedDestination = loopFiles;
                            break;
                        }
                    }
                }

                if (updatedDestination == null || !updatedDestination.Exists)
                {
                    if (await statusContext.ShowMessage("PDF Generation Problem",
                                                        $"Execution Failed for {loopSelected.content.Title} - Continue??{Environment.NewLine}{errorOutput}",
                                                        new List <string> {
                        "Yes", "No"
                    }) == "No")
                    {
                        return;
                    }

                    continue;
                }

                await ThreadSwitcher.ResumeForegroundAsync();

                var editor = new ImageContentEditorWindow(updatedDestination);
                editor.Show();
                if (pageNumber == 1)
                {
                    editor.ImageEditor.TitleSummarySlugFolder.Title   = $"{loopSelected.content.Title} Cover Page";
                    editor.ImageEditor.TitleSummarySlugFolder.Summary =
                        $"Cover Page from {loopSelected.content.Title}.";
                }
                else
                {
                    editor.ImageEditor.TitleSummarySlugFolder.Title =
                        $"{loopSelected.content.Title} - Page {pageNumber}";
                    editor.ImageEditor.TitleSummarySlugFolder.Summary =
                        $"Page {pageNumber} from {loopSelected.content.Title}.";
                }

                editor.ImageEditor.TitleSummarySlugFolder.TitleToSlug();
                editor.ImageEditor.ShowInSearch.ShowInSearch = false;

                editor.ImageEditor.TitleSummarySlugFolder.Folder = loopSelected.content.Folder;
                editor.ImageEditor.TagEdit.Tags            = loopSelected.content.Tags;
                editor.ImageEditor.BodyContent.BodyContent =
                    $"Generated by pdftocairo from {BracketCodeFiles.FileLinkBracketCode(loopSelected.content)}.";
                await ThreadSwitcher.ResumeBackgroundAsync();
            }
        }
        public static async Task ExtractNewAndShowLinkStreamEditors(string toExtractFrom,
                                                                    IProgress <string> progressTracker, List <string> exludedUrls = null)
        {
            await ThreadSwitcher.ResumeBackgroundAsync();

            exludedUrls ??= new List <string>();
            exludedUrls = exludedUrls.Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => x.Trim().ToLower())
                          .ToList();

            if (string.IsNullOrWhiteSpace(toExtractFrom))
            {
                progressTracker?.Report("Nothing to Extract From");
                return;
            }

            progressTracker?.Report("Looking for URLs");

            var allMatches = StringHelpers.UrlsFromText(toExtractFrom).Where(x =>
                                                                             !x.ToLower().Contains(UserSettingsSingleton.CurrentSettings().SiteUrl) &&
                                                                             !exludedUrls.Contains(x.ToLower())).ToList();

            progressTracker?.Report($"Found {allMatches.Count} Matches");

            var linksToShow = new List <string>();

            var db = await Db.Context();

            foreach (var loopMatches in allMatches)
            {
                progressTracker?.Report($"Checking to see if {loopMatches} exists in database...");

                var alreadyExists = await db.LinkStreams.AnyAsync(x => x.Url.ToLower() == loopMatches.ToLower());

                if (alreadyExists)
                {
                    progressTracker?.Report($"{loopMatches} exists in database...");
                }
                else
                {
                    if (!linksToShow.Contains(loopMatches))
                    {
                        progressTracker?.Report($"Adding {loopMatches} to list to show...");
                        linksToShow.Add(loopMatches);
                    }
                }
            }

            await ThreadSwitcher.ResumeForegroundAsync();

            foreach (var loopLinks in linksToShow)
            {
                progressTracker?.Report($"Launching an editor for {loopLinks}...");

                var newWindow = new LinkStreamEditorWindow(
                    new LinkStream
                {
                    ContentId     = Guid.NewGuid(),
                    CreatedBy     = UserSettingsSingleton.CurrentSettings().DefaultCreatedBy,
                    CreatedOn     = DateTime.Now,
                    Url           = loopLinks,
                    ShowInLinkRss = false
                }, true);

                newWindow.Show();
            }
        }