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
	{
		public int DataGridView__PatternFromPattern__of__Current__Pattern__CurrentPatternId = -1;

		public BabebiDataSet.PatternFromPatternRow	Current__PatternFromPattern__of__Current__Pattern						= null;
		public BabebiDataSet.FromPatternRow			Current__FromPattern__of__PatternFromPattern__of__Current__Pattern		= null;
		public BabebiDataSet.PatternRow				Current__Pattern__of__PatternFromPattern__of__Current__Pattern			= null;
		public BabebiDataSet.Pattern_LanguageRow	Current__Pattern_Language__of__PatternFromPattern__of__Current__Pattern	= null;
		public BabebiDataSet.WordOrPatternRow		Current__WordOrPattern__of__PatternFromPattern__of__Current__Pattern	= null;

		private void bindingSource__PatternFromPattern__of__Current__Pattern__CurrentChanged(object sender, EventArgs e)
		{
			if (this.BindingSource__PatternFromPattern__of__Current__Pattern.Current != null)
			{
				DataRow guiDataRow
					=
					(
						(DataRowView)
						(this.BindingSource__PatternFromPattern__of__Current__Pattern.Current)
					).Row;
				
				
				int patternId = this.guiTable__PatternFromPattern__of__Current__Pattern.GetValue__Id(guiDataRow);

				this.DataGridView__PatternFromPattern__of__Current__Pattern__CurrentPatternId = patternId;

				this.Current__PatternFromPattern__of__Current__Pattern						
					= this.DataSet.PatternFromPattern
					.FindById(patternId);

				this.Current__Pattern__of__PatternFromPattern__of__Current__Pattern			
					= this.Current__PatternFromPattern__of__Current__Pattern.PatternRowParent;

				this.Current__Pattern_Language__of__PatternFromPattern__of__Current__Pattern
					= this.DataSet.Pattern_Language
					.FindByPatternIdLanguageCode
					(
						patternId,
						this.Current__Language.Code
					);

				this.Current__FromPattern__of__PatternFromPattern__of__Current__Pattern	
					= this.Current__PatternFromPattern__of__Current__Pattern.FromPatternRowParent;

				this.Current__WordOrPattern__of__PatternFromPattern__of__Current__Pattern
					= this.Current__Pattern__of__PatternFromPattern__of__Current__Pattern.WordOrPatternRowParent;

				this.button__Pattern__Children__PatternFromPattern__Update.Visible = true;
				this.button__Pattern__Children__PatternFromPattern__Focus.Visible	= true;
				this.button__Pattern__Children__PatternFromPattern__Delete.Visible = true;
			}
			else
			{ 
				this.DataGridView__PatternFromPattern__of__Current__Pattern__CurrentPatternId = -1;

				this.Current__PatternFromPattern__of__Current__Pattern						= null;
				this.Current__Pattern__of__PatternFromPattern__of__Current__Pattern			= null;
				this.Current__Pattern_Language__of__PatternFromPattern__of__Current__Pattern	= null;
				this.Current__FromPattern__of__PatternFromPattern__of__Current__Pattern	= null;
				this.Current__WordOrPattern__of__PatternFromPattern__of__Current__Pattern	= null;

				this.button__Pattern__Children__PatternFromPattern__Update	.Visible = false;
				this.button__Pattern__Children__PatternFromPattern__Focus	.Visible = false;
				this.button__Pattern__Children__PatternFromPattern__Delete	.Visible = false;
			}
		}

		#region		4 buttons events

		private void button__Pattern__Children__PatternFromPattern__Create__Click(object sender, EventArgs e)
		{
			Form__PatternFromPattern__of__Current__Pattern__CreateUpdate form = new Form__PatternFromPattern__of__Current__Pattern__CreateUpdate(this, true);

			if (form.ShowDialog() == DialogResult.Cancel)
			{
				return;
			}

			DataRow focusedGuiDataRow 
				= this.guiTable__PatternFromPattern__of__Current__Pattern
				.Find(this.Current__PatternFromPattern__of__Current__Pattern.Id);

			FocusGridRow(this.BindingSource__PatternFromPattern__of__Current__Pattern, focusedGuiDataRow);
		}

		private void button__Pattern__Children__PatternFromPattern__Update__Click(object sender, EventArgs e)
		{
			Form__PatternFromPattern__of__Current__Pattern__CreateUpdate form = new Form__PatternFromPattern__of__Current__Pattern__CreateUpdate(this, false);

			form.ShowDialog();
		}

		private void button__Pattern__Children__PatternFromPattern__Delete__Click(object sender, EventArgs e)
		{
			if (this.Current__PatternFromPattern__of__Current__Pattern == null)
			{
				MessageBox.Show("There is no 'Pattern (From Pattern)' to delete.", "Empty", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return; 
			}

			DialogResult dialogResult 
				= MessageBox.Show
				(
					"Do you really want to delete the 'Pattern' (" + 
					this.Current__PatternFromPattern__of__Current__Pattern.PatternRowParent.UniqueKeyString +
					") ?",
					"Deleting", 
					MessageBoxButtons.YesNo, 
					MessageBoxIcon.Question
				);
			if (dialogResult == DialogResult.No)
			{
				return;
			}

			this.DeletePattern(this.Current__PatternFromPattern__of__Current__Pattern.PatternRowParent);
		}

		private void button__Pattern__Children__PatternFromPattern__Focus__Click(object sender, EventArgs e)
		{
			if (this.Current__PatternFromPattern__of__Current__Pattern == null)
			{
				return;
			}

			this.Current__Pattern = this.Current__PatternFromPattern__of__Current__Pattern.PatternRowParent;

			this.Current__Pattern_Language = (BabebiDataSet.Pattern_LanguageRow)(this.DataRow__Language(this.Current__Pattern));

			this.populate__Pattern();

			/// on pointe par défaut sur WFPs
			if (this.tabControl__Pattern__Children.SelectedIndex != 0)
			{
				this.tabControl__Pattern__Children.SelectedIndex = 0;
			}
		}

		#endregion	4 buttons events
	}
}