<commit_before>﻿using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using IdleRGB.Properties;
using System.Windows.Media;
using System.Security.Permissions;
using CUE.NET;
using CUE.NET.Devices.Generic;
using System.Windows.Input;

namespace IdleRGB
{
    /// <summary>
    ///     Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class SettingsWindow : ToolWindow
    {
        bool autoStart;
        Color newInactiveColor;
        Color newCapsColor;


        /// <summary>
        ///     Initializes a new instance of the <see cref="SettingsWindow" /> class.
        /// </summary>
        public SettingsWindow()
        {
            InitializeComponent();

            InitializeComboBox(hoursComboBox, 23);
            InitializeComboBox(minutesComboBox, 59);
            InitializeComboBox(secondsComboBox, 59);

            LoadSettings();
        }

        /// <summary>
        ///     Changes checkbox values to current idleTime.
        /// </summary>
        private void LoadSettings()
        {
            var it = Settings.Default.idleTime;

            hoursComboBox.SelectedItem = it.Hours;
            minutesComboBox.SelectedItem = it.Minutes;
            secondsComboBox.SelectedItem = it.Seconds;

            Color color = Color.FromRgb(Settings.Default.idleColor.R, Settings.Default.idleColor.G, Settings.Default.idleColor.B);
            inactiveRectangle.Fill = new SolidColorBrush(color);

            color = Color.FromRgb(Settings.Default.capsColor.R, Settings.Default.capsColor.G, Settings.Default.capsColor.B);
            capsLockRectangle.Fill = new SolidColorBrush(color);

            InitializeAutoStartCheckbox();
        }

        /// <summary>
        ///     Initializes ComboBox.
        /// </summary>
        /// <param name="comboBox"></param>
        /// <param name="upperLimit"></param>
        private void InitializeComboBox(ComboBox comboBox, int upperLimit)
        {
            for (var i = 0; i <= upperLimit; i++)
                comboBox.Items.Add(i);
        }

        /// <summary>
        ///     Saves settings.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            TimeSpan newTime = GetNewTime();

            if (Settings.Default.idleTime != newTime)
                Settings.Default.idleTime = newTime;
                Settings.Default.Save();

            if (autostartCheckbox.IsChecked != autoStart)
            {
                try
                {
                    autoStart = (bool)autostartCheckbox.IsChecked;

                    Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

                    if (autoStart)
                        key.SetValue(Process.GetCurrentProcess().ProcessName, Process.GetCurrentProcess().MainModule.FileName);
                    else
                        key.DeleteValue(Process.GetCurrentProcess().ProcessName, false);
                }

                catch(Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

            Color color = Color.FromRgb(Settings.Default.idleColor.R, Settings.Default.idleColor.R, Settings.Default.idleColor.B);

            if(color != newInactiveColor)
                Settings.Default.idleColor = System.Drawing.Color.FromArgb(newInactiveColor.R, newInactiveColor.G, newInactiveColor.G);

            if(color != newCapsColor)
                Settings.Default.capsColor = System.Drawing.Color.FromArgb(newCapsColor.R, newCapsColor.G, newCapsColor.G);

            Settings.Default.Save();
            Close();
        }


        /// <summary>
        ///     Closes window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        /// <summary>
        ///     Removes autoStartCheckbox if registry is unavailable. Otherwise sets appropriate check value.
        /// </summary>
        private void InitializeAutoStartCheckbox()
        {
            try
            {
                RegistryPermission perm1 = new RegistryPermission(RegistryPermissionAccess.Write, @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                perm1.Demand();
                Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);<commit_msg>Bug fix<commit_after>﻿using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using IdleRGB.Properties;
using System.Windows.Media;
using System.Security.Permissions;
using CUE.NET;
using CUE.NET.Devices.Generic;
using System.Windows.Input;

namespace IdleRGB
{
    /// <summary>
    ///     Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class SettingsWindow : ToolWindow
    {
        bool autoStart;
        Color newInactiveColor;
        Color newCapsColor;


        /// <summary>
        ///     Initializes a new instance of the <see cref="SettingsWindow" /> class.
        /// </summary>
        public SettingsWindow()
        {
            InitializeComponent();

            InitializeComboBox(hoursComboBox, 23);
            InitializeComboBox(minutesComboBox, 59);
            InitializeComboBox(secondsComboBox, 59);

            LoadSettings();
        }

        /// <summary>
        ///     Changes checkbox values to current idleTime.
        /// </summary>
        private void LoadSettings()
        {
            var it = Settings.Default.idleTime;

            hoursComboBox.SelectedItem = it.Hours;
            minutesComboBox.SelectedItem = it.Minutes;
            secondsComboBox.SelectedItem = it.Seconds;

            Color color = Color.FromRgb(Settings.Default.idleColor.R, Settings.Default.idleColor.G, Settings.Default.idleColor.B);
            inactiveRectangle.Fill = new SolidColorBrush(color);

            color = Color.FromRgb(Settings.Default.capsColor.R, Settings.Default.capsColor.G, Settings.Default.capsColor.B);
            capsLockRectangle.Fill = new SolidColorBrush(color);

            InitializeAutoStartCheckbox();
        }

        /// <summary>
        ///     Initializes ComboBox.
        /// </summary>
        /// <param name="comboBox"></param>
        /// <param name="upperLimit"></param>
        private void InitializeComboBox(ComboBox comboBox, int upperLimit)
        {
            for (var i = 0; i <= upperLimit; i++)
                comboBox.Items.Add(i);
        }

        /// <summary>
        ///     Saves settings.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            TimeSpan newTime = GetNewTime();

            if (Settings.Default.idleTime != newTime)
                Settings.Default.idleTime = newTime;
                Settings.Default.Save();

            if (autostartCheckbox.IsChecked != autoStart)
            {
                try
                {
                    autoStart = (bool)autostartCheckbox.IsChecked;

                    Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

                    if (autoStart)
                        key.SetValue(Process.GetCurrentProcess().ProcessName, Process.GetCurrentProcess().MainModule.FileName);
                    else
                        key.DeleteValue(Process.GetCurrentProcess().ProcessName, false);
                }

                catch(Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

            Color color = Color.FromRgb(Settings.Default.idleColor.R, Settings.Default.idleColor.R, Settings.Default.idleColor.B);

            if(color != newInactiveColor)
                Settings.Default.idleColor = System.Drawing.Color.FromArgb(newInactiveColor.R, newInactiveColor.G, newInactiveColor.G);

            if(color != newCapsColor)
                Settings.Default.capsColor = System.Drawing.Color.FromArgb(newCapsColor.R, newCapsColor.G, newCapsColor.G);

            Settings.Default.Save();
            Close();
        }


        /// <summary>
        ///     Closes window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            var capsToggled = Keyboard.IsKeyToggled(Key.CapsLock);
            if (capsToggled)
                LedChanger.ChangeLeds(Settings.Default.capsColor);
            else
                LedChanger.ResetLeds();
            Close();
        }

        /// <summary>
        ///     Removes autoStartCheckbox if registry is unavailable. Otherwise sets appropriate check value.
        /// </summary>
        private void InitializeAutoStartCheckbox()
        {
            try
            {
                RegistryPermission perm1 = new RegistryPermission(RegistryPermissionAccess.Write, @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                perm1.Demand();
                Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
{'fraction_non_alphanumeric': 0.11018883585806184, 'fraction_numerical': 0.0035277028429134675, 'mean_word_length': 1.8375036797173978, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 57, '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': '1659511', 'n_tokens_mistral': 2547, 'n_tokens_neox': 2435, 'n_words': 452}