<commit_before>﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WinCommandPalette.Plugin.CommandBase;
using WinCommandPalette.Plugin.CreateCommand;
using WinCommandPalette.PluginSystem;

namespace WinCommandPalette.ViewModels.Options
{
    public class ManageCommandViewModel : ViewModelBase
    {
        private Config config;

        public ObservableCollection<ICommandBase> Commands
        {
            get => new ObservableCollection<ICommandBase>(this.config.Commands);

            set
            {
                this.config.Commands = value.ToList();
                this.NotifyPropertyChanged(nameof(this.Commands));
            }
        }

        private int selectedIndex = -1;

        public int SelectedIndex
        {
            get => this.selectedIndex;

            set
            {
                this.selectedIndex = value;
                this.NotifyPropertyChanged(nameof(this.SelectedIndex));
                this.NotifyPropertyChanged(nameof(this.SelectedItem));
                this.NotifyPropertyChanged(nameof(this.CanManage));
            }
        }

        public bool CanManage => this.SelectedIndex > -1;

        private ICommandBase selectedItem;

        public ICommandBase SelectedItem
        {
            get => this.selectedItem;

            set
            {
                this.selectedItem = value;
                if (this.SelectedItem != null)
                {
                    this.SelectedPlugin = PluginHelper.GetPlugins().Where(p => p.Commands.ContainsKey(this.SelectedItem.GetType().FullName)).FirstOrDefault();
                }

                this.NotifyPropertyChanged(nameof(this.SelectedItem));
            }
        }

        private ICreateCommand commandCreator;

        public ICreateCommand CommandCreator
        {
            get => this.commandCreator;

            set
            {
                this.commandCreator = value;
                if (this.CommandCreator != null)
                {
                    this.CommandCreator.ShowCommand(this.SelectedItem);
                }
                this.NotifyPropertyChanged(nameof(this.CommandCreator));
            }
        }

        private PluginSystem.Plugin selectedPlugin;

        public PluginSystem.Plugin SelectedPlugin
        {
            get => this.selectedPlugin;

            set
            {
                this.selectedPlugin = value;
                if (this.SelectedPlugin.Assembly != null)
                {
                    this.CommandCreator = this.SelectedPlugin.Commands[this.SelectedItem.GetType().FullName];
                }
                this.NotifyPropertyChanged(nameof(this.SelectedPlugin));
            }
        }

        public ManageCommandViewModel(Config config)
        {
            this.config = config ??
                throw new ArgumentNullException(nameof(config));
        }

        internal void Refresh()
        {
            this.NotifyPropertyChanged(nameof(this.Commands));
            if (this.SelectedIndex == -1 && this.Commands.Count > 0)
            {
                this.SelectedIndex = 0;
            }
        }

        internal void SaveChanges(object sender, RoutedEventArgs e)
        {
            var command = this.CommandCreator?.GetCommand();
            if (command != null)
            {
                var commandsCopy = this.Commands;
                var index = commandsCopy.IndexOf(this.SelectedItem);
                commandsCopy[index] = command;

                this.Commands = commandsCopy;
                this.selectedIndex = index;

                MessageBox.Show($"Command '{command.Name}' updated!", "WinCommand Palette - Options", MessageBoxButton.OK, MessageBoxImage.None);
            }
        }

        internal void Reset(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Are you sure you want reset all changes to this command?", "WinCommand Palette - Options", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                this.CommandCreator?.ShowCommand(this.SelectedItem);
            }
        }

        internal void Delete(object sender, RoutedEventArgs e)
        {
            var commandsCopy = this.Commands;
            var index = commandsCopy.IndexOf(this.SelectedItem);
            commandsCopy.RemoveAt(index);
            this.Commands = commandsCopy;

            if (commandsCopy.Count > 0)
            {
                this.SelectedIndex = --index;
            }
        }
    }
}<commit_msg>Ask before delete<commit_after>﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WinCommandPalette.Plugin.CommandBase;
using WinCommandPalette.Plugin.CreateCommand;
using WinCommandPalette.PluginSystem;

