<commit_before>﻿namespace T3000.Forms
{
    using PRGReaderLibrary;
    using Properties;
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Collections.Generic;

    public partial class SelectUnitsForm : Form
    {
        public Units SelectedUnits { get; private set; } = Units.Unused;
        public bool IsValidated { get; private set; } = true;
        public List<CustomDigitalUnitsPoint> CustomUnits { get; set; } = null;

        public SelectUnitsForm(Units selectedUnits = Units.Unused, List<CustomDigitalUnitsPoint> customUnits = null)
        {
            InitializeComponent();

            SelectedUnits = selectedUnits;
            CustomUnits = customUnits;
            UpdateUnits();
        }

        private int ToNumber(Units units) =>
            (int)(units + 1);

        private Units ToUnits(int units) =>
            (Units)(units - 1);

        public void UpdateUnits()
        {
            try
            {
                analogUnitsListBox.Items.Clear();
                foreach (var name in UnitsNamesConstants.GetAnalogNames(CustomUnits))
                {
                    analogUnitsListBox.Items.Add($"{ToNumber(name.Key)}. {name.Value.OffOnName}");
                }

                digitalUnitsListBox.Items.Clear();
                foreach (var name in UnitsNamesConstants.GetDigitalNames(CustomUnits))
                {
                    digitalUnitsListBox.Items.Add($"{ToNumber(name.Key)}. {name.Value.OffOnName}");
                }

                ShowSelectedItem();
            }
            catch (Exception exception)
            {
                MessageBoxUtilities.ShowException(exception);
            }
        }
        private void unitsListBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Save(sender, e);
            }
        }

        private void Save(object sender, EventArgs e)
        {
            if (!IsValidated)
            {
                MessageBoxUtilities.ShowWarning(Resources.SelectUnitsFormNotValid);
                DialogResult = DialogResult.None;
                return;
            }

            DialogResult = DialogResult.OK;
            Close();
        }

        private void Cancel(object sender, EventArgs e)
        {
            Close();
        }

        private void EditCustomUnits(object sender, EventArgs e)
        {
            var form = new EditCustomUnitsForm(CustomUnits);

            if (form.ShowDialog() == DialogResult.OK)
            {
                CustomUnits = form.CustomUnits;
                UpdateUnits();
            }
        }

        private void numberTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    Save(sender, e);
                    break;

                case Keys.W:
                case Keys.Up:
                case Keys.PageUp:
                case Keys.VolumeUp:
                    SelectedUnits = EnumUtilities.PrevValue(SelectedUnits);
                    ShowSelectedItem();
                    break;

                case Keys.S:
                case Keys.Down:
                case Keys.PageDown:
                case Keys.VolumeDown:
                    SelectedUnits = EnumUtilities.NextValue(SelectedUnits);
                    ShowSelectedItem();
                    break;
            }
        }

        public void ShowSelectedItem()
        {
            numberTextBox.Text = ToNumber(SelectedUnits).ToString();
            messageLabel.Text = string.Format(Resources.SelectUnitsFormSelectedUnits, SelectedUnits.GetOffOnName(CustomUnits));
            if (SelectedUnits.IsAnalog())
            {
                analogUnitsListBox.SelectedIndex = (int) SelectedUnits;
                digitalUnitsListBox.SelectedIndex = -1;
            }
            else
            {
                digitalUnitsListBox.SelectedIndex = SelectedUnits - Units.DigitalUnused;
                analogUnitsListBox.SelectedIndex = -1;
            }<commit_msg>Added support for Left/Right keys on SelectUnitsForm<commit_after>﻿namespace T3000.Forms
{
    using PRGReaderLibrary;
    using Properties;
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Collections.Generic;

    public partial class SelectUnitsForm : Form
    {
        public Units SelectedUnits { get; private set; } = Units.Unused;
        public bool IsValidated { get; private set; } = true;
        public List<CustomDigitalUnitsPoint> CustomUnits { get; set; } = null;

        public SelectUnitsForm(Units selectedUnits = Units.Unused, List<CustomDigitalUnitsPoint> customUnits = null)
        {
            InitializeComponent();

            SelectedUnits = selectedUnits;
            CustomUnits = customUnits;
            UpdateUnits();
        }

        private int ToNumber(Units units) =>
            (int)(units + 1);

        private Units ToUnits(int units) =>
            (Units)(units - 1);

        public void UpdateUnits()
        {
            try
            {
                analogUnitsListBox.Items.Clear();
                foreach (var name in UnitsNamesConstants.GetAnalogNames(CustomUnits))
                {
                    analogUnitsListBox.Items.Add($"{ToNumber(name.Key)}. {name.Value.OffOnName}");
                }

                digitalUnitsListBox.Items.Clear();
                foreach (var name in UnitsNamesConstants.GetDigitalNames(CustomUnits))
                {
                    digitalUnitsListBox.Items.Add($"{ToNumber(name.Key)}. {name.Value.OffOnName}");
                }

                ShowSelectedItem();
            }
            catch (Exception exception)
            {
                MessageBoxUtilities.ShowException(exception);
            }
        }
        private void unitsListBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Save(sender, e);
            }
        }

        private void Save(object sender, EventArgs e)
        {
            if (!IsValidated)
            {
                MessageBoxUtilities.ShowWarning(Resources.SelectUnitsFormNotValid);
                DialogResult = DialogResult.None;
                return;
            }

            DialogResult = DialogResult.OK;
            Close();
        }

        private void Cancel(object sender, EventArgs e)
        {
            Close();
        }

        private void EditCustomUnits(object sender, EventArgs e)
        {
            var form = new EditCustomUnitsForm(CustomUnits);

            if (form.ShowDialog() == DialogResult.OK)
            {
                CustomUnits = form.CustomUnits;
                UpdateUnits();
            }
        }

        private void numberTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            var maxItemsInColumn = analogUnitsListBox.Height / analogUnitsListBox.ItemHeight;
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    Save(sender, e);
                    break;

                case Keys.W:
                case Keys.Up:
                case Keys.PageUp:
                case Keys.VolumeUp:
                    SelectedUnits = EnumUtilities.PrevValue(SelectedUnits);
                    ShowSelectedItem();
                    break;

                case Keys.S:
                case Keys.Down:
                case Keys.PageDown:
                case Keys.VolumeDown:
                    SelectedUnits = EnumUtilities.NextValue(SelectedUnits);
                    ShowSelectedItem();
                    break;

                case Keys.Left:
                case Keys.A:
                    for (var i = 0; i < maxItemsInColumn; ++i)
                    {
                        SelectedUnits = EnumUtilities.PrevValue(SelectedUnits);
                    }
                    ShowSelectedItem();
                    break;

                case Keys.Right:
                case Keys.D:
                    for (var i = 0; i < maxItemsInColumn; ++i)
                    {
                        SelectedUnits = EnumUtilities.NextValue(SelectedUnits);
                    }
                    ShowSelectedItem();
                    break;
            }
        }

        public void ShowSelectedItem()
        {
            numberTextBox.Text = ToNumber(SelectedUnits).ToString();
            messageLabel.Text = string.Format(Resources.SelectUnitsFormSelectedUnits, SelectedUnits.GetOffOnName(CustomUnits));
            if (SelectedUnits.IsAnalog())
            {
                analogUnitsListBox.SelectedIndex = (int) SelectedUnits;
                digitalUnitsListBox.SelectedIndex = -1;
            }
            else
            {
                digitalUnitsListBox.SelectedIndex = SelectedUnits - Units.DigitalUnused;
                analogUnitsListBox.SelectedIndex = -1;
            }
{'fraction_non_alphanumeric': 0.07936156062957216, 'fraction_numerical': 0.0019951230325870096, 'mean_word_length': 1.383883751651255, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '2357004', 'n_tokens_mistral': 2256, 'n_tokens_neox': 2171, 'n_words': 432}