using MrEModuleSdk.Devices.Mre3;
using MrEModuleSdk.Dto;
using System;

namespace Mre3Example
{
    public partial class Mre3Example
    {
        private static void PrintWelcome()
        {
            Console.WriteLine("MR-E-3 SDK Examples");
            Console.WriteLine("-------------------");
            Console.WriteLine("This console application demonstrates several common SDK tasks.");
            Console.WriteLine("Connect to a device, choose an example, and inspect the behavior.");
            Console.WriteLine();
        }

        private static int? PromptForComPort()
        {
            Console.Write("Enter the COM port number of the MR-E-3: ");
            var comPortText = Console.ReadLine();

            if (int.TryParse(comPortText, out var port))
            {
                return port;
            }

            return null;
        }

        private static void PrintConnectedDeviceSummary(IMrE3SdkComDeviceController controller)
        {
            Console.WriteLine("Connected device summary");
            Console.WriteLine($"Product serial number: {controller.GetProductSerialNumber()}");
            Console.WriteLine($"Electronic serial number: {controller.GetElectronicSerialNumber()}");
            Console.WriteLine();
        }

        private static void PrintMenu()
        {
            Console.WriteLine("Choose an example");
            Console.WriteLine("1 - Device overview");
            Console.WriteLine("2 - Static input");
            Console.WriteLine("3 - Signal generator");
            Console.WriteLine("4 - Readout values");
            Console.WriteLine("5 - Operation modes");
            Console.WriteLine("6 - Vector pattern unit trigger");
            Console.WriteLine("0 - Exit");
            Console.Write("Selection: ");
        }

        private static void WaitForEnter(string message = "Press Enter to continue.")
        {
            Console.WriteLine();
            Console.WriteLine(message);
            Console.ReadLine();
        }

        private static void StopDynamicSources(IMrE3SdkComDeviceController controller)
        {
            try
            {
                // Stop generated outputs before disconnecting so the device returns to a quiet state.
                controller.SignalFlowManager.InputStage.SignalGenerator.SwitchRunning(EAxis.X, false);
                controller.SignalFlowManager.InputStage.SignalGenerator.SwitchRunning(EAxis.Y, false);
                controller.SignalFlowManager.InputStage.ArbitraryVector.SwitchArbitraryVector(EAxis.X, false);
                controller.SignalFlowManager.InputStage.ArbitraryVector.SwitchArbitraryVector(EAxis.Y, false);
            }
            catch
            {
                // Best-effort cleanup only.
            }
        }
    }
}
