<commit_before>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

namespace osu_StreamCompanion.Code.Modules.MapDataParsers.Parser1
{

    public partial class MapDataParserSettings : UserControl
    {
        private readonly string _errorFilenameExists = "Pattern with that file name already exists" + Environment.NewLine + "Pattern not added";
        private readonly string _errorHorriblyWrong = "Something went horribly wrong " + Environment.NewLine + "Pattern not added";

        private BindingList<FileFormating> _patternDictionary;

        private readonly Dictionary<int, int> _statusToSelection = new Dictionary<int, int>();
        private readonly Dictionary<int, int> _selectionToStatus = new Dictionary<int, int>();

        private Dictionary<string, string> _PreviewReplacementDict;


        public MapDataParserSettings(ref BindingList<FileFormating> patternDictionary)
        {
            InitializeComponent();

            _patternDictionary = patternDictionary;
            var source = new BindingSource(_patternDictionary, null);
            dataGridView.DataSource = source;

            var _saveEvents = new Dictionary<int, string>();
            _saveEvents.Add(0, "Never");
            _saveEvents.Add(1, "Listening");
            _saveEvents.Add(2, "Playing");
            _saveEvents.Add(8, "Watching");
            _saveEvents.Add(16, "Editing");
            _saveEvents.Add(27, "All");

            _statusToSelection.Add(1, 0);
            _statusToSelection.Add(2, 1);
            _statusToSelection.Add(8, 2);
            _statusToSelection.Add(16, 3);
            _statusToSelection.Add(27, 4);

            _selectionToStatus.Add(0, 1);
            _selectionToStatus.Add(1, 2);
            _selectionToStatus.Add(2, 8);
            _selectionToStatus.Add(3, 16);
            _selectionToStatus.Add(4, 27);



            foreach (var ev in _saveEvents)
            {
                comboBox_saveEvent.Items.Add(ev.Value);
            }
        }


        private void UserError(string msg)
        {
            MessageBox.Show(msg, "Info");
        }
        private bool AddFormat(string fileName, string formating, int saveEvent, bool suppressErrors = false)
        {
            lock (_patternDictionary)
            {
                if (FileNameAlreadyExists(fileName))
                {
                    if (!suppressErrors)
                        UserError(_errorFilenameExists);
                }
                else
                {
                    _patternDictionary.Add(new FileFormating
                    {
                        Filename = fileName,
                        Pattern = formating,
                        SaveEvent = saveEvent
                    });
                    return true;
                }
            }
            return false;
        }


        private bool FileNameAlreadyExists(string filename, string excl = "")
        {
            for (int i = 0; i < _patternDictionary.Count; i++)
            {
                string currentFileName = _patternDictionary[i].Filename;
                if (string.IsNullOrEmpty(currentFileName)) continue;
                if (currentFileName != excl && currentFileName == filename)
                    return true;
            }
            return false;
        }


        private void LoadPattern(int idx)
        {
            lock (_patternDictionary)
            {
                if (idx < _patternDictionary.Count)
                {
                    textBox_FileName.Text = _patternDictionary[idx].Filename;
                    textBox_Formating.Text = _patternDictionary[idx].Pattern;
                    comboBox_saveEvent.SelectedIndex = _statusToSelection[_patternDictionary[idx].SaveEvent == 0 ? 27 : _patternDictionary[idx].SaveEvent];
                    UpdatePreview(idx);
                }
            }
        }

        private void UpdatePreview(int idx)
        {
            if (_PreviewReplacementDict != null)
                textBox_Preview.Text = FormatMapString(textBox_Formating.Text, _PreviewReplacementDict);
            else
                textBox_Preview.Text = "Change map in osu!";
        }

        private void button_AddPattern_Click(object sender, EventArgs e)
        {
            string fileName = textBox_FileName.Text;
            string formating = textBox_Formating.Text;
            int Event = _selectionToStatus[comboBox_saveEvent.SelectedIndex - 1];

            if (fileName != string.Empty && formating != string.Empty)
            {
                AddFormat(fileName, formating, Event);
            }
            else
            {
                UserError("Fill all fields first.");
            }

        }

