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

namespace TestCOM
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private NiApiLib.SrvrSession SrvrSession;
		private NiApiLib.Table tblFev;
		private NiApiLib.Filter myFiltr;
		private NiApiLib.Structure myStructure;
		private NiApiLib.DataRow drFev;
		private System.Windows.Forms.CheckBox [] checkboxArray;
		private System.Data.DataTable dtTickers;
        private System.Data.DataTable dtSelectedTickers;
		private DataSet myDataSet;
		private NiApiLib.DataRowSet myDataRowSet;
		private System.Boolean UpdatesOn;
		private System.Boolean Connected;
        private string[] tickersNameArray;
        public string a;
        private string[] selectedTickersArray;
	
		
		//---------------------------------------------------------------------
		private System.Windows.Forms.Button btnConnect;
		private System.Windows.Forms.Button btnDisconnect;
		private System.Windows.Forms.GroupBox gbConnectionSettings;
		private System.Windows.Forms.Label lblServerAddress;
		private System.Windows.Forms.Label lblPort;
		private System.Windows.Forms.Label lblLoginName;
		private System.Windows.Forms.Label lblPassword;
		private System.Windows.Forms.Label lblConnectionStatus;
		private System.Windows.Forms.Label lblConnectionStatuseMsg;
		private System.Windows.Forms.TextBox tbServerAddress;
		private System.Windows.Forms.TextBox tbPort;
		private System.Windows.Forms.TextBox tbLoginName;
		private System.Windows.Forms.TextBox tbPassword;
		private System.Windows.Forms.DataGrid dgTickers;
		private System.Windows.Forms.CheckBox checkBox1;
		private System.Windows.Forms.CheckBox checkBox2;
		private System.Windows.Forms.Button button1;
        private Button btSetFilter;
        private Button btClearFilters;
		
		
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public MainForm()
		{
			InitializeComponent();

			this.SrvrSession = new NiApiLib.SrvrSessionClass();
			this.tblFev= new TableClass();
			this.myFiltr=new FilterClass();
			this.myStructure=new NiApiLib.StructureClass();
			this.drFev=new NiApiLib.DataRowClass();
			this.myDataRowSet=new DataRowSetClass();
			this.UpdatesOn=false;
			this.Connected=false;
			

			this.SrvrSession.OnMessage+=new _ISrvrSessionEvents_OnMessageEventHandler(SrvrSession_OnMessage);
			this.SrvrSession.OnLogin+=new _ISrvrSessionEvents_OnLoginEventHandler(SrvrSession_OnLogin);
			this.tblFev.OnData+=new _ITableEvents_OnDataEventHandler(tblFev_OnData);
			this.tblFev.OnDataComplete+=new _ITableEvents_OnDataCompleteEventHandler(tblFev_OnDataComplete);
		//	this.tblFev.OnModify += new _ITableEvents_OnModifyEventHandler(tblFev_OnModify);
			this.tblFev.OnInsert += new _ITableEvents_OnInsertEventHandler(tblFev_OnModify);
			
		}

		/// <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 Windows Form 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.btnConnect = new System.Windows.Forms.Button();
            this.btnDisconnect = new System.Windows.Forms.Button();
            this.gbConnectionSettings = new System.Windows.Forms.GroupBox();
            this.tbPassword = new System.Windows.Forms.TextBox();
            this.tbLoginName = new System.Windows.Forms.TextBox();
            this.tbPort = new System.Windows.Forms.TextBox();
            this.tbServerAddress = new System.Windows.Forms.TextBox();
            this.lblPassword = new System.Windows.Forms.Label();
            this.lblLoginName = new System.Windows.Forms.Label();
            this.lblPort = new System.Windows.Forms.Label();
            this.lblServerAddress = new System.Windows.Forms.Label();
            this.lblConnectionStatus = new System.Windows.Forms.Label();
            this.lblConnectionStatuseMsg = new System.Windows.Forms.Label();
            this.dgTickers = new System.Windows.Forms.DataGrid();
            this.checkBox1 = new System.Windows.Forms.CheckBox();
            this.checkBox2 = new System.Windows.Forms.CheckBox();
            this.button1 = new System.Windows.Forms.Button();
            this.btSetFilter = new System.Windows.Forms.Button();
            this.btClearFilters = new System.Windows.Forms.Button();
            this.gbConnectionSettings.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dgTickers)).BeginInit();
            this.SuspendLayout();
            // 
            // btnConnect
            // 
            this.btnConnect.Location = new System.Drawing.Point(339, 83);
            this.btnConnect.Name = "btnConnect";
            this.btnConnect.Size = new System.Drawing.Size(115, 28);
            this.btnConnect.TabIndex = 0;
            this.btnConnect.Text = "Connect";
            this.btnConnect.Click += new System.EventHandler(this.button1_Click);
            // 
            // btnDisconnect
            // 
            this.btnDisconnect.Location = new System.Drawing.Point(472, 83);
            this.btnDisconnect.Name = "btnDisconnect";
            this.btnDisconnect.Size = new System.Drawing.Size(116, 28);
            this.btnDisconnect.TabIndex = 1;
            this.btnDisconnect.Text = "Disconnect";
            this.btnDisconnect.Click += new System.EventHandler(this.button2_Click);
            // 
            // gbConnectionSettings
            // 
            this.gbConnectionSettings.Controls.Add(this.tbPassword);
            this.gbConnectionSettings.Controls.Add(this.tbLoginName);
            this.gbConnectionSettings.Controls.Add(this.tbPort);
            this.gbConnectionSettings.Controls.Add(this.tbServerAddress);
            this.gbConnectionSettings.Controls.Add(this.lblPassword);
            this.gbConnectionSettings.Controls.Add(this.lblLoginName);
            this.gbConnectionSettings.Controls.Add(this.lblPort);
            this.gbConnectionSettings.Controls.Add(this.lblServerAddress);
            this.gbConnectionSettings.Location = new System.Drawing.Point(10, 9);
            this.gbConnectionSettings.Name = "gbConnectionSettings";
            this.gbConnectionSettings.Size = new System.Drawing.Size(316, 139);
            this.gbConnectionSettings.TabIndex = 3;
            this.gbConnectionSettings.TabStop = false;
            this.gbConnectionSettings.Text = "Connection Settings";
            // 
            // tbPassword
            // 
            this.tbPassword.Location = new System.Drawing.Point(125, 102);
            this.tbPassword.Name = "tbPassword";
            this.tbPassword.Size = new System.Drawing.Size(173, 22);
            this.tbPassword.TabIndex = 7;
            this.tbPassword.Text = "tester";
            // 
            // tbLoginName
            // 
            this.tbLoginName.Location = new System.Drawing.Point(125, 74);
            this.tbLoginName.Name = "tbLoginName";
            this.tbLoginName.Size = new System.Drawing.Size(173, 22);
            this.tbLoginName.TabIndex = 6;
            this.tbLoginName.Text = "tester";
            // 
            // tbPort
            // 
            this.tbPort.Location = new System.Drawing.Point(125, 46);
            this.tbPort.Name = "tbPort";
            this.tbPort.Size = new System.Drawing.Size(173, 22);
            this.tbPort.TabIndex = 5;
            this.tbPort.Text = "2900";
            // 
            // tbServerAddress
            // 
            this.tbServerAddress.Location = new System.Drawing.Point(125, 18);
            this.tbServerAddress.Name = "tbServerAddress";
            this.tbServerAddress.Size = new System.Drawing.Size(173, 22);
            this.tbServerAddress.TabIndex = 4;
            this.tbServerAddress.Text = "10.200.200.161";
            // 
            // lblPassword
            // 
            this.lblPassword.Location = new System.Drawing.Point(10, 111);
            this.lblPassword.Name = "lblPassword";
            this.lblPassword.Size = new System.Drawing.Size(105, 18);
            this.lblPassword.TabIndex = 3;
            this.lblPassword.Text = "Password";
            // 
            // lblLoginName
            // 
            this.lblLoginName.Location = new System.Drawing.Point(10, 83);
            this.lblLoginName.Name = "lblLoginName";
            this.lblLoginName.Size = new System.Drawing.Size(105, 19);
            this.lblLoginName.TabIndex = 2;
            this.lblLoginName.Text = "Login Name";
            // 
            // lblPort
            // 
            this.lblPort.Location = new System.Drawing.Point(10, 55);
            this.lblPort.Name = "lblPort";
            this.lblPort.Size = new System.Drawing.Size(105, 19);
            this.lblPort.TabIndex = 1;
            this.lblPort.Text = "Port";
            // 
            // lblServerAddress
            // 
            this.lblServerAddress.Location = new System.Drawing.Point(10, 28);
            this.lblServerAddress.Name = "lblServerAddress";
            this.lblServerAddress.Size = new System.Drawing.Size(105, 18);
            this.lblServerAddress.TabIndex = 0;
            this.lblServerAddress.Text = "Server Address";
            // 
            // lblConnectionStatus
            // 
            this.lblConnectionStatus.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
            this.lblConnectionStatus.ForeColor = System.Drawing.SystemColors.ControlText;
            this.lblConnectionStatus.Location = new System.Drawing.Point(336, 18);
            this.lblConnectionStatus.Name = "lblConnectionStatus";
            this.lblConnectionStatus.Size = new System.Drawing.Size(144, 19);
            this.lblConnectionStatus.TabIndex = 4;
            this.lblConnectionStatus.Text = "Connection Status:";
            // 
            // lblConnectionStatuseMsg
            // 
            this.lblConnectionStatuseMsg.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
            this.lblConnectionStatuseMsg.ForeColor = System.Drawing.SystemColors.ActiveCaption;
            this.lblConnectionStatuseMsg.Location = new System.Drawing.Point(336, 46);
            this.lblConnectionStatuseMsg.Name = "lblConnectionStatuseMsg";
            this.lblConnectionStatuseMsg.Size = new System.Drawing.Size(144, 19);
            this.lblConnectionStatuseMsg.TabIndex = 5;
            // 
            // dgTickers
            // 
            this.dgTickers.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.dgTickers.DataMember = "";
            this.dgTickers.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.dgTickers.Location = new System.Drawing.Point(38, 166);
            this.dgTickers.Name = "dgTickers";
            this.dgTickers.ReadOnly = true;
            this.dgTickers.Size = new System.Drawing.Size(903, 159);
            this.dgTickers.TabIndex = 6;
            // 
            // checkBox1
            // 
            this.checkBox1.Location = new System.Drawing.Point(19, 233);
            this.checkBox1.Name = "checkBox1";
            this.checkBox1.Size = new System.Drawing.Size(19, 19);
            this.checkBox1.TabIndex = 8;
            this.checkBox1.Visible = false;
            // 
            // checkBox2
            // 
            this.checkBox2.Location = new System.Drawing.Point(19, 210);
            this.checkBox2.Name = "checkBox2";
            this.checkBox2.Size = new System.Drawing.Size(19, 21);
            this.checkBox2.TabIndex = 9;
            this.checkBox2.Text = "checkBox2";
            this.checkBox2.Visible = false;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(339, 120);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(115, 28);
            this.button1.TabIndex = 10;
            this.button1.Text = "Updates on";
            this.button1.Click += new System.EventHandler(this.button1_Click_1);
            // 
            // btSetFilter
            // 
            this.btSetFilter.Location = new System.Drawing.Point(472, 120);
            this.btSetFilter.Name = "btSetFilter";
            this.btSetFilter.Size = new System.Drawing.Size(116, 28);
            this.btSetFilter.TabIndex = 11;
            this.btSetFilter.Text = "Set Filter";
            this.btSetFilter.Click += new System.EventHandler(this.btSetFilter_Click);
            // 
            // btClearFilters
            // 
            this.btClearFilters.Location = new System.Drawing.Point(604, 120);
            this.btClearFilters.Name = "btClearFilters";
            this.btClearFilters.Size = new System.Drawing.Size(106, 28);
            this.btClearFilters.TabIndex = 12;
            this.btClearFilters.Text = "Clear Filters";
            this.btClearFilters.UseVisualStyleBackColor = true;
            this.btClearFilters.Click += new System.EventHandler(this.btClearFilters_Click);
            // 
            // MainForm
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
            this.ClientSize = new System.Drawing.Size(960, 342);
            this.Controls.Add(this.btClearFilters);
            this.Controls.Add(this.btSetFilter);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.checkBox2);
            this.Controls.Add(this.checkBox1);
            this.Controls.Add(this.dgTickers);
            this.Controls.Add(this.lblConnectionStatuseMsg);
            this.Controls.Add(this.lblConnectionStatus);
            this.Controls.Add(this.gbConnectionSettings);
            this.Controls.Add(this.btnDisconnect);
            this.Controls.Add(this.btnConnect);
            this.Name = "MainForm";
            this.Text = "Form1";
            this.gbConnectionSettings.ResumeLayout(false);
            this.gbConnectionSettings.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dgTickers)).EndInit();
            this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
            MainForm myMainForm = new MainForm();
            Application.Run(myMainForm);
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
//			if(this.SrvrSession==null)
//			{
//				this.SrvrSession=new NiApiLib.SrvrSessionClass();
//				this.SrvrSession.OnLogin+=new _ISrvrSessionEvents_OnLoginEventHandler(SrvrSession_OnLogin);
//			}
//			
			if(this.Connected==false)
			{
				this.SrvrSession.HostAddr=this.tbServerAddress.Text;
				this.SrvrSession.HostPort=System.Convert.ToInt32(this.tbPort.Text);
				this.SrvrSession.Name=this.tbLoginName.Text;
				this.SrvrSession.Password=this.tbPassword.Text;

                this.SrvrSession.Journal = "testcom3";


				this.SrvrSession.Connect();
						
				if(this.tblFev==null)
				{
					this.tblFev= new TableClass();
					this.tblFev.OnData+=new _ITableEvents_OnDataEventHandler(tblFev_OnData);
					this.tblFev.OnInsert += new _ITableEvents_OnInsertEventHandler(tblFev_OnModify);
				}
				this.lblConnectionStatuseMsg.Text="Connected";
				this.Connected=true;
			}
			
		}

		private void SrvrSession_OnMessage(object Msg)
		{
		}

		private void SrvrSession_OnLogin(int LoginResult)
		{
			this.lblConnectionStatuseMsg.Text="Logged";
		
			this.tblFev.Open(this.SrvrSession, "all_trades", NiApiLib.ETableType.tabNoneAddr);
			this.lblConnectionStatuseMsg.Text="Table Opened";
					
			this.myStructure=(NiApiLib.Structure)this.tblFev.Structure;
		
			this.myFiltr.Structure=this.tblFev.Structure;


			//object filter = null;
			this.tblFev.GetSlice(this.myFiltr);
		
            

			
			/*
			string s1="I_NAME";
			string s2="RUR";
			string s3="CNY";
			string s4="GBP";
			string s5="CAD";

			object o1=(object)s1;
			object o2=(object)s2;
			object o3=(object)s3;
			object o4=(object)s4;
			object o5=(object)s5;
	
			this.myFiltr.Add(ref o1, NiApiLib.ECondition.conEqual,ref o3);
			this.myFiltr.Add(ref o1, NiApiLib.ECondition.conEqual,ref o2);
			this.myFiltr.Add(ref o1, NiApiLib.ECondition.conEqual,ref o4);
			this.myFiltr.Add(ref o1, NiApiLib.ECondition.conEqual,ref o5);
					
				
			this.tblFev.GetSlice(this.myFiltr);
			 */
            


		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			this.SrvrSession.Disconnect();
			this.lblConnectionStatuseMsg.Text="Disconnected";
			
	//		this.SrvrSession=null;
			this.tblFev=null;
			this.myDataSet.Clear();
			this.Connected=false;
	


			
		}


		private void tblFev_OnData(int Code, object DataRowSet, string TableName)
		{
			myDataRowSet=(NiApiLib.DataRowSet)DataRowSet;

			NiApiLib.DataRow myrow=new NiApiLib.DataRowClass();
			object o=new object();
			int k=1;
			o=(object)k;
			myrow=(NiApiLib.DataRow)myDataRowSet.GetRow(0);

			for(int i=0;i<myDataRowSet.Count; i++)
			{				
				myrow=(NiApiLib.DataRow)myDataRowSet.GetRow(i);
			}

			dtTickers= new DataTable("Tickers");
	
			for(int i=0;i<myrow.Count;i++)
			{
				dtTickers.Columns.Add();
				dtTickers.Columns[i].ColumnName=this.myStructure.FieldDispName(i);
			}
			
		
			
			string [] rowArray = new string[myrow.Count];
						
			checkboxArray= new CheckBox[myDataRowSet.Count];

            this.tickersNameArray = new string[myDataRowSet.Count];

		
			for(int i=0;i<myDataRowSet.Count;i++)
			{	
				for(int j=0;j<myrow.Count;j++)
				{
					myrow=(NiApiLib.DataRow)myDataRowSet.GetRow(i);
					int t=j;
					o=(object)t;
					rowArray[j]=myrow.get_Item(ref o).ToString();

                    if(j==1)
                    {
                        this.tickersNameArray[i] = myrow.get_Item(ref o).ToString();
                    }
				}
				/*
								checkboxArray[i]=new CheckBox();
								checkboxArray[i].Location=new System.Drawing.Point(16,182+i*17);
								checkboxArray[i].Name="checkBox"+i.ToString();
								checkboxArray[i].Size=new System.Drawing.Size(16, 24);
								checkboxArray[i].TabIndex=10+i;
								checkboxArray[i].Text="";
								checkboxArray[i].Visible=true;

								this.Controls.Add(checkboxArray[i]);
				*/
				System.Data.DataRow row;
				row=dtTickers.NewRow();
				row.ItemArray=rowArray;
				dtTickers.Rows.Add(row);
			
			}


			myDataSet = new DataSet("myDataSet");
			myDataSet.Tables.Add(dtTickers);
					
			
			this.dgTickers.DataSource=this.myDataSet;
			this.dgTickers.DataMember="Tickers";
			

		}

		private void tblFev_OnDataComplete(int Code, string TableName)
		{
            //object fieldNum = (int)1;
            //object value    = (string)"ËÓÊÎÉË";
            //this.tblFev.AddUpdateFilter(0, ELogicalOperation.logOr, ref fieldNum, ECondition.conEqual, ref value);
            //value    = (string)"ÐÀÎ ÅÝÑ";
            //this.tblFev.AddUpdateFilter(0, ELogicalOperation.logOr, ref fieldNum, ECondition.conEqual, ref value);
		}


		private void button1_Click_1(object sender, System.EventArgs e)
		{
			if(this.tblFev!=null)
			{
				if(this.UpdatesOn==false)
				{
					this.tblFev.SetOnlineUpdates(NiApiLib.ESubscribtion.subEnabled);
					this.UpdatesOn=true;
                    this.button1.Text = "Updates off";
				}
				else
				{
					this.tblFev.SetOnlineUpdates(NiApiLib.ESubscribtion.subDisabled);
					this.UpdatesOn=false;
                    this.button1.Text = "Updates on";
				}
			}
		}

	
		void tblFev_OnModify(object NiDataRow, string TableName)
		{
			NiApiLib.DataRow niRow = (NiApiLib.DataRow)NiDataRow;
			string[] array=new string[niRow.Count];
	
			for(int k=0;k<this.dtTickers.Rows.Count;k++)
			{
				
				object o = (int)1;
				object key = niRow.get_Item(ref o);
				if ( this.dtTickers.Rows[k].ItemArray[1].ToString()==key.ToString() )
				{
					for (int j = 0; j < niRow.Count; j++)
					{
						o = j;
						array[j] = niRow.get_Item(ref o).ToString();
                  	}
		
					this.dtTickers.Rows[k].ItemArray=array;
					break;
				}
			}
			
			this.myDataSet.AcceptChanges();

		}

        private void btSetFilter_Click(object sender, EventArgs e)
        {
            SetFilterForm myForm = new SetFilterForm();
            myForm.tickersArray = new string[this.tickersNameArray.Length];
            for (int i = 0; i < this.tickersNameArray.Length; i++)
            {
                myForm.tickersArray[i] = this.tickersNameArray[i];
            }

      
            myForm.ShowDialog();

            this.selectedTickersArray = new string[myForm.selectedTickersArrey.Length];

            object fieldNum = (int)1;
           
            for (int i = 0; i < myForm.selectedTickersArrey.Length; i++)
            {
                object value = (string)myForm.selectedTickersArrey[i];
                this.tblFev.AddUpdateFilter(0, ELogicalOperation.logOr, ref fieldNum, ECondition.conEqual, ref value);
            }
                
          
        }


        private void btClearFilters_Click(object sender, EventArgs e)
        {
            this.tblFev.DeleteUpdateFilter(0);
        }
	}
}
