<commit_before>﻿using HotChocolatey.Model;
using HotChocolatey.Utility;
using HotChocolatey.ViewModel.Ginnivan;
using System;
using System.ComponentModel;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace HotChocolatey.ViewModel
{
    [Magic]
    public class MainWindowViewModel : INotifyPropertyChanged, ProgressIndication.IProgressIndicator
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public ChocolateyController Controller { get; } = new ChocolateyController();
        public Packages Packages { get; }
        public Diagnostics Diagnostics { get; } = new Diagnostics();

        public bool IsLogVisible { get; set; }
        public bool IsUserAllowedToExecuteActions { get; set; } = true;

        public PackageManagerViewModel PackageManagerViewModel { get; }

        public AwaitableDelegateCommand RefreshCommand { get; }
        public AwaitableDelegateCommand UpgradeAllCommand { get; }

        bool ProgressIndication.IProgressIndicator.IsInProgress
        {
            set
            {
                IsUserAllowedToExecuteActions = !value;
                if (!value)
                {
                    SynchronizationContext.Current.Post(async state =>
                    {
                        using (new ProgressIndication(PackageManagerViewModel))
                        {
                            await Refresh();
                        }
                    }, null);
                }
            }
        }

        public MainWindowViewModel()
        {
            Log.ResetSettings(true, true, true, Diagnostics);
            Log.Info("---");
            Log.Info($"Version:{Assembly.GetCallingAssembly().GetName().Version} MachineName:{Environment.MachineName} OSVersion:{Environment.OSVersion} Is64BitOperatingSystem:{Environment.Is64BitOperatingSystem}");

#if !DEBUG
            Application.Current.DispatcherUnhandledException += (s, e) => Log.Error("DispatcherUnhandledException: {0}", e.Exception);
#endif

            RefreshCommand = new AwaitableDelegateCommand(ExecuteRefreshCommand);
            UpgradeAllCommand = new AwaitableDelegateCommand(ExecuteUpgradeAllCommand);

            PackageManagerViewModel = new PackageManagerViewModel(this);
            Packages = new Packages(Controller);
        }

        public async Task Loaded()
        {
            PackageManagerViewModel.Packages = Packages;
            PackageManagerViewModel.Load(Controller);
            PackageManagerViewModel.Searched += OnSearched;
            PackageManagerViewModel.ScrolledToBottom += OnScrolledToBottom;

            Controller.StartGetInstalled();

            try
            {
                var result = await Controller.GetVersion();
                Log.Info($"Chocolatey version: {result}");
            }
            catch (Win32Exception ex)
            {
                Log.Error($"Choco not installed? Message: {ex.Message}");
                MessageBox.Show("Choco not installed?", "Hot Chocolatey Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
        }

        private async Task ExecuteRefreshCommand()
        {
            using (new ProgressIndication(PackageManagerViewModel))
            {
                await Refresh();
            }
        }

        private async Task ExecuteUpgradeAllCommand()
        {
            using (new ProgressIndication(this))
            {
                await Packages.ApplyFilter(FilterFactory.BuildUpgradeFilter(Controller));

                foreach (var package in Packages.Items)
                {
                    if (!await Controller.Upgrade(package))
                    {<commit_msg>Get version before the installed packages<commit_after>﻿using HotChocolatey.Model;
using HotChocolatey.Utility;
using HotChocolatey.ViewModel.Ginnivan;
using System;
using System.ComponentModel;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace HotChocolatey.ViewModel
{
    [Magic]
    public class MainWindowViewModel : INotifyPropertyChanged, ProgressIndication.IProgressIndicator
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public ChocolateyController Controller { get; } = new ChocolateyController();
        public Packages Packages { get; }
        public Diagnostics Diagnostics { get; } = new Diagnostics();

        public bool IsLogVisible { get; set; }
        public bool IsUserAllowedToExecuteActions { get; set; } = true;

        public PackageManagerViewModel PackageManagerViewModel { get; }

        public AwaitableDelegateCommand RefreshCommand { get; }
        public AwaitableDelegateCommand UpgradeAllCommand { get; }

        bool ProgressIndication.IProgressIndicator.IsInProgress
        {
            set
            {
                IsUserAllowedToExecuteActions = !value;
                if (!value)
                {
                    SynchronizationContext.Current.Post(async state =>
                    {
                        using (new ProgressIndication(PackageManagerViewModel))
                        {
                            await Refresh();
                        }
                    }, null);
                }
            }
        }

        public MainWindowViewModel()
        {
            Log.ResetSettings(true, true, true, Diagnostics);
            Log.Info("---");
            Log.Info($"Version:{Assembly.GetCallingAssembly().GetName().Version} MachineName:{Environment.MachineName} OSVersion:{Environment.OSVersion} Is64BitOperatingSystem:{Environment.Is64BitOperatingSystem}");

#if !DEBUG
            Application.Current.DispatcherUnhandledException += (s, e) => Log.Error("DispatcherUnhandledException: {0}", e.Exception);
#endif

            RefreshCommand = new AwaitableDelegateCommand(ExecuteRefreshCommand);
            UpgradeAllCommand = new AwaitableDelegateCommand(ExecuteUpgradeAllCommand);

            PackageManagerViewModel = new PackageManagerViewModel(this);
            Packages = new Packages(Controller);
        }

        public async Task Loaded()
        {
            PackageManagerViewModel.Packages = Packages;
            PackageManagerViewModel.Load(Controller);
            PackageManagerViewModel.Searched += OnSearched;
            PackageManagerViewModel.ScrolledToBottom += OnScrolledToBottom;

            try
            {
                var result = await Controller.GetVersion();
                Log.Info($"Chocolatey version: {result}");
            }
            catch (Win32Exception ex)
            {
                Log.Error($"Choco not installed? Message: {ex.Message}");
                MessageBox.Show("Choco not installed?", "Hot Chocolatey Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            Controller.StartGetInstalled();
        }

        private async Task ExecuteRefreshCommand()
        {
            using (new ProgressIndication(PackageManagerViewModel))
            {
                await Refresh();
            }
        }

        private async Task ExecuteUpgradeAllCommand()
        {
            using (new ProgressIndication(this))
            {
                await Packages.ApplyFilter(FilterFactory.BuildUpgradeFilter(Controller));

                foreach (var package in Packages.Items)
                {
                    if (!await Controller.Upgrade(package))
                    {
{'fraction_non_alphanumeric': 0.07941567065073041, 'fraction_numerical': 0.0018592297476759628, 'mean_word_length': 2.053933495539335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1682256', 'n_tokens_mistral': 1808, 'n_tokens_neox': 1715, 'n_words': 363}