using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace LLNet.Controls
{
	public delegate void BeforeGetRowEventHandler(object sender, EventArgs e);
	public delegate void AfterGetRowEventHandler(object sender, AdvancedListEventArgs e);

	public class AdvancedListEventArgs : System.EventArgs
	{
		private DataRow row;

		public DataRow Row
		{
			set { row = value; }
			get { return row; }
		}

		public AdvancedListEventArgs()
		{

		}

	}
	/// <summary>
	/// Summary description for LLAdvancedList.
	/// </summary>
	public class LLAdvancedList : DevExpress.XtraEditors.ButtonEdit 
	{
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		private Model.IBLL bllInstance;

		private Utils.QueryParams externPList;
		private Utils.QueryParams qpResult;
		
		private string connectionName;

		public event BeforeGetRowEventHandler BeforeGetRow;
		public event AfterGetRowEventHandler AfterGetRow;

		public LLAdvancedList()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			// TODO: Add any initialization after the InitializeComponent call
			this.ButtonClick +=new DevExpress.XtraEditors.Controls.ButtonPressedEventHandler(LLAdvancedList_ButtonClick);
		
		

		}

		private dlgAdvancedList dlg;

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
																									new DevExpress.XtraEditors.Controls.EditorButton()});
			this.Properties.Name = "Properties";
			this.Size = new System.Drawing.Size(75, 20);
			this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.LLAdvancedList_KeyDown);
			this.Validating += new System.ComponentModel.CancelEventHandler(this.LLAdvancedList_Validating);

		}
		#endregion


		#region public properties

		public Model.IBLL BllInstance
		{
			get {return bllInstance;}
			set {bllInstance = value;}
		}

		public Utils.QueryParams QueryParams
		{
			get {return externPList;}
			set {externPList = value;}
		}

		public Utils.QueryParams QPResult
		{
			get {return qpResult;}
			set {qpResult = value;}
		}

		public string ConnectionName
		{
			get {return connectionName;}
			set {connectionName=value;}
		}
		#endregion

		private void OpenListOfValues()
		{
			object editValue;
			object tag;

			editValue = this.EditValue;
			tag = this.Tag;
			
			dlg = new dlgAdvancedList();
			dlg.dataView = this.DataBindings["EditValue"].DataSource as DataView;
			dlg.dataView.Table.Clear();
			dlg.llToolbar.SetDataBinding(dlg.dataView);
			dlg.Grid.DataSource = dlg.dataView;
			dlg.llToolbar.Open(bllInstance,connectionName,externPList);
						

			if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				DataRowView drv = null;
				if (dlg.BindingContext[dlg.dataView].Count > 0)
				{
					drv = dlg.BindingContext[dlg.dataView].Current as DataRowView;
					this.Tag = drv.Row;
					this.EditValue = drv.Row[DataBindings["EditValue"].BindingMemberInfo.BindingField].ToString();
				}

				if (AfterGetRow != null)
				{
					AdvancedListEventArgs aea = new AdvancedListEventArgs();
					if (dlg.BindingContext[dlg.dataView].Count > 0)
					{
						aea.Row = drv.Row;
					}
					AfterGetRow(this,aea);
					//this.Tag = drv.Row[DataBindings["Tag"].BindingMemberInfo.BindingField].ToString();
					
				}
			}
			else
			{
				this.Tag = tag;
				this.EditValue = editValue;
				
			}

		}

		private void LLAdvancedList_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
		{
			OpenListOfValues();
		}

		public void GetRow()
		{
			qpResult = null;

			string pValue = this.DataBindings["EditValue"].BindingMemberInfo.BindingField.ToString();
			string vParam = "'" + this.EditValue.ToString() + "'";

			if (BeforeGetRow != null)
			{
				BeforeGetRow(vParam,null);
			}

			Utils.QueryParams oldExternPList = externPList;


			if (qpResult == null)
			{
				Utils.QueryParams qPrms = new LLNet.Utils.QueryParams();

				qPrms.AddRecursiveConditionParam(Utils.QueryParams.PredefinedTags.AllToolbarsTag,"AND",pValue,"=",vParam);
				externPList = qPrms;
			}
			else
			{
				externPList = qpResult;
			}
					
			dlg = new dlgAdvancedList();
			dlg.dataView = this.DataBindings["EditValue"].DataSource as DataView;
			dlg.llToolbar.SetDataBinding(dlg.dataView);
			dlg.Grid.DataSource = dlg.dataView;
			dlg.llToolbar.Open(bllInstance,connectionName,externPList);

			externPList = oldExternPList;

			DataRowView drv = null;

			if (dlg.BindingContext[dlg.dataView].Count > 0)
			{
				drv = dlg.BindingContext[dlg.dataView].Current as DataRowView;
				this.Tag = drv.Row;
			}
			if (AfterGetRow != null)
			{
				AdvancedListEventArgs aea = new AdvancedListEventArgs();

				if (dlg.BindingContext[dlg.dataView].Count > 0)
				{
					aea.Row = drv.Row;
				}
				AfterGetRow(this,aea);
			}

		}
		
		private void LLAdvancedList_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			switch ( e.KeyCode)
			{
				case Keys.Enter:
					GetRow();
					break;
				case Keys.F4:
					OpenListOfValues();
					break;
			}
		}

		private void LLAdvancedList_Validating(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if (Utils.Helpers.ObjToStr(this.EditValue)!="")
			{
				GetRow();
			}
			else
			{
				this.EditValue = null;
			}
		}
	}
}
