<commit_before>﻿using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Design;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using GitHub.Api;
using GitHub.Extensions;
using GitHub.Factories;
using GitHub.Info;
using GitHub.Models;
using GitHub.Primitives;
using GitHub.Services;
using GitHub.Services.Vssdk.Commands;
using GitHub.VisualStudio;
using ReactiveUI;
using OleMenuCommand = Microsoft.VisualStudio.Shell.OleMenuCommand;

namespace GitHub.ViewModels.GitHubPane
{
    /// <summary>
    /// The view model for the GitHub Pane.
    /// </summary>
    [Export(typeof(IGitHubPaneViewModel))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public sealed class GitHubPaneViewModel : ViewModelBase, IGitHubPaneViewModel, IDisposable
    {
        static readonly Regex pullUri = CreateRoute("/:owner/:repo/pull/:number");

        readonly IViewViewModelFactory viewModelFactory;
        readonly ISimpleApiClientFactory apiClientFactory;
        readonly IConnectionManager connectionManager;
        readonly ITeamExplorerContext teamExplorerContext;
        readonly IVisualStudioBrowser browser;
        readonly IUsageTracker usageTracker;
        readonly INavigationViewModel navigator;
        readonly ILoggedOutViewModel loggedOut;
        readonly INotAGitHubRepositoryViewModel notAGitHubRepository;
        readonly INotAGitRepositoryViewModel notAGitRepository;
        readonly SemaphoreSlim navigating = new SemaphoreSlim(1);
        readonly ObservableAsPropertyHelper<ContentOverride> contentOverride;
        readonly ObservableAsPropertyHelper<bool> isSearchEnabled;
        readonly ObservableAsPropertyHelper<string> title;
        readonly ReactiveCommand<object> navigateBack;
        readonly ReactiveCommand<object> navigateForward;
        readonly ReactiveCommand<Unit> refresh;
        readonly ReactiveCommand<Unit> showPullRequests;
        readonly ReactiveCommand<object> openInBrowser;
        readonly ReactiveCommand<object> help;
        readonly SemaphoreSlim initializing = new SemaphoreSlim(1);
        bool initialized;
        IViewModel content;
        ILocalRepositoryModel localRepository;
        string searchQuery;

        [ImportingConstructor]
        public GitHubPaneViewModel(
            IViewViewModelFactory viewModelFactory,
            ISimpleApiClientFactory apiClientFactory,
            IConnectionManager connectionManager,
            ITeamExplorerContext teamExplorerContext,
            IVisualStudioBrowser browser,
            IUsageTracker usageTracker,
            INavigationViewModel navigator,
            ILoggedOutViewModel loggedOut,
            INotAGitHubRepositoryViewModel notAGitHubRepository,
            INotAGitRepositoryViewModel notAGitRepository)
        {
            Guard.ArgumentNotNull(viewModelFactory, nameof(viewModelFactory));
            Guard.ArgumentNotNull(apiClientFactory, nameof(apiClientFactory));
            Guard.ArgumentNotNull(connectionManager, nameof(connectionManager));
            Guard.ArgumentNotNull(teamExplorerContext, nameof(teamExplorerContext));
            Guard.ArgumentNotNull(browser, nameof(browser));
            Guard.ArgumentNotNull(usageTracker, nameof(usageTracker));
            Guard.ArgumentNotNull(navigator, nameof(navigator));
            Guard.ArgumentNotNull(loggedOut, nameof(loggedOut));
            Guard.ArgumentNotNull(notAGitHubRepository, nameof(notAGitHubRepository));
            Guard.ArgumentNotNull(notAGitRepository, nameof(notAGitRepository));

            this.viewModelFactory = viewModelFactory;
            this.apiClientFactory = apiClientFactory;
            this.connectionManager = connectionManager;
            this.teamExplorerContext = teamExplorerContext;
            this.browser = browser;
            this.usageTracker = usageTracker;
            this.navigator = navigator;
            this.loggedOut = loggedOut;
            this.notAGitHubRepository = notAGitHubRepository;
            this.notAGitRepository = notAGitRepository;

            var contentAndNavigatorContent = Observable.CombineLatest(
                this.WhenAnyValue(x => x.Content),
                navigator.WhenAnyValue(x => x.Content),
                (c, nc) => new { Content = c, NavigatorContent = nc });

            contentOverride = contentAndNavigatorContent
                .SelectMany(x =>
                {
                    if (x.Content == null) return Observable.Return(ContentOverride.Spinner);
                    else if (x.Content == navigator && x.NavigatorContent != null)
                    {
                        return x.NavigatorContent.WhenAnyValue(
                            y => y.IsLoading,
                            y => y.Error,
                            (l, e) =>
                            {
                                if (l) return ContentOverride.Spinner;
                                if (e != null) return ContentOverride.Error;
                                else return ContentOverride.None;
                            });
                    }
                    else return Observable.Return(ContentOverride.None);
                })
                .ToProperty(this, x => x.ContentOverride);

            // Returns navigator.Content if Content == navigator, otherwise null.
            var currentPage = contentAndNavigatorContent
                .Select(x => x.Content == navigator ? x.NavigatorContent : null);

            title = currentPage
                .SelectMany(x => x?.WhenAnyValue(y => y.Title) ?? Observable.Return<string>(null))
                .Select(x => x ?? "GitHub")
                .ToProperty(this, x => x.Title);

            isSearchEnabled = currentPage
                .Select(x => x is ISearchablePageViewModel)
                .ToProperty(this, x => x.IsSearchEnabled);

            navigateBack = ReactiveCommand.CreateCombined(
                currentPage.Select(x => x != null),
                navigator.NavigateBack);

            navigateForward = ReactiveCommand.CreateCombined(
                currentPage.Select(x => x != null),
                navigator.NavigateForward);

            refresh = ReactiveCommand.CreateAsyncTask(
                currentPage.SelectMany(x => x?.WhenAnyValue(
                        y => y.IsLoading,
                        y => y.IsBusy,
                        (loading, busy) => !loading && !busy)
                            ?? Observable.Return(false)),
                _ => navigator.Content.Refresh());
            refresh.ThrownExceptions.Subscribe();

            showPullRequests = ReactiveCommand.CreateAsyncTask(
                this.WhenAny(x => x.Content, x => x.Value == navigator),
                _ => ShowPullRequests());

            openInBrowser = ReactiveCommand.Create(currentPage.Select(x => x is IOpenInBrowser));
            openInBrowser.Subscribe(_ =>
            {
                var url = ((IOpenInBrowser)navigator.Content).WebUrl;
                if (url != null) browser.OpenUrl(url);
            });

            help = ReactiveCommand.Create();
            help.Subscribe(_ =>
            {<commit_msg>Removed unused stuff.<commit_after>﻿using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Design;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using GitHub.Api;
using GitHub.Extensions;
using GitHub.Factories;
using GitHub.Info;
using GitHub.Models;
using GitHub.Primitives;
using GitHub.Services;
using GitHub.Services.Vssdk.Commands;
using GitHub.VisualStudio;
using ReactiveUI;
using OleMenuCommand = Microsoft.VisualStudio.Shell.OleMenuCommand;

namespace GitHub.ViewModels.GitHubPane
{
    /// <summary>
    /// The view model for the GitHub Pane.
    /// </summary>
    [Export(typeof(IGitHubPaneViewModel))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public sealed class GitHubPaneViewModel : ViewModelBase, IGitHubPaneViewModel, IDisposable
    {
        static readonly Regex pullUri = CreateRoute("/:owner/:repo/pull/:number");

        readonly IViewViewModelFactory viewModelFactory;
        readonly ISimpleApiClientFactory apiClientFactory;
        readonly IConnectionManager connectionManager;
        readonly ITeamExplorerContext teamExplorerContext;
        readonly INavigationViewModel navigator;
        readonly ILoggedOutViewModel loggedOut;
        readonly INotAGitHubRepositoryViewModel notAGitHubRepository;
        readonly INotAGitRepositoryViewModel notAGitRepository;
        readonly SemaphoreSlim navigating = new SemaphoreSlim(1);
        readonly ObservableAsPropertyHelper<ContentOverride> contentOverride;
        readonly ObservableAsPropertyHelper<bool> isSearchEnabled;
        readonly ObservableAsPropertyHelper<string> title;
        readonly ReactiveCommand<Unit> refresh;
        readonly ReactiveCommand<Unit> showPullRequests;
        readonly ReactiveCommand<object> openInBrowser;
        readonly ReactiveCommand<object> help;
        readonly SemaphoreSlim initializing = new SemaphoreSlim(1);
        bool initialized;
        IViewModel content;
        ILocalRepositoryModel localRepository;
        string searchQuery;

        [ImportingConstructor]
        public GitHubPaneViewModel(
            IViewViewModelFactory viewModelFactory,
            ISimpleApiClientFactory apiClientFactory,
            IConnectionManager connectionManager,
            ITeamExplorerContext teamExplorerContext,
            IVisualStudioBrowser browser,
            IUsageTracker usageTracker,
            INavigationViewModel navigator,
            ILoggedOutViewModel loggedOut,
            INotAGitHubRepositoryViewModel notAGitHubRepository,
            INotAGitRepositoryViewModel notAGitRepository)
        {
            Guard.ArgumentNotNull(viewModelFactory, nameof(viewModelFactory));
            Guard.ArgumentNotNull(apiClientFactory, nameof(apiClientFactory));
            Guard.ArgumentNotNull(connectionManager, nameof(connectionManager));
            Guard.ArgumentNotNull(teamExplorerContext, nameof(teamExplorerContext));
            Guard.ArgumentNotNull(browser, nameof(browser));
            Guard.ArgumentNotNull(usageTracker, nameof(usageTracker));
            Guard.ArgumentNotNull(navigator, nameof(navigator));
            Guard.ArgumentNotNull(loggedOut, nameof(loggedOut));
            Guard.ArgumentNotNull(notAGitHubRepository, nameof(notAGitHubRepository));
            Guard.ArgumentNotNull(notAGitRepository, nameof(notAGitRepository));

            this.viewModelFactory = viewModelFactory;
            this.apiClientFactory = apiClientFactory;
            this.connectionManager = connectionManager;
            this.teamExplorerContext = teamExplorerContext;
            this.navigator = navigator;
            this.loggedOut = loggedOut;
            this.notAGitHubRepository = notAGitHubRepository;
            this.notAGitRepository = notAGitRepository;

            var contentAndNavigatorContent = Observable.CombineLatest(
                this.WhenAnyValue(x => x.Content),
                navigator.WhenAnyValue(x => x.Content),
                (c, nc) => new { Content = c, NavigatorContent = nc });

            contentOverride = contentAndNavigatorContent
                .SelectMany(x =>
                {
                    if (x.Content == null) return Observable.Return(ContentOverride.Spinner);
                    else if (x.Content == navigator && x.NavigatorContent != null)
                    {
                        return x.NavigatorContent.WhenAnyValue(
                            y => y.IsLoading,
                            y => y.Error,
                            (l, e) =>
                            {
                                if (l) return ContentOverride.Spinner;
                                if (e != null) return ContentOverride.Error;
                                else return ContentOverride.None;
                            });
                    }
                    else return Observable.Return(ContentOverride.None);
                })
                .ToProperty(this, x => x.ContentOverride);

            // Returns navigator.Content if Content == navigator, otherwise null.
            var currentPage = contentAndNavigatorContent
                .Select(x => x.Content == navigator ? x.NavigatorContent : null);

            title = currentPage
                .SelectMany(x => x?.WhenAnyValue(y => y.Title) ?? Observable.Return<string>(null))
                .Select(x => x ?? "GitHub")
                .ToProperty(this, x => x.Title);

            isSearchEnabled = currentPage
                .Select(x => x is ISearchablePageViewModel)
                .ToProperty(this, x => x.IsSearchEnabled);

            refresh = ReactiveCommand.CreateAsyncTask(
                currentPage.SelectMany(x => x?.WhenAnyValue(
                        y => y.IsLoading,
                        y => y.IsBusy,
                        (loading, busy) => !loading && !busy)
                            ?? Observable.Return(false)),
                _ => navigator.Content.Refresh());
            refresh.ThrownExceptions.Subscribe();

            showPullRequests = ReactiveCommand.CreateAsyncTask(
                this.WhenAny(x => x.Content, x => x.Value == navigator),
                _ => ShowPullRequests());

            openInBrowser = ReactiveCommand.Create(currentPage.Select(x => x is IOpenInBrowser));
            openInBrowser.Subscribe(_ =>
            {
                var url = ((IOpenInBrowser)navigator.Content).WebUrl;
                if (url != null) browser.OpenUrl(url);
            });

            help = ReactiveCommand.Create();
            help.Subscribe(_ =>
            {
{'fraction_non_alphanumeric': 0.07996855570642464, 'fraction_numerical': 0.0002858572143214464, 'mean_word_length': 2.240852246410375, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6718319', 'n_tokens_mistral': 3469, 'n_tokens_neox': 3290, 'n_words': 668}