<commit_before>﻿// Attributions for icons used in this dialog box:
//
// Exclude User Confirmed Icon:
// Artist: Double-J Design (Available for custom work)
// Iconset: Origami Colored Pencil Icons (160 icons)
// License: CC Attribution 4.0

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using DesktopAnalytics;
using Glyssen.Character;
using Glyssen.Controls;
using Glyssen.Properties;
using Glyssen.Utilities;
using L10NSharp;
using L10NSharp.UI;
using Paratext;
using SIL.Extensions;
using SIL.Reporting;
using static System.String;

namespace Glyssen.Dialogs
{
	public partial class AssignCharacterDlg : FormWithPersistedSettings, IMessageFilter
	{
		private readonly AssignCharacterViewModel m_viewModel;
		private string m_xOfYFmt;
		private string m_singleVoiceCheckboxFmt;
		private bool m_promptToCloseWhenAssignmentsAreComplete = true;
		int m_characterListHoveredIndex = -1;
		private readonly ToolTip m_characterListToolTip = new ToolTip();
		private bool m_formLoading;
		private readonly FontProxy m_originalDefaultFontForLists;
		private readonly FontProxy m_originalDefaultFontForCharacterAndDeliveryColumns;
		private Font m_primaryReferenceTextFont;
		private Font m_englishReferenceTextFont;
		private bool m_userMadeChangesToReferenceTextMatchup;
		private readonly string m_defaultBlocksViewerText;
		private int m_IndexOfFirstFilterItemRemoved;
		private object[] m_filterItemsForRainbowModeOnly;

		private void HandleStringsLocalized()
		{
			m_viewModel.SetUiStrings(
				LocalizationManager.GetString("DialogBoxes.AssignCharacterDlg.Narrator", "narrator ({0})"),
				LocalizationManager.GetString("DialogBoxes.AssignCharacterDlg.BookChapterCharacter", "book title or chapter ({0})"),
				LocalizationManager.GetString("DialogBoxes.AssignCharacterDlg.IntroCharacter", "introduction ({0})"),
				LocalizationManager.GetString("DialogBoxes.AssignCharacterDlg.ExtraCharacter", "section head ({0})"),
				LocalizationManager.GetString("DialogBoxes.AssignCharacterDlg.NormalDelivery", "normal"));

			if (m_toolStripComboBoxFilter.Items.Count == m_IndexOfFirstFilterItemRemoved)
			{
				Debug.Assert(m_filterItemsForRainbowModeOnly != null);
				m_toolStripComboBoxFilter.Items.AddRange(m_filterItemsForRainbowModeOnly);
			}
			L10N.LocalizeComboList(m_toolStripComboBoxFilter, "DialogBoxes.AssignCharacterDlg.FilterOptions");
			UpdateFilterItems();

			m_xOfYFmt = m_labelXofY.Text;
			m_singleVoiceCheckboxFmt = m_chkSingleVoice.Text;

			Text = Format(Text, m_viewModel.ProjectName);
		}

