using RAYLASE.SPICE3;
using RAYLASE.SPICE3.ClientLib;
using System.Collections.Generic;

namespace DashedLine
{
	internal class Program
	{
		// insert the IP of your SP-ICE-3
		private readonly string _cardIp = "192.168.2.138";

		private ClientAPI _Client = new ClientAPI();

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

			p._Client.Connect( p._cardIp );
			p._Client.EnableLog( "log" );
			// Reset the card to its default settings:
			// This restores all settings to the values that were last saved on the card.
			p._Client.System.ResetToDefaults();
			p._Client.Laser.ArmLaser( true );
			LaserConfig lc = p._Client.Laser.GetConfig();
			lc.PowerScale = 1.0;
			p._Client.Laser.SetConfig( lc );

			p.markDashedPattern();
		}

		/// <summary>
		/// mark two rectangles with dashed line enabled
		/// </summary>
		private void markDashedPattern()
		{
			double jumpSpeed = 4;
			double markSpeed = 1;

			int listID = 143;

			ScannerConfig scanner = _Client.Scanner.GetConfig();
			double fieldSize = scanner.FieldSize.X;
			double sizeX = 0.49 * fieldSize;
			double sizeY = 0.49 * fieldSize;

			// Dashed line pattern containing distances in μm
			// Pattern will be marked as follows: 2000μm on - 1000μm off - 5000μm on - 3000μm off
			// and will be repeated while marking until dashed line is disabled
			List<double> dashedPattern = new List<double>() { 2000, 1000, 5000, 3000 };

			CommandList list = new CommandList();
			list.AppendJumpSpeed( jumpSpeed );
			list.AppendJumpDelay( 0 );
			list.AppendMarkSpeed( markSpeed );
			list.AppendPolyDelay( 0 );
			list.AppendMarkDelay( 0 );
			list.AppendPower( 32768 );
			// Append the dashed pattern to the list
			list.AppendDashedLinePattern( dashedPattern );
			// Enable dashed line
			list.AppendEnableDashedLine( true, 0 );
			// Mark a large rectangle with dashed line enabled
			list.AppendJumpAbs( -sizeX, -sizeY );
			list.AppendMarkAbs( -sizeX, sizeY );
			list.AppendMarkAbs( sizeX, sizeY );
			list.AppendMarkAbs( sizeX, -sizeY );
			list.AppendMarkAbs( -sizeX, -sizeY );

			// Mark a small rectangle with dashed line enabled
			// Set the dashed pattern position to 0
			list.AppendEnableDashedLine( true, 0 );
			list.AppendJumpAbs( -0.5 * sizeX, -0.5 * sizeY );
			list.AppendMarkAbs( -0.5 * sizeX, 0.5 * sizeY );
			list.AppendMarkAbs( 0.5 * sizeX, 0.5 * sizeY );
			list.AppendMarkAbs( 0.5 * sizeX, -0.5 * sizeY );
			list.AppendMarkAbs( -0.5 * sizeX, -0.5 * sizeY );

			_Client.List.Set( listID, list );
			_Client.List.Execute( listID );
			_Client.List.WaitForListDone( out int? doneID, System.Threading.Timeout.Infinite );

			_Client.List.Delete( listID );
		}
	}
}
