<commit_before>﻿using KAGTools.Helpers;
using KAGTools.Services;
using KAGTools.ViewModels;
using KAGTools.Windows;
using Microsoft.WindowsAPICodePack.Dialogs;
using Squirrel;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace KAGTools
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private const ShortcutLocation ShortcutLocations = ShortcutLocation.StartMenu | ShortcutLocation.Desktop;
        private static KAGTools.Properties.Settings Settings = KAGTools.Properties.Settings.Default;

        private void Application_Exit(object sender, ExitEventArgs e)
        {
            Settings.Save();
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
#if !DEBUG
            using (UpdateManager mgr = new UpdateManager(""))
            {
                SquirrelAwareApp.HandleEvents(
                    onFirstRun: OnFirstRun,
                    onInitialInstall: (v) => OnAppInitialInstall(v, mgr),
                    onAppUpdate: (v) => OnAppUpdate(v, mgr),
                    onAppUninstall: (v) => OnAppUninstall(v, mgr));
            }
#endif

            if (!UpdateHelper.RestoreSettings())
            {
                MessageBoxHelper.Error("There was an error restoring settings.");
            }

            /*if (Settings.UpgradeRequired)
            {
                Settings.Upgrade();
                Settings.UpgradeRequired = false;
                Settings.Save();
            }*/

            if (!Directory.Exists(Settings.KagDirectory))
            {
                // KAG Directory not specified or doesn't exist. Show dialog...
                if (!FindKagDirectory())
                {
                    Application.Current.Shutdown();
                    return;
                }
            }

            IViewService viewService = ServiceManager.RegisterService<IViewService>(new ViewService());
            viewService.RegisterView(typeof(MainWindow), typeof(MainViewModel));
            viewService.RegisterView(typeof(ModsWindow), typeof(ModsViewModel));
            viewService.RegisterView(typeof(InputWindow), typeof(InputViewModel));
            viewService.RegisterView(typeof(ManualWindow), typeof(ManualViewModel));
            viewService.RegisterView(typeof(ApiWindow), typeof(ApiViewModel));

            ServiceManager.GetService<IViewService>().OpenWindow(new MainViewModel());
        }

        #region Squirrel Events
        private static void OnFirstRun()
        {
            System.Diagnostics.Debug.WriteLine("OnFirstRun");
            MessageBoxHelper.Info("Install successful.");
        }

        private static void OnAppInitialInstall(Version version, UpdateManager mgr)
        {
            System.Diagnostics.Debug.WriteLine("OnAppInitialInstall");
            mgr.CreateShortcutsForExecutable(AssemblyHelper.AppFileName, ShortcutLocations, false);
            mgr.CreateUninstallerRegistryEntry();
        }

        private static void OnAppUpdate(Version version, UpdateManager mgr)
        {
            System.Diagnostics.Debug.WriteLine("OnAppUpdate");
            mgr.CreateShortcutsForExecutable(AssemblyHelper.AppFileName, ShortcutLocations, true);
            mgr.CreateUninstallerRegistryEntry();

            MessageBoxHelper.Info(string.Format("New version (v{0}) installed.", AssemblyHelper.FileVersionInfo.ProductVersion));
        }

        private static void OnAppUninstall(Version version, UpdateManager mgr)
        {
            System.Diagnostics.Debug.WriteLine("OnAppUninstall");
            mgr.RemoveShortcutsForExecutable(AssemblyHelper.AppFileName, ShortcutLocations);<commit_msg>Remove unused logging and add comments to Squirrel events<commit_after>﻿using KAGTools.Helpers;
using KAGTools.Services;
using KAGTools.ViewModels;
using KAGTools.Windows;
using Microsoft.WindowsAPICodePack.Dialogs;
using Squirrel;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace KAGTools
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private const ShortcutLocation ShortcutLocations = ShortcutLocation.StartMenu | ShortcutLocation.Desktop;
        private static KAGTools.Properties.Settings Settings = KAGTools.Properties.Settings.Default;

        private void Application_Exit(object sender, ExitEventArgs e)
        {
            Settings.Save();
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
#if !DEBUG
            using (UpdateManager mgr = new UpdateManager(""))
            {
                SquirrelAwareApp.HandleEvents(
                    onFirstRun: OnFirstRun,
                    onInitialInstall: (v) => OnAppInitialInstall(v, mgr),
                    onAppUpdate: (v) => OnAppUpdate(v, mgr),
                    onAppUninstall: (v) => OnAppUninstall(v, mgr));
            }
#endif

            if (!UpdateHelper.RestoreSettings())
            {
                MessageBoxHelper.Error("There was an error restoring settings.");
            }

            /*if (Settings.UpgradeRequired)
            {
                Settings.Upgrade();
                Settings.UpgradeRequired = false;
                Settings.Save();
            }*/

            if (!Directory.Exists(Settings.KagDirectory))
            {
                // KAG Directory not specified or doesn't exist. Show dialog...
                if (!FindKagDirectory())
                {
                    Application.Current.Shutdown();
                    return;
                }
            }

            IViewService viewService = ServiceManager.RegisterService<IViewService>(new ViewService());
            viewService.RegisterView(typeof(MainWindow), typeof(MainViewModel));
            viewService.RegisterView(typeof(ModsWindow), typeof(ModsViewModel));
            viewService.RegisterView(typeof(InputWindow), typeof(InputViewModel));
            viewService.RegisterView(typeof(ManualWindow), typeof(ManualViewModel));
            viewService.RegisterView(typeof(ApiWindow), typeof(ApiViewModel));

            ServiceManager.GetService<IViewService>().OpenWindow(new MainViewModel());
        }

        #region Squirrel Events
        // Runs after install
        private static void OnFirstRun()
        {
            MessageBoxHelper.Info("Install successful.");
        }

        // Runs before app launch
        private static void OnAppInitialInstall(Version version, UpdateManager mgr)
        {
            mgr.CreateShortcutsForExecutable(AssemblyHelper.AppFileName, ShortcutLocations, false);
            mgr.CreateUninstallerRegistryEntry();
        }

        // Runs before app launch
        private static void OnAppUpdate(Version version, UpdateManager mgr)
        {
            mgr.CreateShortcutsForExecutable(AssemblyHelper.AppFileName, ShortcutLocations, true);
            mgr.CreateUninstallerRegistryEntry();

            MessageBoxHelper.Info(string.Format("New version (v{0}) installed.", AssemblyHelper.FileVersionInfo.ProductVersion));
        }
        
        private static void OnAppUninstall(Version version, UpdateManager mgr)
        {
            mgr.RemoveShortcutsForExecutable(AssemblyHelper.AppFileName, ShortcutLocations);
{'fraction_non_alphanumeric': 0.08937960042060988, 'fraction_numerical': 0.0002628811777076761, 'mean_word_length': 2.3534596738651388, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, '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': '192592', 'n_tokens_mistral': 1901, 'n_tokens_neox': 1827, 'n_words': 378}