		public AssignCharacterDlg(AssignCharacterViewModel viewModel)
		{
			InitializeComponent();

			m_defaultBlocksViewerText = m_blocksViewer.Text;

			const int numberOfFilterItemsForRainbowModeOnly = 1;
			m_IndexOfFirstFilterItemRemoved = m_toolStripComboBoxFilter.Items.Count - numberOfFilterItemsForRainbowModeOnly;
			m_filterItemsForRainbowModeOnly = new object[numberOfFilterItemsForRainbowModeOnly];
			for (int i = 0; i < numberOfFilterItemsForRainbowModeOnly; i++)
				m_filterItemsForRainbowModeOnly[i] = m_toolStripComboBoxFilter.Items[m_IndexOfFirstFilterItemRemoved];

			m_viewModel = viewModel;
			if (m_viewModel.CanDisplayReferenceTextForCurrentBlock)
			{
				// We want CheckChanged event to fire, so just setting Checked to true is not enough.
				m_toolStripButtonMatchReferenceText.CheckState = (Settings.Default.AssignCharactersMatchReferenceText ||
				                                                  (m_viewModel.Mode & BlocksToDisplay.NotAlignedToReferenceText) !=
				                                                  0)
					? CheckState.Checked
					: CheckState.Unchecked;
				HandleCharacterSelectionTabIndexChanged(m_tabControlCharacterSelection, new EventArgs());
			}
			else
			{
				Debug.Assert(!m_toolStripButtonMatchReferenceText.Checked);
				m_toolStripButtonMatchReferenceText.Enabled = false;
			}

			m_tabControlCharacterSelection.ItemSize = new Size(0, 1);
			m_tabControlCharacterSelection.Location = new Point(m_tabControlCharacterSelection.Location.X, -1);

			m_txtCharacterFilter.CorrectHeight();
			m_txtDeliveryFilter.CorrectHeight();
			if (Settings.Default.AssignCharactersShowGridView ||
			    m_viewModel.BlockGroupingStyle == BlockGroupingType.BlockCorrelation)
				m_toolStripButtonGridView.Checked = true;

			var books = new BookSet();
			foreach (var bookId in m_viewModel.IncludedBooks)
				books.Add(bookId);
			m_scriptureReference.VerseControl.BooksPresentSet = books;
			m_scriptureReference.VerseControl.ShowEmptyBooks = false;

			m_scriptureReference.VerseControl.AllowVerseSegments = false;
			m_scriptureReference.VerseControl.Versification = m_viewModel.Versification;
			m_scriptureReference.VerseControl.VerseRefChanged += m_scriptureReference_VerseRefChanged;
			m_scriptureReference.VerseControl.Disposed += (sender, args) =>
				m_scriptureReference.VerseControl.VerseRefChanged -= m_scriptureReference_VerseRefChanged;

			m_blocksViewer.Initialize(m_viewModel,
				AssignCharacterViewModel.Character.GetCharacterIdForUi,
				block => block.Delivery);
			m_viewModel.CurrentBlockChanged += LoadBlock;
			m_viewModel.CurrentBlockMatchupChanged += LoadBlockMatchup;

			UpdateProgressBarForMode();

			m_dataGridReferenceText.DataError += HandleDataGridViewDataError;
			colPrimary.HeaderText = m_viewModel.PrimaryReferenceTextName;

			HandleStringsLocalized();
			LocalizeItemDlg.StringsLocalized += HandleStringsLocalized;

			colCharacter.DisplayMember = m_listBoxCharacters.DisplayMember = "LocalizedDisplay";
			colDelivery.DisplayMember = m_listBoxDeliveries.DisplayMember = "LocalizedDisplay";
			m_originalDefaultFontForLists = new FontProxy(m_listBoxCharacters.Font);
			m_originalDefaultFontForCharacterAndDeliveryColumns = new FontProxy(m_dataGridReferenceText.DefaultCellStyle.Font);
			SetFontsFromViewModel(this, null);

			m_viewModel.AssignedBlocksIncremented += m_viewModel_AssignedBlocksIncremented;
			m_viewModel.UiFontSizeChanged += SetFontsFromViewModel;

			m_blocksViewer.VisibleChanged += BlocksViewerVisibleChanged;

			SetFilterControlsFromMode();

			m_viewModel.CurrentBookSaved += UpdateSavedText;
			m_viewModel.FilterReset += HandleFilterReset;
<commit_msg>PG-900 The constructor saved m_defaultBlocksViewerText after it had already been changed when initializing m_blocksViewer.Text when initially in 'rainbow' mode. Moved the initialization of m_defaultBlocksViewerText to HandleStringsLocalized().  m_blocksViewer.Text is still not localized, but couldn't get it to work.  "Who speaks this part?" doesn't localize.<commit_after>﻿// Attributions for icons used in this dialog box:
//
// Exclude User Confirmed Icon:
// Artist: Double-J Design (Available for custom work)
// Iconset: Origami Colored Pencil Icons (160 icons)
// License: CC Attribution 4.0

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using DesktopAnalytics;
using Glyssen.Character;
using Glyssen.Controls;
using Glyssen.Properties;
using Glyssen.Utilities;
using L10NSharp;
using L10NSharp.UI;
using Paratext;
using SIL.Extensions;
using SIL.Reporting;
using static System.String;

namespace Glyssen.Dialogs
{
	public partial class AssignCharacterDlg : FormWithPersistedSettings, IMessageFilter
	{
		private readonly AssignCharacterViewModel m_viewModel;
		private string m_xOfYFmt;
		private string m_singleVoiceCheckboxFmt;
		private bool m_promptToCloseWhenAssignmentsAreComplete = true;
		int m_characterListHoveredIndex = -1;
		private readonly ToolTip m_characterListToolTip = new ToolTip();
		private bool m_formLoading;
		private readonly FontProxy m_originalDefaultFontForLists;
		private readonly FontProxy m_originalDefaultFontForCharacterAndDeliveryColumns;
		private Font m_primaryReferenceTextFont;
		private Font m_englishReferenceTextFont;
		private bool m_userMadeChangesToReferenceTextMatchup;
		private string m_defaultBlocksViewerText;
		private int m_IndexOfFirstFilterItemRemoved;
		private object[] m_filterItemsForRainbowModeOnly;

		private void HandleStringsLocalized()
		{
			m_viewModel.SetUiStrings(
				LocalizationManager.GetString("DialogBoxes.AssignCharacterDlg.Narrator", "narrator ({0})"),
				LocalizationManager.GetString("DialogBoxes.AssignCharacterDlg.BookChapterCharacter", "book title or chapter ({0})"),
				LocalizationManager.GetString("DialogBoxes.AssignCharacterDlg.IntroCharacter", "introduction ({0})"),
				LocalizationManager.GetString("DialogBoxes.AssignCharacterDlg.ExtraCharacter", "section head ({0})"),
				LocalizationManager.GetString("DialogBoxes.AssignCharacterDlg.NormalDelivery", "normal"));

			if (m_toolStripComboBoxFilter.Items.Count == m_IndexOfFirstFilterItemRemoved)
			{
				Debug.Assert(m_filterItemsForRainbowModeOnly != null);
				m_toolStripComboBoxFilter.Items.AddRange(m_filterItemsForRainbowModeOnly);
			}
			L10N.LocalizeComboList(m_toolStripComboBoxFilter, "DialogBoxes.AssignCharacterDlg.FilterOptions");
			UpdateFilterItems();

			m_defaultBlocksViewerText = m_blocksViewer.Text;
			m_xOfYFmt = m_labelXofY.Text;
			m_singleVoiceCheckboxFmt = m_chkSingleVoice.Text;

			Text = Format(Text, m_viewModel.ProjectName);
		}

		public AssignCharacterDlg(AssignCharacterViewModel viewModel)
		{
			InitializeComponent();

			const int numberOfFilterItemsForRainbowModeOnly = 1;
			m_IndexOfFirstFilterItemRemoved = m_toolStripComboBoxFilter.Items.Count - numberOfFilterItemsForRainbowModeOnly;
			m_filterItemsForRainbowModeOnly = new object[numberOfFilterItemsForRainbowModeOnly];
			for (int i = 0; i < numberOfFilterItemsForRainbowModeOnly; i++)
				m_filterItemsForRainbowModeOnly[i] = m_toolStripComboBoxFilter.Items[m_IndexOfFirstFilterItemRemoved];

			m_viewModel = viewModel;

			HandleStringsLocalized();
			LocalizeItemDlg.StringsLocalized += HandleStringsLocalized;

			if (m_viewModel.CanDisplayReferenceTextForCurrentBlock)
			{
				// We want CheckChanged event to fire, so just setting Checked to true is not enough.
				m_toolStripButtonMatchReferenceText.CheckState = (Settings.Default.AssignCharactersMatchReferenceText ||
				                                                  (m_viewModel.Mode & BlocksToDisplay.NotAlignedToReferenceText) !=
				                                                  0)
					? CheckState.Checked
					: CheckState.Unchecked;
				HandleCharacterSelectionTabIndexChanged(m_tabControlCharacterSelection, new EventArgs());
			}
			else
			{
				Debug.Assert(!m_toolStripButtonMatchReferenceText.Checked);
				m_toolStripButtonMatchReferenceText.Enabled = false;
			}

			m_tabControlCharacterSelection.ItemSize = new Size(0, 1);
			m_tabControlCharacterSelection.Location = new Point(m_tabControlCharacterSelection.Location.X, -1);

			m_txtCharacterFilter.CorrectHeight();
			m_txtDeliveryFilter.CorrectHeight();
			if (Settings.Default.AssignCharactersShowGridView ||
			    m_viewModel.BlockGroupingStyle == BlockGroupingType.BlockCorrelation)
				m_toolStripButtonGridView.Checked = true;

			var books = new BookSet();
			foreach (var bookId in m_viewModel.IncludedBooks)
				books.Add(bookId);
			m_scriptureReference.VerseControl.BooksPresentSet = books;
			m_scriptureReference.VerseControl.ShowEmptyBooks = false;

			m_scriptureReference.VerseControl.AllowVerseSegments = false;
			m_scriptureReference.VerseControl.Versification = m_viewModel.Versification;
			m_scriptureReference.VerseControl.VerseRefChanged += m_scriptureReference_VerseRefChanged;
			m_scriptureReference.VerseControl.Disposed += (sender, args) =>
				m_scriptureReference.VerseControl.VerseRefChanged -= m_scriptureReference_VerseRefChanged;

			m_blocksViewer.Initialize(m_viewModel,
				AssignCharacterViewModel.Character.GetCharacterIdForUi,
				block => block.Delivery);
			m_viewModel.CurrentBlockChanged += LoadBlock;
			m_viewModel.CurrentBlockMatchupChanged += LoadBlockMatchup;

			UpdateProgressBarForMode();

			m_dataGridReferenceText.DataError += HandleDataGridViewDataError;
			colPrimary.HeaderText = m_viewModel.PrimaryReferenceTextName;

			colCharacter.DisplayMember = m_listBoxCharacters.DisplayMember = "LocalizedDisplay";
			colDelivery.DisplayMember = m_listBoxDeliveries.DisplayMember = "LocalizedDisplay";
			m_originalDefaultFontForLists = new FontProxy(m_listBoxCharacters.Font);
			m_originalDefaultFontForCharacterAndDeliveryColumns = new FontProxy(m_dataGridReferenceText.DefaultCellStyle.Font);
			SetFontsFromViewModel(this, null);

			m_viewModel.AssignedBlocksIncremented += m_viewModel_AssignedBlocksIncremented;
			m_viewModel.UiFontSizeChanged += SetFontsFromViewModel;

			m_blocksViewer.VisibleChanged += BlocksViewerVisibleChanged;

			SetFilterControlsFromMode();

			m_viewModel.CurrentBookSaved += UpdateSavedText;
			m_viewModel.FilterReset += HandleFilterReset;

{'fraction_non_alphanumeric': 0.0730869100351306, 'fraction_numerical': 0.003589430273407668, 'mean_word_length': 5.774443869632695, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '914158', 'n_tokens_mistral': 4241, 'n_tokens_neox': 3740, 'n_words': 663}