        private void dataGridView_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView.SelectedRows.Count == 1)
            {
                LoadPattern(dataGridView.SelectedRows[0].Index);
            }
        }

        private void button_EditPattern_Click(object sender, EventArgs e)
        {
            if (dataGridView.SelectedRows.Count == 1)
            {
                string fileName = textBox_FileName.Text;
                string formating = textBox_Formating.Text;
                int Event = _selectionToStatus[comboBox_saveEvent.SelectedIndex - 1];
                int id = dataGridView.SelectedRows[0].Index;

                if (string.IsNullOrWhiteSpace(fileName) || string.IsNullOrWhiteSpace(formating))
                {
                    UserError("Fill all fields first.");
                    return;
                }
                if (FileNameAlreadyExists(fileName))
                {
                    UserError(_errorFilenameExists);
                    return;
                }
                if (id == -1)
                    UserError(_errorHorriblyWrong);
                else
                {
                    lock (_patternDictionary)
                    {
                        _patternDictionary[id] = new FileFormating
                        {
                            Filename = fileName,
                            Pattern = formating,
                            SaveEvent = Event
                        };<commit_msg>Revert "Fix save event being set incorrectly"<commit_after>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

namespace osu_StreamCompanion.Code.Modules.MapDataParsers.Parser1
{

    public partial class MapDataParserSettings : UserControl
    {
        private readonly string _errorFilenameExists = "Pattern with that file name already exists" + Environment.NewLine + "Pattern not added";
        private readonly string _errorHorriblyWrong = "Something went horribly wrong " + Environment.NewLine + "Pattern not added";

        private BindingList<FileFormating> _patternDictionary;

        private readonly Dictionary<int, int> _statusToSelection = new Dictionary<int, int>();
        private readonly Dictionary<int, int> _selectionToStatus = new Dictionary<int, int>();

        private Dictionary<string, string> _PreviewReplacementDict;


        public MapDataParserSettings(ref BindingList<FileFormating> patternDictionary)
        {
            InitializeComponent();

            _patternDictionary = patternDictionary;
            var source = new BindingSource(_patternDictionary, null);
            dataGridView.DataSource = source;

            var _saveEvents = new Dictionary<int, string>();
            _saveEvents.Add(0, "Never");
            _saveEvents.Add(1, "Listening");
            _saveEvents.Add(2, "Playing");
            _saveEvents.Add(8, "Watching");
            _saveEvents.Add(16, "Editing");
            _saveEvents.Add(27, "All");

            _statusToSelection.Add(1, 0);
            _statusToSelection.Add(2, 1);
            _statusToSelection.Add(8, 2);
            _statusToSelection.Add(16, 3);
            _statusToSelection.Add(27, 4);

            _selectionToStatus.Add(0, 1);
            _selectionToStatus.Add(1, 2);
            _selectionToStatus.Add(2, 8);
            _selectionToStatus.Add(3, 16);
            _selectionToStatus.Add(4, 27);



            foreach (var ev in _saveEvents)
            {
                comboBox_saveEvent.Items.Add(ev.Value);
            }
        }


        private void UserError(string msg)
        {
            MessageBox.Show(msg, "Info");
        }
        private bool AddFormat(string fileName, string formating, int saveEvent, bool suppressErrors = false)
        {
            lock (_patternDictionary)
            {
                if (FileNameAlreadyExists(fileName))
                {
                    if (!suppressErrors)
                        UserError(_errorFilenameExists);
                }
                else
                {
                    _patternDictionary.Add(new FileFormating
                    {
                        Filename = fileName,
                        Pattern = formating,
                        SaveEvent = saveEvent
                    });
                    return true;
                }
            }
            return false;
        }


        private bool FileNameAlreadyExists(string filename, string excl = "")
        {
            for (int i = 0; i < _patternDictionary.Count; i++)
            {
                string currentFileName = _patternDictionary[i].Filename;
                if (string.IsNullOrEmpty(currentFileName)) continue;
                if (currentFileName != excl && currentFileName == filename)
                    return true;
            }
            return false;
        }


        private void LoadPattern(int idx)
        {
            lock (_patternDictionary)
            {
                if (idx < _patternDictionary.Count)
                {
                    textBox_FileName.Text = _patternDictionary[idx].Filename;
                    textBox_Formating.Text = _patternDictionary[idx].Pattern;
                    comboBox_saveEvent.SelectedIndex = _statusToSelection[_patternDictionary[idx].SaveEvent == 0 ? 27 : _patternDictionary[idx].SaveEvent];
                    UpdatePreview(idx);
                }
            }
        }

        private void UpdatePreview(int idx)
        {
            if (_PreviewReplacementDict != null)
                textBox_Preview.Text = FormatMapString(textBox_Formating.Text, _PreviewReplacementDict);
            else
                textBox_Preview.Text = "Change map in osu!";
        }

        private void button_AddPattern_Click(object sender, EventArgs e)
        {
            string fileName = textBox_FileName.Text;
            string formating = textBox_Formating.Text;
            int Event = _selectionToStatus[comboBox_saveEvent.SelectedIndex];

            if (fileName != string.Empty && formating != string.Empty)
            {
                AddFormat(fileName, formating, Event);
            }
            else
            {
                UserError("Fill all fields first.");
            }

        }

        private void dataGridView_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView.SelectedRows.Count == 1)
            {
                LoadPattern(dataGridView.SelectedRows[0].Index);
            }
        }

        private void button_EditPattern_Click(object sender, EventArgs e)
        {
            if (dataGridView.SelectedRows.Count == 1)
            {
                string fileName = textBox_FileName.Text;
                string formating = textBox_Formating.Text;
                int Event = _selectionToStatus[comboBox_saveEvent.SelectedIndex];
                int id = dataGridView.SelectedRows[0].Index;

                if (string.IsNullOrWhiteSpace(fileName) || string.IsNullOrWhiteSpace(formating))
                {
                    UserError("Fill all fields first.");
                    return;
                }
                if (FileNameAlreadyExists(fileName))
                {
                    UserError(_errorFilenameExists);
                    return;
                }
                if (id == -1)
                    UserError(_errorHorriblyWrong);
                else
                {
                    lock (_patternDictionary)
                    {
                        _patternDictionary[id] = new FileFormating
                        {
                            Filename = fileName,
                            Pattern = formating,
                            SaveEvent = Event
                        };
{'fraction_non_alphanumeric': 0.07796125754207685, 'fraction_numerical': 0.00682756430612893, 'mean_word_length': 1.5300261096605745, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 19, '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': '379359', 'n_tokens_mistral': 3094, 'n_tokens_neox': 2880, 'n_words': 626}