using iq2dev.com.iqCommLib.Comm.FirmwareUpdate;
using iq2dev.com.IQCommLib.BLOB;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace iq2dev.com.IQCommLib.Example
{
    class Program
    {
		static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

#if false
			BLOB_ReadWrite_Example();
#endif

			FirmwareUpdate_Example();
		}

		const int DATA_BUFFER_SIZE = 256;
		byte[] buffer = new byte[DATA_BUFFER_SIZE];
		const Int16 READ_BLOB_ID = -4096;

		static ProtocolCommunicator Connect()
		{
			IQComport comPort = new IQComport();
			ProtocolCommunicator protocolCommunicator = new ProtocolCommunicator(comPort);
			var connectedString = comPort.Connect();
			Console.WriteLine($"iqInterface/iqLink connected to: {connectedString}");
			List<byte> pdIn = new List<byte>();
			IQMstStatus status = protocolCommunicator.getMstStatus(pdIn);
			protocolCommunicator.setOperMode(IQMstMode.Auto);
			status = protocolCommunicator.getMstStatus(pdIn);

			Console.WriteLine($"Status: state={status.state}, " +
												   $"pdOutValid={!status.isPDOutInvalid}, " +
												   $"pdInValid={!status.isPDInInvalid}, " +
												   $"event={status.isEvent}, " +
												   $"ODactive={status.isODActive}, " +
												   $"newPdOut={status.isNewPDOutSet}, " +
												   $"pdIn={string.Join(":", pdIn)}");

			List<byte> vendorNameByteList = protocolCommunicator.ReadOD(0x10, 0);

			string vendorName = Encoding.UTF8.GetString(vendorNameByteList.ToArray(), 0, vendorNameByteList.Count);
			Console.WriteLine($"Vendor Name: {vendorName}");

			return protocolCommunicator;
		}
#if false
		static void BLOB_ReadWrite_Example()
		{
			var protocolCommunicator = Connect();

			var blob = new Blob(protocolCommunicator);
			blob.LoggingEvent += LoggingEvent;

			var blobData = blob.ReadBlob(READ_BLOB_ID);

			Console.WriteLine($"BLOB ID {READ_BLOB_ID}, \n Data {string.Join(":", blobData.ToArray()):2X} \n");

			blobData.Reverse();

			blob.WriteBlob(-READ_BLOB_ID, blobData);

			//validate
			var blobDataChanged = blob.ReadBlob(READ_BLOB_ID);

			if (blobDataChanged.SequenceEqual(blobData) == false)
			{
				throw new Exception($"Writing BLOB failed. they are not equal: \n" +
					$"written: {string.Join(":", blobData.ToArray()):2X}" +
					$"actual: {string.Join(":", blobDataChanged.ToArray()):2X}");
			}
			blob.LoggingEvent -= LoggingEvent;
		}
#endif
		private static void LoggingEvent(string message)
		{
			Console.WriteLine($"{message}");
		}
#if false
		static void FirmwareUpdate_Example()
		{
			Stopwatch timer = new Stopwatch();
			timer.Start();

			var protocolCommunicator = Connect();

			var firmwareUpdate = new FirmwareUpdate(protocolCommunicator);

			firmwareUpdate.LoggingEvent += LoggingEvent;

			var fileBuffer = File.ReadAllBytes(@"c:\temp\IQ2 Development-V1.1.3.0EMULATE_MAXREFDES42-20190307-IOLFW1.0\fw_application_binary.bin");

			firmwareUpdate.Update("BOOTLOAD_FW", "1234", fileBuffer.ToList(), 3);

			timer.Stop();
			LoggingEvent($"Time: {timer.Elapsed.TotalSeconds} seconds");
		}
#endif		
	}
}
