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

namespace LLNet.Controls
{
	public delegate void AfterButtonClickEventHandler();
	public delegate void BeforeButtonClickEventHandler();

	/// <summary>
	/// Summary description for LLRepAdvancedList.
	/// </summary>
	public class LLRepAdvancedList : DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
	
		private dlgAdvancedList dlg;
		private object oldEditValue;
		private object oldTag;
		private Utils.QueryParams oldExternPList;

		private Model.IBLL bllInstance;

		private Utils.QueryParams externPList;

		private string queryname;

		private string connectionName;
		
		private object objView;

		public LLRepAdvancedList()
		{
			///
			/// Required for Windows.Forms Class Composition Designer support
			///
			InitializeComponent();

			this.ButtonClick +=new DevExpress.XtraEditors.Controls.ButtonPressedEventHandler(LLRepAdvancedList_ButtonClick);
		}

		/// <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 public properties
		
		public event AfterButtonClickEventHandler AfterButtonClick;
		public event BeforeButtonClickEventHandler BeforeButtonClick;

		private DevExpress.XtraEditors.BaseEdit actEditor
		{
			get 
			{
				if (objView is DevExpress.XtraGrid.Views.Grid.GridView)
				{
					return ((DevExpress.XtraGrid.Views.Grid.GridView)objView).ActiveEditor;
				}
				else
				{
					return ((DevExpress.XtraVerticalGrid.VGridControl)objView).ActiveEditor;

				}
			}
		}

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

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

		public string QueryName
		{
			get { return queryname; }
			set { queryname = value; }
		}

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

		public dlgAdvancedList DlgAdvancedList
		{
			get { return dlg; }
		}

		#endregion

		#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()
		{
			((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
			// 
			// LLRepAdvancedList
			// 
			this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.LLRepAdvancedList_KeyDown);
			this.Validating += new System.ComponentModel.CancelEventHandler(this.LLRepAdvancedList_Validating);
			((System.ComponentModel.ISupportInitialize)(this)).EndInit();

		}
		#endregion


		public void OpenListOfValues()
		{
			if (BeforeButtonClick != null)
			{
				BeforeButtonClick();
			}
			
			oldEditValue = actEditor.EditValue;
			oldTag = actEditor.Tag;
			dlg = new dlgAdvancedList();
			dlg.dataView = actEditor.DataBindings["EditValue"].DataSource as DataView;
			dlg.dataView.Table.Clear();
			dlg.llToolbar.SetDataBinding(dlg.dataView);
			dlg.Grid.DataSource = dlg.dataView;
			dlg.llToolbar.QueryName = this.queryname;
			dlg.llToolbar.Open(bllInstance,connectionName,this.externPList);

			if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				if (dlg.BindingContext[dlg.dataView].Count > 0)
				{
					if (actEditor != null)
					{
						DataRowView drv = dlg.BindingContext[dlg.dataView].Current as DataRowView;
						actEditor.EditValue = drv.Row[actEditor.DataBindings["EditValue"].BindingMemberInfo.BindingField].ToString();
						actEditor.Tag = drv.Row[actEditor.DataBindings["Tag"].BindingMemberInfo.BindingField].ToString();
					}
				}
				if (AfterButtonClick != null)
				{
					AfterButtonClick();
				}
			}
			else
			{
				if (actEditor != null)
				{
					actEditor.Tag = oldTag;
					actEditor.EditValue = oldEditValue;
				}
			}
		}
		private void LLRepAdvancedList_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
		{
			if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Ellipsis)
			{
				OpenListOfValues();
			}
		}

		public void GetRow()
		{
			if (BeforeButtonClick != null)
			{
				BeforeButtonClick();
			}

			dlg = new dlgAdvancedList();

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

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

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

			externPList = oldExternPList;

			if (dlg.BindingContext[dlg.dataView].Count > 0)
			{
				DataRowView drv = dlg.BindingContext[dlg.dataView].Current as DataRowView;
				if (actEditor != null)
				{
					actEditor.EditValue = drv.Row[actEditor.DataBindings["EditValue"].BindingMemberInfo.BindingField].ToString();
					actEditor.Tag = drv.Row[actEditor.DataBindings["Tag"].BindingMemberInfo.BindingField].ToString();
				}
			}
			if (AfterButtonClick != null)
			{
				AfterButtonClick();
			}

		}

		private void LLRepAdvancedList_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			switch ( e.KeyCode)
			{
				case Keys.Enter:
					if (actEditor.Text != "") 
					{
						GetRow();
					}
//					else 
//					{
//						OpenListOfValues();
//					}

					break;
				case Keys.F4:
					OpenListOfValues();
					break;
			}
		}

		public bool CancelValidating = false;

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

		}
	}
}
