using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Babebi4
{
	public partial class Form__LetterOrSymbol_Language__Modify : Form
	{
		private MainForm mainForm;

		private BabebiDataSet.LetterDataTable			letterTable;

		private BabebiDataSet.LetterOrSymbol_LanguageDataTable	letterOrSymbol_LanguageTable;
		private BabebiDataSet.LetterOrSymbol_LanguageRow		letterOrSymbol_Language;

		#region		ctors

		public Form__LetterOrSymbol_Language__Modify(MainForm mainForm) : this()
		{
			this.mainForm = mainForm;

			this.letterTable = this.mainForm.DataSet.Letter;

			this.letterOrSymbol_LanguageTable = this.mainForm.DataSet.LetterOrSymbol_Language;

			this.letterOrSymbol_Language = this.mainForm.Current__LetterOrSymbol_Language;


		//	moisi : Expression ? this.textBox__Current.Text = this.letterOrSymbol_Language.LetterString;
		}

		public Form__LetterOrSymbol_Language__Modify()
		{
			InitializeComponent();
		}

		#endregion	ctors

		//	moisi
		//private void computeUniqueKeyString(BabebiDataSet.WordChildRow wordChildRow)
		//{
		//	string wordUniqueKeyString = "";

		//	wordChildRow.WordRowParent.UniqueKeyString = wordUniqueKeyString;

		//	this.cascadeModify(wordChildRow.WordRowParent);
		//}


		private void cascadeModify(BabebiDataSet.WordRow wordRow)
		{
			#region		having WordParent
			
			//moisi

			//BabebiDataSet.WordParentRow[] WordParentRows = wordRow.GetWordParentRows();

			//if (WordParentRows.Length == 1)
			//{
			//	BabebiDataSet.WordParentRow WordParentRow = WordParentRows[0];

			//	foreach (BabebiDataSet.WordChildRow wordChildRow in WordParentRow.GetWordChildRows())
			//	{
			//	}
			//}

			#endregion	having WordParent
			#region		having WordDerivating

			#endregion	having WordDerivating
			#region		participating to concatenating

			#endregion	participating to concatenating
		}

		private void button__Modify__Click(object sender, EventArgs e)
		{
			string current	= this.textBox__Current.Text;
			string modified	= this.textBox__Modified.Text;

			#region		checking

			if (modified == current)
			{
				MessageBox.Show("Same value => Cancel");
			}

			if (modified == "")
			{
				MessageBox.Show("The new string of the letter can not be empty.", "ERROR",MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}

			BabebiDataSet.LetterRow letterRow =	this.letterTable.FindByString(modified);

			if (letterRow != null)
			{
				MessageBox.Show
				(
					"The '" + modified + "' Letter is already existing:\n You must select a not-already-existing string for the Letter.", 
					"ERROR",
					MessageBoxButtons.OK, 
					MessageBoxIcon.Error
				);
				return;
			}

			#endregion	checking


			//moisi	this.letterOrSymbol_Language.LetterRow.String = modified;
			
			/* moisi du 20201108?
			foreach 
			(
				BabebiDataSet.LetterClusterLetterRow letterClusterLetterRow
				in
				this.letterOrSymbol_Language.LetterRow.GetLetterClusterLetterRows()
			)
			{
				BabebiDataSet.LetterClusterRow letterClusterRow
					= letterClusterLetterRow.LetterClusterRow;

				string modifiedLetterClusterString = "";
				foreach
				(
					BabebiDataSet.LetterClusterLetterRow childLetterClusterLetterRow
					in
					letterClusterRow.GetLetterClusterLetterRows()
				)
				{
					modifiedLetterClusterString += childLetterClusterLetterRow.LetterString;
				}

				letterClusterRow.String = modifiedLetterClusterString;

				#region		Derivating

				foreach
				(
					BabebiDataSet.DerivatingRow derivatingRow 
					in 
					letterClusterRow.GetDerivatingRows())
				{
					foreach
					(
						BabebiDataSet.WordDerivatedRow wordDerivatedRow
						in 
						derivatingRow.GetWordDerivatedRows()
					)
					{
						string wordDerivatingUniqueKeyString 
							= wordDerivatedRow.WordDerivatingRowParent.WordRowParent.UniqueKeyString;

						string wordUniqueKeyString;

						if (derivatingRow.AppendStringAtLast)
						{
							wordUniqueKeyString = wordDerivatingUniqueKeyString + modifiedLetterClusterString;
						}
						else
						{ 
							wordUniqueKeyString	= modifiedLetterClusterString + wordDerivatingUniqueKeyString;
						}

						wordDerivatedRow.WordRowParent.UniqueKeyString = wordUniqueKeyString;

						this.cascadeModify(wordDerivatedRow.WordRowParent);
					}
				}

				#endregion	Derivating
				#region		Child	

				foreach
				(
					BabebiDataSet.ChildRow childRow 
					in 
					letterClusterRow.GetChildRowsByChild__LetterCluster__1())
				{
					BabebiDataSet.WordOrPatternRow parentRow = childRow.ParentRowParent;

					#region		WordFromPattern ?

					BabebiDataSet.WordFromPatternRow[] wordFromPatternRows = childRow.GetWordFromPatternRows();
					if (wordFromPatternRows.Length == 1)
					{
						BabebiDataSet.WordFromPatternRow wordFromPatternRow = wordFromPatternRows[0];

						string wordUniqueKeyString;

						BabebiDataSet.PatternRow patternRow = wordFromPatternRow.PatternRowParent;

						string pattern = patternRow.UniqueKeyString;

						List<string> subStrings = new List<string>();
						subStrings.Add("");

						int i = 0;
						foreach (char c in pattern)
						{
							if (c == '!' || c == '?')
							{
								i++;
								subStrings.Add("");
							}
							else
							{ 
								subStrings[i] += c;
							}
						}


						if (subStrings.Count == 2)
						{
							wordUniqueKeyString
								= subStrings[0]
								+ childRow.LetterClusterRowByChild__LetterCluster__1.String
								+ subStrings[1];
						}
						else if (subStrings.Count == 3)
						{
							wordUniqueKeyString
								= subStrings[0]
								+ childRow.LetterClusterRowByChild__LetterCluster__1.String
								+ subStrings[1]
								+ childRow.LetterClusterRowByChild__LetterCluster__2.String
								+ subStrings[2];
						}
						else if (subStrings.Count == 4)
						{
							wordUniqueKeyString
								= subStrings[0]
								+ childRow.LetterClusterRowByChild__LetterCluster__1.String
								+ subStrings[1]
								+ childRow.LetterClusterRowByChild__LetterCluster__2.String
								+ subStrings[2]
								+ childRow.LetterClusterRowByChild__LetterCluster__3.String
								+ subStrings[3];
						}
						else
						{
							throw new NotImplementedException
							(
								"The pattern contains " + subStrings.Count.ToString() + "."
							);
						}

						wordFromPatternRow.WordRowParent.UniqueKeyString = wordUniqueKeyString;

						this.cascadeModify(wordFromPatternRow.WordRowParent);
					}

					#endregion	WordFromPattern ?
				}

				#endregion	Child
			}
			*/
			this.Close();
		}
	}
}
