using Ion.Sdk.Ddr;
using Ion.Sdk.Ici;
using Ion.Sdk.Idi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ion.Sdk.DemoApp.Examples
{
    public static class BlackBox_SingleTest_TouchTest
    {
        private static async Task Main_BlackBox_SingleTest_TouchTest_Entry(string[] args)
        {
            const string port = "COM7";
            const string blackBoxPassword = "";

            try
            {
                await BlackBox.StartSingleTest(port, blackBoxPassword);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }

        public static class BlackBox
        {
            private static IBlackBoxChannelTaskSource blackBox = null;

            public static async Task StartSingleTest(string port, string blackBoxPassword)
            {
                var region = DataDisplayInfos.RegionInfoList.Get("EU_Installations").ToRegion();
                var measurementInfo = DataDisplayInfos.MeasurementInfoList.Get("AUTO_IT_INST");

                using (Channel.BlackBox.Get<Channel.BlackBox.Message.TouchTest>().Subscribe(_ => OnTouchTest()))
                using (var channel = Channel.Serial.Open(port))
                using (blackBox = Channel.BlackBox.Create(channel, blackBoxPassword: blackBoxPassword))
                {
                    var test = new SingleTest(new EmptyMeasurement(measurementInfo.Id, region));
                    var measuredTest = await blackBox.Start(test, blackBox.Stop);
                }

            }

            private static void OnTouchTest()
            {
                // touch pre-test notification

                // continue with single test   
                blackBox.ExecuteAction(Ion.Sdk.Ici.BlackBox.Predefined.Actions.StartTest);

                // break single test
                //blackBox.ExecuteAction(Actions.Break);
            }
        }
    }
}

