using System;
namespace pmsDataGridView
{
	public class BarCodeScanner
	{
		string last_scanned_code;
		
		public BarCodeScanner()
		{
			//Nothing to do;	
			System.Console.WriteLine( "Init Scanner");
			this.last_scanned_code ="";
        }
		
		public string Scan()
		{
			string data;
			System.Diagnostics.Process myproc = new System.Diagnostics.Process( );
			myproc.StartInfo.UseShellExecute = false;
			myproc.StartInfo.RedirectStandardOutput = true;
			myproc.EnableRaisingEvents = false;
			myproc.StartInfo.FileName = "/usr/bin/zbarcam";
			myproc.StartInfo.Arguments = "";
			System.Console.WriteLine( "Starting app");
			if( myproc.Start( )  )
			{
				//Using WaitForExit( ) allows for the host program
				//to wait for the command its executing before it continues
				
				data = myproc.StandardOutput.ReadLine();
				System.Console.WriteLine( data + " was returned" );
				//Cache last scanned code
				last_scanned_code = data;
				System.Media.SystemSounds.Exclamation.Play();
				//Kill scanner process after barcode is read, we just need one code
				if (!myproc.HasExited)
				{
					myproc.Kill();
				}
				//myproc.CloseMainWindow();
			
			} else {
				System.Console.WriteLine( "Unable to access WebCam");
				last_scanned_code="";
				System.Media.SystemSounds.Beep.Play();
			}
						   
			//Store last scanned code
			
			return last_scanned_code;
		}
                   
	}
}

