using iq2dev.com.iqCommLib;
#if false
using iq2dev.com.iqCommLib.Comm.FirmwareUpdate;
using iq2dev.com.IQCommLib.BLOB;
#endif
using iqCommLib.Example;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;

namespace iq2dev.com.IQCommLib.Example
{
    class Program
    {
		enum ConnectionType
        {
			Serial,

			Ethernet,
        }

		static void Main(string[] args)
        {
            Console.WriteLine("IQCommLib.Example");

#if false
			//ethernet communication
			var ethernetComm = new IQEthernet("169.254.34.44", 10001);
			//var ethernetComm = new IQEthernet("192.168.1.200", 10001);
			var communicator = Connect(ethernetComm);
			//ethernetComm.Disconnect();

#else
			//serial communication
			IQComport comPort = new IQComport();
			var communicator = Connect(comPort);
			//comPort.Disconnect();
#endif

#if false
			BlobExample.BLOB_ReadWrite_Example(communicator);
#endif

#if false
			FwUpdateExample.FirmwareUpdate_Example(communicator);
#endif

#if false
			//ISDU fast frocessing supported only from version 1.2.3.0
			FastProcessingExample.FastProcessingIsdu_Example(communicator);
#endif

#if false
			EmcTesterExample.EmcDeviceTester_Example(communicator);
#endif

#if false //only for internal use
			ReadDiagnostic_Example(communicator);
#endif

		}

		static ProtocolCommunicator Connect(ITransmitComm transmitComm)
		{
			var connectedString = transmitComm.Connect();
			Console.WriteLine($"iqInterface/iqLink connected to: {connectedString}");
			ProtocolCommunicator protocolCommunicator = new ProtocolCommunicator(transmitComm);
			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;
		}

		public static void LoggingEvent(string message)
		{
			Console.WriteLine($"{message}");
		}

		static void ReadDiagnostic_Example(ProtocolCommunicator protocolCommunicator)
		{
			Stopwatch timer = new Stopwatch();
			timer.Start();

			var diagnoseInfoList = protocolCommunicator.ReadDiagnoseInformation();
			LoggingEvent($"diagnostic: {BitConverter.ToString(diagnoseInfoList.ToArray())}");

			//it is required to reboot iqInterface after using EMC test
			protocolCommunicator.writeIQCommPar(IQPar.IQParRebootDevice, new List<byte>() { 0 });

			Thread.Sleep(2000);
			diagnoseInfoList = protocolCommunicator.ReadDiagnoseInformation();
			LoggingEvent($"diagnostic: {BitConverter.ToString(diagnoseInfoList.ToArray())}");

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