<commit_before>﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Deployment.Application;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using System.Windows;

using Spark.Dialogs;
using Spark.Models;
using Spark.Models.Serializers;
using Spark.ViewModels;
using Spark.Views;

namespace Spark
{
    public partial class App : Application
    {
        public static readonly string ApplicationName = "Spark";
        public static readonly string SettingsFileName = "Settings.xml";
        public static readonly string ClientVersionsFileName = "Versions.xml";

        #region Properties
        public UserSettings CurrentSettings { get; protected set; }
        public IEnumerable<ClientVersion> ClientVersions { get; protected set; }
        #endregion

        #region Application Lifecycle
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            // Load settings and client versions from file (or defaults)
            this.CurrentSettings = LoadSettingsOrDefaults(App.SettingsFileName);
            this.ClientVersions = LoadClientVersionsOrDefaults(App.ClientVersionsFileName);

            // Initialize the main window and view model
            var window = new MainWindow();
            var dialogService = new DialogService(window);
            var viewModel = new MainViewModel(this.CurrentSettings, this.ClientVersions, dialogService);

            // Bind the request close event to closing the window
            viewModel.RequestClose += delegate
            {
                window.Close();
            };

            // Assign the view model to the data context and display the main window
            window.DataContext = viewModel;
            window.Show();
        }

        protected override void OnExit(ExitEventArgs e)
        {
            SaveUserSettings(App.SettingsFileName, this.CurrentSettings);
            SaveClientVersions(App.ClientVersionsFileName, this.ClientVersions);

            base.OnExit(e);
        }
        #endregion

        #region Save/Load User Settings
        static void SaveUserSettings(string fileName, UserSettings settings)
        {
            if (fileName == null)
                throw new ArgumentNullException("fileName");

            if (settings == null)
                throw new ArgumentNullException("settings");

            try
            {
                var xml = new XDocument(
                    new XDeclaration("1.0", "utf-8", "yes"),
                    settings.Serialize());

                // Save user settings to file
                xml.Save(fileName);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(string.Format("Unable to save user settings: {0}", ex.Message));
            }
        }

        static UserSettings LoadSettingsOrDefaults(string fileName)
        {
            if (fileName == null)
                throw new ArgumentNullException("fileName");

            try<commit_msg>Added root element to UserSettings XML<commit_after>﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Deployment.Application;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using System.Windows;

using Spark.Dialogs;
using Spark.Models;
using Spark.Models.Serializers;
using Spark.ViewModels;
using Spark.Views;

namespace Spark
{
    public partial class App : Application
    {
        public static readonly string ApplicationName = "Spark";
        public static readonly string SettingsFileName = "Settings.xml";
        public static readonly string ClientVersionsFileName = "Versions.xml";

        #region Properties
        public UserSettings CurrentSettings { get; protected set; }
        public IEnumerable<ClientVersion> ClientVersions { get; protected set; }
        #endregion

        #region Application Lifecycle
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            // Load settings and client versions from file (or defaults)
            this.CurrentSettings = LoadSettingsOrDefaults(App.SettingsFileName);
            this.ClientVersions = LoadClientVersionsOrDefaults(App.ClientVersionsFileName);

            // Initialize the main window and view model
            var window = new MainWindow();
            var dialogService = new DialogService(window);
            var viewModel = new MainViewModel(this.CurrentSettings, this.ClientVersions, dialogService);

            // Bind the request close event to closing the window
            viewModel.RequestClose += delegate
            {
                window.Close();
            };

            // Assign the view model to the data context and display the main window
            window.DataContext = viewModel;
            window.Show();
        }

        protected override void OnExit(ExitEventArgs e)
        {
            SaveUserSettings(App.SettingsFileName, this.CurrentSettings);
            SaveClientVersions(App.ClientVersionsFileName, this.ClientVersions);

            base.OnExit(e);
        }
        #endregion

        #region Save/Load User Settings
        static void SaveUserSettings(string fileName, UserSettings settings)
        {
            if (fileName == null)
                throw new ArgumentNullException("fileName");

            if (settings == null)
                throw new ArgumentNullException("settings");

            try
            {
                var xml = new XDocument(
                    new XDeclaration("1.0", "utf-8", "yes"),
                    new XElement("Settings", settings.Serialize()));

                // Save user settings to file
                xml.Save(fileName);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(string.Format("Unable to save user settings: {0}", ex.Message));
            }
        }

        static UserSettings LoadSettingsOrDefaults(string fileName)
        {
            if (fileName == null)
                throw new ArgumentNullException("fileName");

            try
{'fraction_non_alphanumeric': 0.07591914690434506, 'fraction_numerical': 0.001273277096928219, 'mean_word_length': 2.1232604373757455, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '5240343', 'n_tokens_mistral': 1471, 'n_tokens_neox': 1426, 'n_words': 432}