using EgwProxy.Shelly;
using EgwProxy.Shelly.Clients;
using EgwProxy.Shelly.Options;
using IOB_UT_NEXT;
using IOB_UT_NEXT.Config;
using MapoSDK;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.NetworkInformation;
using System.Threading;
using System.Threading.Tasks;

namespace IOB_WIN_SHELLY.Iob
{
    /// <summary>
    /// Classe gestione oggetti Shelly tipo PM1 (Gen1)
    /// </summary>
    public class ShellyClientGen1 : GenericNext
    {
        #region Public Constructors

        /// <summary>
        /// Estende l'init della classe base, impiegando il pacchetto EgwProxy.Shelly
        /// - gestione dei task da svolgere da configurazione json specifica
        /// - specializzazione da conf e non da codice
        /// </summary>
        /// <param name="caller">Form chiamante</param>
        /// <param name="IobConfFull">Configurazione (v 4.x)</param>
        public ShellyClientGen1(AdapterFormNext caller, IobConfTree IobConfFull) : base(caller, IobConfFull)
        {
            lgInfo("Init Iob.ShellyClientGen1");
            // imposto ping a 3
            maxQueuePing = 3;
            // imposto
            B_input = 0;
            // init datetime counters
            DateTime adesso = DateTime.Now;
            lastPzCountSend = adesso;
            lastWarnODL = adesso;
            vetoCheckStatus = adesso;
            // 2023.09.05 imposto anche primo ping e check disconnected...
            lastPING = adesso;
            lastDisconnCheck = adesso;
            var VETO_PING_SEC = getOptPar("VETO_PING_SEC");
            if (!string.IsNullOrEmpty(VETO_PING_SEC))
            {
                int.TryParse(VETO_PING_SEC, out vetoPingSec);
            }
            var POWEROFF_TIMEOUT_SEC = getOptPar("POWEROFF_TIMEOUT_SEC");
            if (!string.IsNullOrEmpty(POWEROFF_TIMEOUT_SEC))
            {
                int.TryParse(POWEROFF_TIMEOUT_SEC, out PoweroffTimeoutSec);
            }

            var devData = IobConfFull.Device.Connect;
            ShellyOptions options = new ShellyOptions()
            {
                DefaultTimeout = TimeSpan.FromMilliseconds(devData.PingMsTimeout * 10),
                ServerUri = new Uri($"http://{devData.IpAddr}/rpc")
            };
            // init secondo il tipo di Shelly...

            shellyCli = new Shelly1PmClient(new HttpClient(), options);

        }

        public override async Task InitializeAsync()
        {
            // invio conf macchina all'inizio
            await SendMachineConfAsync();
        }

        #endregion Public Constructors

        #region Public Methods

        public override Dictionary<string, string> getDynData()
        {
            Dictionary<string, string> outData = new Dictionary<string, string>();
            // verifico che non sia una chiamata troppo frequente, ogni 10 sec minimo...
            if (DateTime.Now > lastReadPLC.AddSeconds(shellyCallDelay))
            {
                // chiama lettura status completo e recupera info in memRead e le passa come DynData...
                lastShellyResp = Task.Run(() => ((Shelly1PmClient)shellyCli).GetSwitchStatus(CancellationToken.None, 0)).Result;
                // NB: da verificare tipo di chiamata in base al tipo di shelly... TDB ora è cablato x Shelly1PM
                if (lastShellyResp.IsSuccess)
                {
                    lastReadPLC = DateTime.Now;
                    // recupero da conf memMap e ciclo
                    foreach (var item in IOBConfFull.Memory.mMapRead)
                    {
                        //string outVal = "";
                        double outValDbl = 0;
                        // se trovo includo in dynData...
                        switch (item.Value.memAddr)
                        {
                            case "ActEnergy":
                                outValDbl = lastShellyResp.Value.ActEnergy.Total;
                                //outVal = lastShellyRespRT.Value.ActEnergy.Total.ToString("F3", CultureInfo.InvariantCulture);
                                break;

                            case "Current":
                                outValDbl = lastShellyResp.Value.Current;
                                //outVal = lastShellyRespRT.Value.Current.ToString("F3", CultureInfo.InvariantCulture);
                                break;

                            case "Power":
                                outValDbl = lastShellyResp.Value.Power;
                                //outVal = lastShellyRespRT.Value.Power.ToString("F3", CultureInfo.InvariantCulture);
                                break;

                            case "Voltage":
                                outValDbl = lastShellyResp.Value.Voltage;
                                //outVal = lastShellyRespRT.Value.Voltage.ToString("F3", CultureInfo.InvariantCulture);
                                break;

                            default:
                                break;
                        }
                        // se ho fattore --> gestisco!
                        if (item.Value.factor != 1)
                        {
                            outValDbl = outValDbl * item.Value.factor;
                        }
                        // modalità saveValue x reduce fLog
                        saveValue(ref outData, item.Key, outValDbl);
                        //outData.Add(item.Key, outValDbl.ToString("F3", CultureInfo.InvariantCulture));
                    }
                }
                // traccio dati ricevuti
                trackExchDataRaw(lastShellyResp, 1024);
            }
            return outData;
        }

