using System;
using System.Collections.Generic;

namespace IOB_UT_NEXT
{
    /// <summary>
    /// Rappresentazione dello stato corrente dell'IOB
    /// </summary>
    public class IobWinStatus
    {
        #region Public Properties

        /// <summary>
        /// ID univoco
        /// </summary>
        public string CodIob { get; set; } = "0";

        /// <summary>
        /// Contatore IOB
        /// </summary>
        public float counterIOB { get; set; } = 0;

        /// <summary>
        /// Contatore Macchina
        /// </summary>
        public float counterMAC { get; set; } = 0;

        /// <summary>
        /// Ultimo stato noto dei parametri in memoria letti dalla macchina
        /// </summary>
        public Dictionary<string, string> currParams { get; set; } = new Dictionary<string, string>();

        /// <summary>
        /// Note (libere) da child a controller
        /// </summary>
        public string freeNotes { get; set; } = "";

        /// <summary>
        /// Tipo IOB
        /// </summary>
        public string IobType { get; set; } = "";

        /// <summary>
        /// DataOra ultima comunicazione IN (con PLC)
        /// </summary>
        public DateTime lastDataIn { get; set; } = DateTime.Now.AddMinutes(-1);

        /// <summary>
        /// DataOra ultima comunicazione OUT (con MP Server)
        /// </summary>
        public DateTime lastDataOut { get; set; } = DateTime.Now.AddDays(-1);

        /// <summary>
        /// DataOra ultima comunicazione OUT (con MP Server)
        /// </summary>
        public DateTime lastUpdate { get; set; } = DateTime.Now.AddDays(-1);

        /// <summary>
        /// Valore minimo da considerare come delta secondi x indicare variazione sui dati temporali
        /// </summary>
        public int minDeltaSec { get; set; } = 15;

        /// <summary>
        /// Status del SINGOLO IOB
        /// </summary>
        public bool online { get; set; } = false;

        /// <summary>
        /// Lungh coda ALLARMI in uscita
        /// </summary>
        public long queueAlLen { get; set; } = 0;

        /// <summary>
        /// Lunghezza coda EVENTI in uscita
        /// </summary>
        public long queueEvLen { get; set; } = 0;

        /// <summary>
        /// Lunghezza coda FluxLog in uscita
        /// </summary>
        public long queueFlLen { get; set; } = 0;

        /// <summary>
        /// Lungh coda MESSAGGI in uscita
        /// </summary>
        public long queueMsLen { get; set; } = 0;

        /// <summary>
        /// Lungh coda UserLog in uscita
        /// </summary>
        public long queueRawTransfLen { get; set; } = 0;

        /// <summary>
        /// Lungh coda UserLog in uscita
        /// </summary>
        public long queueUlLen { get; set; } = 0;

        /// <summary>
        /// Semaforo IN (IOB-Macchina)
        /// </summary>
        public string semIn { get; set; } = "ND";

        /// <summary>
        /// Semaforo OUT (IOB-MPServer)
        /// </summary>
        public string semOut { get; set; } = "ND";

        /// <summary>
        /// Parametri configurazione (es IP target, porta, ...)
        /// </summary>
        public Dictionary<string, string> setupParams { get; set; } = new Dictionary<string, string>();

        #endregion Public Properties

        #region Public Methods

        /// <summary>
        /// Override metodo di equality
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            if (!(obj is IobWinStatus item))
                return false;

            if (online != item.online)
                return false;
            // controllo se sia ALMENO 15 sec...
            if (lastUpdate != item.lastUpdate)
            {
                if (lastUpdate.Subtract(item.lastUpdate).TotalSeconds > minDeltaSec)
                    return false;
            }
            // controllo se sia ALMENO 15 sec...
            if (lastDataIn != item.lastDataIn)
            {
                if (lastDataIn.Subtract(item.lastDataIn).TotalSeconds > minDeltaSec)
                    return false;
            }
            // controllo se sia ALMENO 5 sec...
            if (lastDataOut != item.lastDataOut)
            {
                if (lastDataOut.Subtract(item.lastDataOut).TotalSeconds > minDeltaSec / 3)
                    return false;
            }
            if (semIn != item.semIn)
                return false;
            if (semOut != item.semOut)
                return false;
            //if (!currParams.Equals(item.currParams))
            //    return false;
            if (counterIOB != item.counterIOB)
                return false;
            if (counterMAC != item.counterMAC)
                return false;
            if (queueAlLen != item.queueAlLen)
                return false;
            if (queueEvLen != item.queueEvLen)
                return false;
            if (queueFlLen != item.queueFlLen)
                return false;
            if (queueMsLen != item.queueMsLen)
                return false;
            if (queueRawTransfLen != item.queueRawTransfLen)
                return false;
            if (queueUlLen != item.queueUlLen)
                return false;
            if (freeNotes != item.freeNotes)
                return false;
            //if (setupParams != item.setupParams)
            //    return false;
            if (IobType != item.IobType)
                return false;
            if (CodIob != item.CodIob)
                return false;

            return true;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        #endregion Public Methods
    }
}