using RAYLASE.SPICE3.ClientLib;
using System;

namespace CallBack
{
	class Program
	{
		// insert the IP of your SP-ICE-3
		readonly string _CardIp = "192.168.2.125";
		ClientAPI _Client = new ClientAPI();
		DateTime _Start;
		int _Counter = 0;

		static void Main( string[] args )
		{
			Program p = new Program();

			p._Client.Connect( p._CardIp );
			// Reset the card to its default settings:
			// This restores the following settings to the values that were last saved on the card:
			// - scanner configuration (such as field size, field correction, etc.)
			// - laser configuration (such as gate/LM timing parameters, power target, etc.)
			// - process variables (such as jump speed, settling times, mark speed, etc.)
			p._Client.System.ResetToDefaults();

			// register listdone callback
			p._Client.List.RegisterListDoneCallback( ( ClientAPI api, int x ) => p.listDone( p._Client, x ) );

			p._Start = DateTime.Now;

			p.markSquare( 0 );
		}

		/// <summary>
		/// callback function for the list done event
		/// unregister after the 10 itteration
		/// restart list
		/// </summary>
		/// <param name="listId"></param>
		void listDone( ClientAPI api, int listId )
		{
			// unregister callback after 10 itterations
			if ( ++_Counter == 10 )
			{
				api.List.UnregisterListDoneCallback( ( ClientAPI a, int x ) => listDone( api, x ) );
				DateTime end = DateTime.Now;

				TimeSpan duration = _Start - end;
			}
			else
				markSquare( listId );
		}

		void markSquare( int listId )
		{
			CommandList list = new CommandList();
			list.AppendJumpSpeed( 5 );
			list.AppendJumpDelay( 0 );
			list.AppendMarkSpeed( 0.2 );
			list.AppendMarkDelay( 0 );
			list.AppendPolyDelay( 0 );
			list.AppendLmFrequency( 1.0 / 50 );
			list.AppendLmWidth( 35 );

			list.AppendJumpAbs( -10000, -10000 );
			list.AppendMarkAbs( 10000, -10000 );
			list.AppendMarkAbs( 10000, 10000 );
			list.AppendMarkAbs( -10000, 10000 );
			list.AppendMarkAbs( -10000, -10000 );

			_Client.List.Set( listId, list );
			_Client.List.Execute( listId );
		}
	}
}