        /// <summary>
        /// Effettua lettura semafori principale <paramref name="currDispData">Parametri da
        /// aggiornare x display in form</paramref>
        /// </summary>
        public override void readSemafori(ref newDisplayData currDispData)
        {
            DateTime adesso = DateTime.Now;
            // se ha risposto ad ultima chiamata --> ok
            if (connectionOk && adesso.Subtract(lastReadPLC).TotalMinutes < 1)
            {
                B_input = 3;
                // aggiungo NON emergenza...
                B_input += (1 << 7);
            }
            else
            {
                B_input = 0;
            }
            lastReadPLC = DateTime.Now;
            lastWatchDog = adesso;
        }

        public override void startAdapter(bool resetQueue)
        {
            base.startAdapter(resetQueue);
            // 2023.09.05 imposto anche primo ping e check disconnected...
            DateTime adesso = DateTime.Now;
            lastWatchDog = adesso;
            //lastPING = adesso;
            lastReadPLC = adesso;
            lastDisconnCheck = adesso;
            // faccio un primo check POST ritardo
            tryConnect();
        }

        /// <summary>
        /// Override connessione
        /// </summary>
        public override void tryConnect()
        {
            bool doLog = (verboseLog || periodicLog);
            lgDebug($"Shelly: tryConnect step 01 | connectionOk: {connectionOk}");
            if (!connectionOk)
            {
                //// resetto coda...
                //QueuePing = new DataQueue("000", "QueuePing", false);
                // controllo che il ping sia stato tentato almeno pingTestSec fa...
                if (DateTime.Now.Subtract(lastPING).TotalSeconds > vetoPingSec)
                {
                    if (doLog)
                    {
                        lgInfo("Shelly: ConnKO - tryConnect");
                    }
                    lgDebug("Shelly: tryConnect step 04");

                    lgDebug("Shelly: Reset QueuePing");

                    bool pingOK = testPingMachine == IPStatus.Success;
                    addTest(pingOK);

                    // se passa il ping faccio il resto...
                    if (pingStatusOk() || pingOK)
                    {
                        // in primis salvo data ping...
                        lastPING = DateTime.Now;
                        connectionOk = true;
                        queueInEnabCurr = true;
                        lgInfo("Shelly - ping OK");
                    }
                    else
                    {
                        // loggo no risposta ping ...
                        lgError("Shelly - ping KO");
                    }
                }
            }
        }

        /// <summary>
        /// Override disconnessione
        /// </summary>
        public override void tryDisconnect()
        {
            lgInfo("Richiesta disconnessione adapter Shelly!");
            connectionOk = false;
            queueInEnabCurr = false;
        }

        #endregion Public Methods

        #region Private Fields

        /// <summary>
        /// Oggetto dove viene salvata ultima risposta shelly
        /// </summary>
        private ShellyResult<EgwProxy.Shelly.DTO.Shelly1PM.SwitchDto> lastShellyResp;

        private int PoweroffTimeoutSec = 100;

        /// <summary>
        /// Delay tra chiamate in sec
        /// </summary>
        private int shellyCallDelay = 1;

        /// <summary>
        /// Client interno x comunicazione con Shelly devices
        /// </summary>
        private ShellyClientBase shellyCli;

        /// <summary>
        /// Veto controllo status x log...
        /// </summary>
        private DateTime vetoCheckStatus = DateTime.Now;

        #endregion Private Fields

        #region Private Methods

        private void addTest(bool pingOk)
        {
            int score = pingOk ? 1 : 0;
            // controllo: se era spenta e risulta ping ok --> reset coda!
            if (B_input == 0 && pingOk)
            {
                B_input = 1;
                QueuePing = new DataQueue(IOBConfFull.General.FilenameIOB, "QueuePing", false, redisMan);
                lgTrace($"QueuePing resetted on addTest");
            }
            QueuePing.Enqueue($"{score}");
            while (QueuePing.Count > maxQueuePing)
            {
                string res = "";
                QueuePing.TryDequeue(out res);
            }
        }

        /// <summary>
        /// Calcola status ping:
        /// - se ha ‹ 50% coda richiesta --› true
        /// - se ha › 50% coda richiesta --› true se è maggior parte a 1 (true)
        /// </summary>
        /// <returns></returns>
        private bool pingStatusOk()
        {
            bool answ = false;
            long numVal = QueuePing.Count;
            if (numVal > maxQueuePing / 2)
            {
                var listaValori = QueuePing.ToList();
                long numOk = listaValori.Where(x => x == "1").Count();
                long numKo = numVal - numOk;
                answ = numOk >= numKo;
                lgTrace($"PING ok per: {numOk} > {numKo}");
            }
            else
            {
                lgTrace($"PING check: {answ} per mancanza dati minimi test");
            }
            return answ;
        }

        #endregion Private Methods
    }
}