using RAYLASE.SPICE3;
using RAYLASE.SPICE3.ClientLib;
using System;

namespace Fonts
{
	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 string _Counter = "counter";

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

			p._Client.Connect( p._CardIp );
			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._Client.Variables.MarkCounter.Create( p._Counter );

			p.configureFont();

			string time;
			time = DateTime.Now.ToString( "yyyy-MM-dd HH:mm:ss" );

			p.markText( time );
			p.markDate();
			p.markCounter();
		}

		/// <summary>
		/// create Font
		/// only digits space dash and colon are defined in this example
		/// Every character is defined with relative commands from the anchor point in the lower left corner
		/// and finishes at the anchor point of the next char
		/// </summary>
		private void configureFont()
		{
			CommandList c0 = new CommandList();
			c0.AppendJumpRel( 500, 1000 );
			c0.AppendMarkRel( 0, -500 );
			c0.AppendMarkRel( 0, -500 );
			c0.AppendMarkRel( -500, 0 );
			c0.AppendMarkRel( 0, 500 );
			c0.AppendMarkRel( 0, 500 );
			c0.AppendMarkRel( 500, 0 );
			c0.AppendJumpRel( 500, -1000 );

			CommandList c1 = new CommandList();
			c1.AppendJumpRel( 500, 0 );
			c1.AppendMarkRel( 0, 1000 );
			c1.AppendJumpRel( 500, -1000 );

			CommandList c2 = new CommandList();
			c2.AppendJumpRel( 0, 1000 );
			c2.AppendMarkRel( 500, 0 );
			c2.AppendMarkRel( 0, -500 );
			c2.AppendMarkRel( -500, 0 );
			c2.AppendMarkRel( 0, -500 );
			c2.AppendMarkRel( 500, 0 );
			c2.AppendJumpRel( 500, 0 );

			CommandList c3 = new CommandList();
			c3.AppendJumpRel( 0, 1000 );
			c3.AppendMarkRel( 500, 0 );
			c3.AppendMarkRel( 0, -500 );
			c3.AppendMarkRel( -500, 0 );
			c3.AppendJumpRel( 500, 0 );
			c3.AppendMarkRel( 0, -500 );
			c3.AppendMarkRel( -500, 0 );
			c3.AppendJumpRel( 1000, 0 );

			CommandList c4 = new CommandList();
			c4.AppendJumpRel( 0, 1000 );
			c4.AppendMarkRel( 0, -500 );
			c4.AppendMarkRel( 500, 0 );
			c4.AppendJumpRel( 0, 500 );
			c4.AppendMarkRel( 0, -1000 );
			c4.AppendJumpRel( 500, 0 );

			CommandList c5 = new CommandList();
			c5.AppendJumpRel( 500, 1000 );
			c5.AppendMarkRel( -500, 0 );
			c5.AppendMarkRel( 0, -500 );
			c5.AppendMarkRel( 500, 0 );
			c5.AppendMarkRel( 0, -500 );
			c5.AppendMarkRel( -500, 0 );
			c5.AppendJumpRel( 1000, 0 );

			CommandList c6 = new CommandList();
			c6.AppendJumpRel( 500, 1000 );
			c6.AppendMarkRel( -500, 0 );
			c6.AppendMarkRel( 0, -500 );
			c6.AppendMarkRel( 0, -500 );
			c6.AppendMarkRel( 500, 0 );
			c6.AppendMarkRel( 0, 500 );
			c6.AppendMarkRel( -500, 0 );
			c6.AppendJumpRel( 1000, -500 );

			CommandList c7 = new CommandList();
			c7.AppendJumpRel( 0, 1000 );
			c7.AppendMarkRel( 500, 0 );
			c7.AppendMarkRel( 0, -500 );
			c7.AppendMarkRel( 0, -500 );
			c7.AppendJumpRel( 500, 0 );

			CommandList c8 = new CommandList();
			c8.AppendJumpRel( 500, 1000 );
			c8.AppendMarkRel( -500, 0 );
			c8.AppendMarkRel( 0, -500 );
			c8.AppendMarkRel( 500, 0 );
			c8.AppendMarkRel( 0, 500 );
			c8.AppendJumpRel( 0, -500 );
			c8.AppendMarkRel( 0, -500 );
			c8.AppendMarkRel( -500, 0 );
			c8.AppendMarkRel( 0, 500 );
			c8.AppendJumpRel( 1000, -500 );

			CommandList c9 = new CommandList();
			c9.AppendJumpRel( 500, 1000 );
			c9.AppendMarkRel( -500, 0 );
			c9.AppendMarkRel( 0, -500 );
			c9.AppendMarkRel( 500, 0 );
			c9.AppendMarkRel( 0, 500 );
			c9.AppendJumpRel( 0, -500 );
			c9.AppendMarkRel( 0, -500 );
			c9.AppendMarkRel( -500, 0 );
			c9.AppendJumpRel( 1000, 0 );

			CommandList colon = new CommandList();
			colon.AppendJumpRel( 0, 200 );
			colon.AppendMarkRel( 200, 0 );
			colon.AppendMarkRel( 0, 200 );
			colon.AppendMarkRel( -200, 0 );
			colon.AppendMarkRel( 0, -200 );
			colon.AppendJumpRel( 0, 400 );
			colon.AppendMarkRel( 200, 0 );
			colon.AppendMarkRel( 0, 200 );
			colon.AppendMarkRel( -200, 0 );
			colon.AppendMarkRel( 0, -200 );
			colon.AppendJumpRel( 500, -600 );

			CommandList space = new CommandList();
			space.AppendJumpRel( 500, 0 );

			CommandList dash = new CommandList();
			dash.AppendJumpRel( 0, 500 );
			dash.AppendMarkRel( 500, 0 );
			dash.AppendJumpRel( 500, -500 );

			Font font = new Font( 0 );
			font.AddChar( ' ', space );
			font.AddChar( '0', c0 );
			font.AddChar( '1', c1 );
			font.AddChar( '2', c2 );
			font.AddChar( '3', c3 );
			font.AddChar( '4', c4 );
			font.AddChar( '5', c5 );
			font.AddChar( '6', c6 );
			font.AddChar( '7', c7 );
			font.AddChar( '8', c8 );
			font.AddChar( '9', c9 );
			font.AddChar( ':', colon );
			font.AddChar( '-', dash );

			_Client.Font.Set( font );
			_Client.Font.SetActive( 0 );
		}

		/// <summary>
		/// Mark the specified text
		/// Every used character has to be defined in the font
		/// </summary>
		/// <param name="text">text to mark</param>
		private void markText( string text )
		{
			CommandList list = new CommandList();
			list.AppendJumpSpeed( 5 );
			list.AppendJumpDelay( 0 );
			list.AppendMarkSpeed( 0.2 );
			list.AppendMarkDelay( 0 );
			list.AppendPolyDelay( 0 );
			list.AppendLmFrequency( 1.0 / 200 );
			list.AppendLmWidth( 200 );
			list.AppendPower( 32000 );
			list.AppendScalingFactor( 2, 2 );

			list.AppendJumpAbs( -10000, -2000 );

			list.AppendMarkText( text );
			list.AppendLaserOff();

			_Client.List.Set( 0, list );
			_Client.List.Execute( 0 );

			int? id;
			_Client.List.WaitForListDone( out id );
		}

		/// <summary>
		/// mark the system time of the SPICE3 card with the font created in configureFont()
		/// </summary>
		private void markDate()
		{
			_Client.Variables.MarkCounter.MarkTimeFormat = "%H:%M:%S %Y-%m-%D";

			CommandList list = new CommandList();
			list.AppendJumpSpeed( 5 );
			list.AppendJumpDelay( 0 );
			list.AppendMarkSpeed( 0.2 );
			list.AppendMarkDelay( 0 );
			list.AppendPolyDelay( 0 );
			list.AppendLmFrequency( 1.0 / 200 );
			list.AppendLmWidth( 200 );
			list.AppendPower( 32000 );
			list.AppendScalingFactor( 2, 2 );

			list.AppendJumpAbs( -10000, 0 );

			list.AppendMarkTime();

			list.AppendLaserOff();

			_Client.List.Set( 0, list );
			_Client.List.Execute( 0 );

			int? id;
			_Client.List.WaitForListDone( out id );
		}

		/// <summary>
		/// mark counter with the font created in configureFont()
		/// </summary>
		private void markCounter()
		{
			CommandList list = new CommandList();
			list.AppendJumpSpeed( 5 );
			list.AppendJumpDelay( 0 );
			list.AppendMarkSpeed( 0.2 );
			list.AppendMarkDelay( 0 );
			list.AppendPolyDelay( 0 );
			list.AppendLmFrequency( 1.0 / 200 );
			list.AppendLmWidth( 200 );
			list.AppendPower( 32000 );
			list.AppendScalingFactor( 2, 2 );

			list.AppendJumpAbs( -10000, 2000 );

			list.AppendMarkCounter( _Counter, 2 );
			list.AppendIncrementCounter( _Counter );

			list.AppendLaserOff();

			_Client.List.Set( 0, list );
			_Client.List.Execute( 0 );

			int? id;
			_Client.List.WaitForListDone( out id );
		}
	}
}
