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 MainForm : Form
	{
		/*
		private void remove__PatternExNihilo()
		{
			if (this.Current__PatternExNihilo.SymbolClusterRow != null)	/// je ne vois pas comment cela ne serait pas à true, mais bon...
			{
				this.DataSet.SymbolCluster.RemoveSymbolClusterRow(this.Current__PatternExNihilo.SymbolClusterRow);
			}
			else
			{
				this.DataSet.PatternExNihilo.RemovePatternExNihiloRow(this.Current__PatternExNihilo);
			}

			this.Current__Pattern.ModeCode = MainForm.ModeCode_NotDefined;


			this.populate__PatternExNihilo(PopulatingType.Cleaning);
		}

		private void populate__PatternExNihilo(PopulatingType populatingType)
		{
			if (populatingType == PopulatingType.Cleaning)
			{
				this.Current__PatternExNihilo = null;

				this.textBox__Pattern__ExNihilo__SymbolClusterString.Text = null;
			}
			else
			{ 
				if (populatingType == PopulatingType.FindingRowsAndSettingFields)
				{
					this.Current__PatternExNihilo 
						= this.DataSet.PatternExNihilo
						.FindById(this.Current__Pattern.Id);
				}

				this.textBox__Pattern__ExNihilo__SymbolClusterString.Text
					= this.Current__PatternExNihilo 
					.SymbolClusterRow
					.String;
			}
		}

		private void button__Pattern__Type__ExNihilo__RESTORE_Click(object sender, EventArgs e)
		{
			this.textBox__Pattern__ExNihilo__SymbolClusterString.Text 
				= this.Current__PatternExNihilo.SymbolClusterRow.String;
		}

		private void textBox__Pattern__Type__ExNihilo__SymbolClusterString_TextChanged(object sender, EventArgs e)
		{
			if (this.doNotTriggerEvent)
			{
				return;
			}

			/// only '!' and '?'
			foreach (char c in this.textBox__Pattern__ExNihilo__SymbolClusterString.Text)
			{
				if (c != '!' && c != '?')
				{
					MessageBox.Show
					(
						"Only '!' and '?' characters are allowed in the 'Symbol Cluster String' text box.",
						"Invalid Key Entry",
						MessageBoxButtons.OK,
						MessageBoxIcon.Error
					);

					this.hasError__SymbolClusterString = true;
					return;
				}
			}

			/// not empty
			if (string.IsNullOrEmpty(this.textBox__Pattern__ExNihilo__SymbolClusterString.Text))
			{ 
				MessageBox.Show
				(
					"The 'Symbol Cluster String' can not be empty.",
					"Empty 'Symbol Cluster String'",
					MessageBoxButtons.OK,
					MessageBoxIcon.Error
				);

				this.hasError__SymbolClusterString = true;
				return;
			}

			/// not already existing
			BabebiDataSet.SymbolClusterRow[] symbolClusters 
				= 
				(BabebiDataSet.SymbolClusterRow[])
				(this.DataSet.SymbolCluster.Select("String = '" + this.textBox__Pattern__ExNihilo__SymbolClusterString.Text + "'"));

			if (symbolClusters.Length == 1)
			{
				BabebiDataSet.SymbolClusterRow symbolCluster = symbolClusters[0];

				if (this.Current__PatternExNihilo.SymbolClusterId != symbolCluster.Id)
				{
					MessageBox.Show
					(
						"The 'Symbol Cluster String': '" + symbolClusters[0].String + "' is already the one of an existing 'Ex-Nihilo' Pattern: " +
						"so, it can not be used for the current Pattern.",
						"UPDATE ABORTED",
						MessageBoxButtons.OK,
						MessageBoxIcon.Error
					);

					this.hasError__SymbolClusterString = true;
					return;

				}
			}


			this.hasError__SymbolClusterString = false;
		}

		private bool hasError__SymbolClusterString_ = false;
		private bool hasError__SymbolClusterString
		{
			get { return this.hasError__SymbolClusterString_; }
			set
			{
				this.hasError__SymbolClusterString_ = value;

				if (this.hasError__SymbolClusterString_)
				{
					this.label__Pattern__ExNihilo__SymbolClusterString.ForeColor = Color.Red;
					this.textBox__Pattern__ExNihilo__SymbolClusterString.ForeColor = Color.Red;
				}
				else
				{
					if (this.textBox__Pattern__ExNihilo__SymbolClusterString.Text == this.Current__Pattern.UniqueKeyString)
					{
						this.label__Pattern__ExNihilo__SymbolClusterString.ForeColor = Color.Black;
						this.textBox__Pattern__ExNihilo__SymbolClusterString.ForeColor = Color.Black;
					}
					else
					{ 
						this.label__Pattern__ExNihilo__SymbolClusterString.ForeColor = Color.Blue;
						this.textBox__Pattern__ExNihilo__SymbolClusterString.ForeColor = Color.Blue;
					}
				}
			}
		}

		private void button__Pattern__ExNihilo__CreateUpdate__Click(object sender, EventArgs e)
		{
			if (this.hasError__SymbolClusterString)
			{ 
				MessageBox.Show
				(
					"The 'Symbol Cluster String' is not valid.\n The Create/Update process aborts.",
					"Error on 'Symbol Cluster String'",
					MessageBoxButtons.OK,
					MessageBoxIcon.Error
				);

				return;
			}

			string newSymbolClusterString = this.textBox__Pattern__ExNihilo__SymbolClusterString.Text;

			if (this.Current__PatternExNihilo != null)
			{
				if (newSymbolClusterString == this.Current__PatternExNihilo.SymbolClusterRow.String)
				{
					MessageBox.Show
					(
						"The value of the 'Symbol Cluster String' field is the same as the one currently stored for this Pattern: there is nothing to update.",
						"UPDATE ABORTED"
					);
					return;
				}
			}
			
			int nextId = -1;
			foreach (BabebiDataSet.SymbolClusterRow symbolCluster in this.DataSet.SymbolCluster)
			{
				if (symbolCluster.Id > nextId)
				{
					nextId = symbolCluster.Id;
				}
			}
			nextId++;

			BabebiDataSet.SymbolClusterRow newSymbolCluster 
				= this.DataSet.SymbolCluster.AddSymbolClusterRow(nextId, newSymbolClusterString);
			
			short rank = 0;
			foreach (char c in newSymbolClusterString)
			{
				rank++;

				if (c == '!')
				{
					this.DataSet.SymbolClusterSymbol.AddSymbolClusterSymbolRow(newSymbolCluster, rank, this.TheSymbolForVowel);
				}
				else if (c == '?')
				{
					this.DataSet.SymbolClusterSymbol.AddSymbolClusterSymbolRow(newSymbolCluster, rank, this.TheSymbolForConsonant);
				}
				else
				{
					throw new Exception();
				}
			}

			if (this.Current__PatternExNihilo == null)
			{
				this.Current__Pattern.ModeCode = MainForm.ModeCode_ExNihilo;

				this.Current__PatternExNihilo
					= this.DataSet.PatternExNihilo
					.AddPatternExNihiloRow
					(
						this.Current__Pattern.Id,
						newSymbolCluster,
						MainForm.ModeCode_ExNihilo
					);
			}
			else
			{ 				
				BabebiDataSet.SymbolClusterRow symbolCluster__ToBeDeleted 
					= this.Current__PatternExNihilo.SymbolClusterRow;

				this.Current__PatternExNihilo.SymbolClusterRow = newSymbolCluster;

				this.DataSet.SymbolCluster.RemoveSymbolClusterRow(symbolCluster__ToBeDeleted);
			}

			this.Current__Pattern.UniqueKeyString = newSymbolClusterString;

			this.textBox__Pattern__UniqueKeyString.Text =  newSymbolClusterString;

			this.label__Pattern__ExNihilo__SymbolClusterString.ForeColor	= Color.Black;
			this.textBox__Pattern__ExNihilo__SymbolClusterString.ForeColor	= Color.Black;
		}

		*/
	}
}