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

namespace LKCSharpSampleMSR
{
	/// <summary>
	/// Form1에 대한 요약 설명입니다.
	/// </summary>

	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button btnOpen;
		private System.Windows.Forms.Button btnClose;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button btnExit;
		private System.Windows.Forms.Button btnMSRRead;
		private System.Windows.Forms.Button btnMSRCancel;
		private System.Windows.Forms.MainMenu mainMenu1;

		private bool m_bMode;
		private System.Windows.Forms.Timer timer1;
		private System.Windows.Forms.TextBox txtMSRData1;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox txtMSRData2;
		private System.Windows.Forms.TextBox txtMSRData3;
		private System.Windows.Forms.CheckBox checkBox2;
		private System.Windows.Forms.CheckBox checkBox1;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.CheckBox m_bTrackCheck1;
		private System.Windows.Forms.CheckBox m_bTrackCheck3;
		private System.Windows.Forms.CheckBox m_bTrackCheck2;
		private System.Windows.Forms.TextBox txtIP;

		public Form1()
		{
			//
			// Windows Form 디자이너 지원에 필요합니다.
			//
			InitializeComponent();

			//
			// TODO: InitializeComponent를 호출한 다음 생성자 코드를 추가합니다.
			//
	        txtIP.Text = "192.168.10.200";

			btnClose.Enabled = false;
			btnMSRRead.Enabled = false;
			btnMSRCancel.Enabled = false;

			m_bTrackCheck1.Checked = true;
			m_bTrackCheck2.Checked = true;
			m_bTrackCheck3.Checked = false;

			timer1 = new Timer();

		}
		/// <summary>
		/// 사용 중인 모든 리소스를 정리합니다.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}
		#region Windows Form 디자이너에서 생성한 코드
		/// <summary>
		/// 디자이너 지원에 필요한 메서드입니다.
		/// 이 메서드의 내용을 코드 편집기로 수정하지 마십시오.
		/// </summary>
		private void InitializeComponent()
		{
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.btnOpen = new System.Windows.Forms.Button();
			this.btnClose = new System.Windows.Forms.Button();
			this.btnMSRRead = new System.Windows.Forms.Button();
			this.btnMSRCancel = new System.Windows.Forms.Button();
			this.label2 = new System.Windows.Forms.Label();
			this.txtMSRData1 = new System.Windows.Forms.TextBox();
			this.btnExit = new System.Windows.Forms.Button();
			this.timer1 = new System.Windows.Forms.Timer();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.txtMSRData2 = new System.Windows.Forms.TextBox();
			this.txtMSRData3 = new System.Windows.Forms.TextBox();
			this.checkBox2 = new System.Windows.Forms.CheckBox();
			this.checkBox1 = new System.Windows.Forms.CheckBox();
			this.panel1 = new System.Windows.Forms.Panel();
			this.m_bTrackCheck1 = new System.Windows.Forms.CheckBox();
			this.m_bTrackCheck3 = new System.Windows.Forms.CheckBox();
			this.m_bTrackCheck2 = new System.Windows.Forms.CheckBox();
			this.txtIP = new System.Windows.Forms.TextBox();
			// 
			// btnOpen
			// 
			this.btnOpen.Location = new System.Drawing.Point(144, 10);
			this.btnOpen.Size = new System.Drawing.Size(88, 24);
			this.btnOpen.Text = "Connect";
			this.btnOpen.Click += new System.EventHandler(this.btnOpen_Click);
			// 
			// btnClose
			// 
			this.btnClose.Location = new System.Drawing.Point(144, 40);
			this.btnClose.Size = new System.Drawing.Size(88, 24);
			this.btnClose.Text = "Disconnect";
			this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
			// 
			// btnMSRRead
			// 
			this.btnMSRRead.Location = new System.Drawing.Point(144, 88);
			this.btnMSRRead.Size = new System.Drawing.Size(88, 24);
			this.btnMSRRead.Text = "MSR Read";
			this.btnMSRRead.Click += new System.EventHandler(this.btnMSRRead_Click);
			// 
			// btnMSRCancel
			// 
			this.btnMSRCancel.Location = new System.Drawing.Point(144, 120);
			this.btnMSRCancel.Size = new System.Drawing.Size(88, 24);
			this.btnMSRCancel.Text = "MSR Cancel";
			this.btnMSRCancel.Click += new System.EventHandler(this.btnMSRCancel_Click);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 144);
			this.label2.Size = new System.Drawing.Size(72, 12);
			this.label2.Text = "MSR Data1";
			// 
			// txtMSRData1
			// 
			this.txtMSRData1.Location = new System.Drawing.Point(16, 160);
			this.txtMSRData1.Size = new System.Drawing.Size(216, 21);
			this.txtMSRData1.Text = "";
			// 
			// btnExit
			// 
			this.btnExit.Location = new System.Drawing.Point(16, 264);
			this.btnExit.Size = new System.Drawing.Size(216, 24);
			this.btnExit.Text = "Exit";
			this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(16, 184);
			this.label3.Size = new System.Drawing.Size(72, 12);
			this.label3.Text = "MSR Data2";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(16, 218);
			this.label4.Size = new System.Drawing.Size(72, 12);
			this.label4.Text = "MSR Data3";
			// 
			// txtMSRData2
			// 
			this.txtMSRData2.Location = new System.Drawing.Point(16, 196);
			this.txtMSRData2.Size = new System.Drawing.Size(216, 21);
			this.txtMSRData2.Text = "";
			// 
			// txtMSRData3
			// 
			this.txtMSRData3.Location = new System.Drawing.Point(16, 232);
			this.txtMSRData3.Size = new System.Drawing.Size(216, 21);
			this.txtMSRData3.Text = "";
			// 
			// checkBox2
			// 
			this.checkBox2.Font = new System.Drawing.Font("굴림", 8.25F, System.Drawing.FontStyle.Regular);
			this.checkBox2.Location = new System.Drawing.Point(8, 24);
			this.checkBox2.Size = new System.Drawing.Size(96, 18);
			this.checkBox2.Text = "MSR Read";
			// 
			// checkBox1
			// 
			this.checkBox1.Location = new System.Drawing.Point(8, 8);
			this.checkBox1.Size = new System.Drawing.Size(96, 18);
			this.checkBox1.Text = "Printer Error";
			// 
			// panel1
			// 
			this.panel1.BackColor = System.Drawing.Color.White;
			this.panel1.Controls.Add(this.checkBox2);
			this.panel1.Controls.Add(this.checkBox1);
			this.panel1.Location = new System.Drawing.Point(16, 40);
			this.panel1.Size = new System.Drawing.Size(112, 48);
			// 
			// m_bTrackCheck1
			// 
			this.m_bTrackCheck1.Location = new System.Drawing.Point(16, 96);
			this.m_bTrackCheck1.Size = new System.Drawing.Size(60, 18);
			this.m_bTrackCheck1.Text = "Track1";
			this.m_bTrackCheck1.CheckStateChanged += new System.EventHandler(this.m_bTrackCheck1_CheckStateChanged);
			// 
			// m_bTrackCheck3
			// 
			this.m_bTrackCheck3.Location = new System.Drawing.Point(16, 120);
			this.m_bTrackCheck3.Size = new System.Drawing.Size(60, 18);
			this.m_bTrackCheck3.Text = "Track3";
			this.m_bTrackCheck3.CheckStateChanged += new System.EventHandler(this.m_bTrackCheck3_CheckStateChanged);
			// 
			// m_bTrackCheck2
			// 
			this.m_bTrackCheck2.Location = new System.Drawing.Point(80, 96);
			this.m_bTrackCheck2.Size = new System.Drawing.Size(60, 18);
			this.m_bTrackCheck2.Text = "Track2";
			this.m_bTrackCheck2.CheckStateChanged += new System.EventHandler(this.m_bTrackCheck2_CheckStateChanged);
			// 
			// txtIP
			// 
			this.txtIP.Location = new System.Drawing.Point(16, 12);
			this.txtIP.Size = new System.Drawing.Size(112, 21);
			this.txtIP.Text = "";
			// 
			// Form1
			// 
			this.ClientSize = new System.Drawing.Size(242, 296);
			this.Controls.Add(this.txtIP);
			this.Controls.Add(this.m_bTrackCheck2);
			this.Controls.Add(this.m_bTrackCheck3);
			this.Controls.Add(this.m_bTrackCheck1);
			this.Controls.Add(this.txtMSRData3);
			this.Controls.Add(this.txtMSRData2);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.btnExit);
			this.Controls.Add(this.txtMSRData1);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.btnMSRCancel);
			this.Controls.Add(this.btnMSRRead);
			this.Controls.Add(this.panel1);
			this.Controls.Add(this.btnClose);
			this.Controls.Add(this.btnOpen);
			this.Menu = this.mainMenu1;
			this.Text = "Form1";

		}
		#endregion

		/// <summary>
		/// 해당 응용 프로그램의 주 진입점입니다.
		/// </summary>

		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void btnOpen_Click(object sender, System.EventArgs e)
		{
			long lResult;
			
	        lResult = LKPrint.OpenPort(txtIP.Text, 9100);
			if (lResult != 0)
			{
				MessageBox.Show("Open Port Failed");
				return;
			}

			btnOpen.Enabled = false;
			btnClose.Enabled = true;
			btnMSRRead.Enabled = true;
			btnMSRCancel.Enabled = true;
			btnExit.Enabled = false;
		}

		private void btnClose_Click(object sender, System.EventArgs e)
		{
			long lResult = LKPrint.ClosePort();
			if (lResult != 0)
			{
				MessageBox.Show("Close Port Failed!!!");
				return;
			}

			btnOpen.Enabled = true;
			btnClose.Enabled = false;
			btnMSRRead.Enabled = false;
			btnMSRCancel.Enabled = false;
			btnExit.Enabled = true;
		}

		// Received Ticks by callback
		private void timer_tick(object sender,EventArgs eArgs)
		{
			long lResult;
			int lState;
		
			timer1.Enabled = false;
			byte []	MSRData = new byte[256];
			byte []	DataTrack1 = new byte[256];
			byte []	DataTrack2 = new byte[256];
			byte []	DataTrack3 = new byte[256];
			long	status;
			string	Data;

			status = LKPrint.MSRReadTrack(DataTrack1, DataTrack2, DataTrack3);

			switch(status)
			{
				case 0:
					if(!m_bMode)
					{
						System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
						Data = enc.GetString(DataTrack1, 0, 256);
						txtMSRData1.Text = Data;
						Data = enc.GetString(DataTrack2, 0, 256);
						txtMSRData2.Text = Data;
						Data = enc.GetString(DataTrack3, 0, 256);
						txtMSRData3.Text = Data;
						m_bMode = true;
						checkBox2.Checked = false;

						timer1.Enabled = false;
						btnMSRRead.Enabled = true;
						btnMSRCancel.Enabled = false;
						LKPrint.MSRReadCancel();
					} 
					else 
					{
						txtMSRData1.Text = "Cancel to MSR Read";
					}		
					return;
				case 1:
					if(!m_bMode)
					{
						txtMSRData1.Text = "Check-Sum Error...";		
						timer1.Interval = 1000;
						timer1.Enabled = true;
					} 
					else 
					{
						txtMSRData1.Text = "Cancel to MSR Read";
					}		
					return;		
				case 2:
					if(!m_bMode)
					{
						txtMSRData1.Text = "Timeout Error...";		
						txtMSRData2.Text = "Timeout Error...";		
						txtMSRData3.Text = "Timeout Error...";		
						timer1.Interval = 1000;
						timer1.Enabled = true;
					} 
					else 
					{
						txtMSRData1.Text = "Cancel to MSR Read";
					}		
					return;
			}		
		}

		
		private void btnMSRRead_Click(object sender, System.EventArgs e)
		{
			long lResult;
			Int32	lTrack;

			lTrack = 0;

			if(m_bTrackCheck1.Checked) lTrack = lTrack | 0x01;
			if(m_bTrackCheck2.Checked) lTrack = lTrack | 0x02;
			if(m_bTrackCheck3.Checked) lTrack = lTrack | 0x04;


			LKPrint.MSRSelectTrack(lTrack);


			lResult = LKPrint.PrinterCheck();
			switch(lResult)
			{
				case -1: // Bluetooth Write Fail. Please Check Connection.
					MessageBox.Show("Write Error"); 
					return;
				case -2: 
					MessageBox.Show("Fail to Read Printer Status");
					return;
			}

			lResult = LKPrint.status();


			if( (lResult & LKPrint.LK_STS_PRINTEROFF)	== LKPrint.LK_STS_PRINTEROFF)
			{
				checkBox1.Checked = true;
			}

			if( (lResult & LKPrint.LK_STS_MSR_READ)	== LKPrint.LK_STS_MSR_READ)
			{
				checkBox2.Checked = true;
			}

			if( (lResult & LKPrint.LK_STS_PAPER_EMPTY)	== LKPrint.LK_STS_PAPER_EMPTY)
			{
				checkBox1.Checked = true;
			}	

			if( (lResult & LKPrint.LK_STS_COVER_OPEN)	== LKPrint.LK_STS_COVER_OPEN)
			{
				checkBox1.Checked = true;
			}

			if( (lResult & LKPrint.LK_STS_BATTERY_LOW)	== LKPrint.LK_STS_BATTERY_LOW)
			{
				checkBox1.Checked = true;
			}


			if((lResult & LKPrint.LK_STS_MSR_READ) != 0x40) 
			{
				txtMSRData1.Text = "MSR Not Read Mode";
				return;
			}

			// Stop CallBack
			timer1.Enabled = false;
			m_bMode = false;
			btnMSRRead.Enabled = false;
			btnMSRCancel.Enabled = true;
			timer1.Interval = 1000;
			timer1.Enabled = true;
			timer1.Tick+=new EventHandler(timer_tick);

		}

		private void btnMSRCancel_Click(object sender, System.EventArgs e)
		{
			m_bMode = true;
			checkBox2.Checked = false;
			// Stop CallBack
			timer1.Enabled = false;

			txtMSRData1.Text = "- Cancel to MSR Read";				
			btnMSRRead.Enabled = true;
			LKPrint.MSRReadCancel();
		}

		private void btnExit_Click(object sender, System.EventArgs e)
		{
			this.Dispose(true);
		}

		private void m_bTrackCheck1_CheckStateChanged(object sender, System.EventArgs e)
		{
			bool	bchecked;

			bchecked = m_bTrackCheck1.Checked;
			if(bchecked)
			{
				if(!m_bTrackCheck3.Checked)
				{
					m_bTrackCheck1.Checked = true;
				} 
				else 
				{
					MessageBox.Show("Track1 and Track3 Setting's Error", "ERROR");
					m_bTrackCheck1.Checked = false;
				}
			}
			else
			{
				m_bTrackCheck1.Checked = false;
			}
		}

		private void m_bTrackCheck2_CheckStateChanged(object sender, System.EventArgs e)
		{
			bool	bchecked;

			bchecked = m_bTrackCheck2.Checked;
			if(bchecked)
			{
				m_bTrackCheck2.Checked = true;
			}
			else
			{
				m_bTrackCheck2.Checked = false;
			}
		}

		private void m_bTrackCheck3_CheckStateChanged(object sender, System.EventArgs e)
		{
			bool	bchecked;

			bchecked = m_bTrackCheck3.Checked;
			if(bchecked)
			{
				if(!m_bTrackCheck1.Checked)
				{
					m_bTrackCheck3.Checked = true;
				} 
				else 
				{
					MessageBox.Show("Track1 and Track3 Setting's Error", "ERROR");
					m_bTrackCheck3.Checked = false;
				}
			}
			else
			{
				m_bTrackCheck3.Checked = false;
			}
		}
	}
}
