using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Linq;
using MercatorApi;
using MercatorExtensions;
using MercatorUi;
using MercatorDatabase;
using System.IO.Ports;
using MercatorUi.SelfScan;

// <ReferenceInclude >"System.IO.Ports.dll"</ReferenceInclude>

namespace SelfScan
{
    public class Customizer : MercatorUi.ICustomizers.ISelfScanStarted
    {
        private ManageLed manageLed;

        public void SelfScanStarted(MercatorUi.SelfScan.SelfScan selfScan)
        {
            selfScan.Disposing += SelfScan_Disposing;
            string ledPort = selfScan.ConfigurationManagerAppSettings["LedPort"];
            if (!string.IsNullOrWhiteSpace(ledPort))
                manageLed = new ManageLed(ledPort);
        }

        private void SelfScan_Disposing(object sender, EventArgs e)
        {
            manageLed?.Close();
        }

        private void SelfScan_SendingToMonitor(object sender, MercatorUi.SelfScan.SelfScan.SendingToMonitorEventArgs e)
        {
            MercatorUi.SelfScan.SelfScan selfScan = (MercatorUi.SelfScan.SelfScan)sender;
            if (manageLed != null)
            {
                switch (e.Level)
                {
                    case MercatorUi.SelfScan.SelfScan.SendingToMonitorEventArgs.LevelEnum.Inactive:
                        MercatorPos.Pos.SendToPort(manageLed.LedPort, MercatorPos.Pos.Evaluate("chr(204)+chr(218)+chr(0)+chr(0)+chr(0)+chr(0)+chr(6)+chr(0)"), "1"); //Off
                        break;
                    case MercatorUi.SelfScan.SelfScan.SendingToMonitorEventArgs.LevelEnum.Normal:
                        MercatorPos.Pos.SendToPort(manageLed.LedPort, MercatorPos.Pos.Evaluate("chr(204)+chr(218)+chr(0)+chr(255)+chr(0)+chr(0)+chr(6)+chr(0)"), "1"); //Green
                        break;
                    case MercatorUi.SelfScan.SelfScan.SendingToMonitorEventArgs.LevelEnum.Closed:
                        MercatorPos.Pos.SendToPort(manageLed.LedPort, MercatorPos.Pos.Evaluate("chr(204)+chr(218)+chr(0)+chr(0)+chr(0)+chr(0)+chr(6)+chr(0)"), "1"); //Off
                        break;
                    case MercatorUi.SelfScan.SelfScan.SendingToMonitorEventArgs.LevelEnum.Alert:
                        MercatorPos.Pos.SendToPort(manageLed.LedPort, MercatorPos.Pos.Evaluate("chr(204)+chr(218)+chr(255)+chr(0)+chr(0)+chr(0)+chr(6)+chr(0)"), "1"); //Red
                        break;
                    case MercatorUi.SelfScan.SelfScan.SendingToMonitorEventArgs.LevelEnum.Warning:
                        MercatorPos.Pos.SendToPort(manageLed.LedPort, MercatorPos.Pos.Evaluate("chr(204)+chr(218)+chr(255)+chr(128)+chr(0)+chr(0)+chr(6)+chr(0)"), "1");//Orange
                        break;
                }
            }
        }
    }

    public class ManageLed
    {
        public string LedPort { get; private set; }
        private SerialPort serialPort = null;
        
        public ManageLed(string ledPort)
        {
            LedPort = ledPort;
            try
            {
                serialPort = new SerialPort(ledPort)
                {
                    BaudRate = 9600,
                    Parity = Parity.None,
                    StopBits = StopBits.One,
                    DataBits = 8
                };
                serialPort.Open();
                if (!serialPort.IsOpen)
                    MercatorUi.Globals.ApiLogDelegate("ManageLed Init : not open !");
                else if (ledPort.StartsWith("COM", StringComparison.InvariantCultureIgnoreCase))
                    serialPort.WriteTimeout = serialPort.ReadTimeout = 1000;
            }
            catch (Exception ex)
            {
                MercatorUi.Globals.ApiLogDelegate("ManageLed Init : " + ex.ToString());
            }
            return;
        }

        public void Close()
        {
            if (serialPort != null)
            {
                serialPort.Dispose();
                serialPort = null;
            }
        }
    }
}