namespace WinCommandPalette.ViewModels.Options
{
    public class ManageCommandViewModel : ViewModelBase
    {
        private Config config;

        public ObservableCollection<ICommandBase> Commands
        {
            get => new ObservableCollection<ICommandBase>(this.config.Commands);

            set
            {
                this.config.Commands = value.ToList();
                this.NotifyPropertyChanged(nameof(this.Commands));
            }
        }

        private int selectedIndex = -1;

        public int SelectedIndex
        {
            get => this.selectedIndex;

            set
            {
                this.selectedIndex = value;
                this.NotifyPropertyChanged(nameof(this.SelectedIndex));
                this.NotifyPropertyChanged(nameof(this.SelectedItem));
                this.NotifyPropertyChanged(nameof(this.CanManage));
            }
        }

        public bool CanManage => this.SelectedIndex > -1;

        private ICommandBase selectedItem;

        public ICommandBase SelectedItem
        {
            get => this.selectedItem;

            set
            {
                this.selectedItem = value;
                if (this.SelectedItem != null)
                {
                    this.SelectedPlugin = PluginHelper.GetPlugins().Where(p => p.Commands.ContainsKey(this.SelectedItem.GetType().FullName)).FirstOrDefault();
                }

                this.NotifyPropertyChanged(nameof(this.SelectedItem));
            }
        }

        private ICreateCommand commandCreator;

        public ICreateCommand CommandCreator
        {
            get => this.commandCreator;

            set
            {
                this.commandCreator = value;
                if (this.CommandCreator != null)
                {
                    this.CommandCreator.ShowCommand(this.SelectedItem);
                }
                this.NotifyPropertyChanged(nameof(this.CommandCreator));
            }
        }

        private PluginSystem.Plugin selectedPlugin;

        public PluginSystem.Plugin SelectedPlugin
        {
            get => this.selectedPlugin;

            set
            {
                this.selectedPlugin = value;
                if (this.SelectedPlugin.Assembly != null)
                {
                    this.CommandCreator = this.SelectedPlugin.Commands[this.SelectedItem.GetType().FullName];
                }
                this.NotifyPropertyChanged(nameof(this.SelectedPlugin));
            }
        }

        public ManageCommandViewModel(Config config)
        {
            this.config = config ??
                throw new ArgumentNullException(nameof(config));
        }

        internal void Refresh()
        {
            this.NotifyPropertyChanged(nameof(this.Commands));
            if (this.SelectedIndex == -1 && this.Commands.Count > 0)
            {
                this.SelectedIndex = 0;
            }
        }

        internal void SaveChanges(object sender, RoutedEventArgs e)
        {
            var command = this.CommandCreator?.GetCommand();
            if (command != null)
            {
                var commandsCopy = this.Commands;
                var index = commandsCopy.IndexOf(this.SelectedItem);
                commandsCopy[index] = command;

                this.Commands = commandsCopy;
                this.selectedIndex = index;

                MessageBox.Show($"Command '{command.Name}' updated!", "WinCommand Palette - Options", MessageBoxButton.OK, MessageBoxImage.None);
            }
        }

        internal void Reset(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Are you sure you want reset all changes to this command?", "WinCommand Palette - Options", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                this.CommandCreator?.ShowCommand(this.SelectedItem);
            }
        }

        internal void Delete(object sender, RoutedEventArgs e)
        {

            if (MessageBox.Show($"Are you sure you want to delete '{this.SelectedItem?.Name}'?", "WinCommand Palette - Options", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No)
            {
                return;
            }

            var commandsCopy = this.Commands;
            var index = commandsCopy.IndexOf(this.SelectedItem);
            commandsCopy.RemoveAt(index);
            this.Commands = commandsCopy;

            if (commandsCopy.Count > 0)
            {
                this.SelectedIndex = --index;
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.08457452348713676, 'fraction_numerical': 0.001249869805228622, 'mean_word_length': 1.6791294642857142, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 27, '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': '1790924', 'n_tokens_mistral': 2380, 'n_tokens_neox': 2257, 'n_words': 430}