using System;
using MrEModuleSdk.Devices.Mre2;

namespace Mre2Example
{
    internal class Example
    {
        static void Main()
        {
            Console.WriteLine("Starting basic MR-E-2 example...");

            // Customers should work with the SdkComDeviceController classes from MrEModuleSdk.dll.
            var controller = new MrE2SdkComDeviceController();

            try
            {
                Console.Write("Enter the COM port number of the MR-E-2: ");
                var comPortText = Console.ReadLine();
                if (!int.TryParse(comPortText, out var comPort))
                {
                    Console.WriteLine("Invalid COM port number.");
                    return;
                }

                if (!controller.Connect(comPort))
                {
                    throw new InvalidOperationException("Connection failed.");
                }

                Console.WriteLine("Connection successful.");

                // This is the basic pattern: connect, read values that matter, then disconnect safely.
                controller.ChangeModeToPro();

                Console.WriteLine();
                Console.WriteLine("Board information");
                Console.WriteLine($"Mirror serial number: {controller.GetProductSerialNumber()}");
                Console.WriteLine(
                    $"Board firmware version: {controller.Status.GetBoardFirmwareMajorVersion()}." +
                    $"{controller.Status.GetBoardFirmwareMinorVersion()}." +
                    $"{controller.Status.GetBoardFirmwareRevisionVersion()}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Example failed: {ex.Message}");
            }
            finally
            {
                controller.Disconnect(true);
                Console.WriteLine();
                Console.WriteLine("Example finished. Press Enter to close.");
                Console.ReadLine();
            }
        }
    }
}
