using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace IOB_UT
{
    /// <summary>
    /// Classe di helper per metodi Euromap63 tramite File Exchange
    /// </summary>
    public class Eurom63
    {
        #region Public Enums

        /// <summary>
        /// Elenco dei livelli di comunicazione attivi
        /// </summary>
        public enum ComLevel
        {
            /// <summary>
            /// Comunicazione NON attiva
            /// </summary>
            None = 0,

            /// <summary>
            /// COntrollo comunicazione col PLC
            /// </summary>
            IsConnected,

            /// <summary>
            /// Ricezione info preliminari macchina
            /// </summary>
            HasInfo,

            /// <summary>
            /// Impostato data-ora correnti
            /// </summary>
            TimeSet,

            /// <summary>
            /// Verificato il canale di comunicazione è OK, fatta pulizia con AbortAll, NON ci sono report attivi
            /// </summary>
            ChannelOk,

            /// <summary>
            /// Status data richiesti
            /// </summary>
            StatusRequested,

            /// <summary>
            /// Prod data richiesti
            /// </summary>
            ProdRequested
        }

        public enum FileOpr
        {
            Copy = 0,
            ReplaceAndCopy
        }

        public enum FileType
        {
            NA = 0,
            Dat,
            Job,
            Log,
            Req,
            Rsp
        }

        public enum JobType
        {
            NA = 0,
            AbortAll,
            GetId,
            GetInfo,
            GetParams,
            GetStatus,
            GetProduction,
            IsConnected,
            SetDateTime,
            SetParamOdl
        }

        #endregion Public Enums

        #region Public Classes

        /// <summary>
        /// Struttura per un file da processare
        /// </summary>
        public class FileTask
        {
            #region Public Properties

            public FileOpr OprReq { get; set; } = FileOpr.Copy;
            public string Path { get; set; } = @"EUR63_JOB\SESS9999.FILE";

            #endregion Public Properties
        }

        public class ProtoConf
        {
            #region Public Fields

            /// <summary>
            /// Array delel sessioni attive
            /// </summary>
            public Dictionary<int, Session> ActiveSessions = new Dictionary<int, Session>();

            #endregion Public Fields

            #region Public Properties

            /// <summary>
            /// Directory base scambio dati
            /// </summary>
            public string BaseDir { get; set; } = @"C:\EUROMAP63\";

            /// <summary>
            /// array estensioni da eliminare in cleanup
            /// </summary>
            public List<string> cleanupExt { get; set; } = new List<string>();

            /// <summary>
            /// array dati dinamici da acquisire
            /// </summary>
            public List<DynDataItem> DynData { get; set; } = new List<DynDataItem>();

            /// <summary>
            /// Numero di pezzi/pallet per eventuaole moltiplica valore letto dalla macchina
            /// </summary>
            public int PzPallet { get; set; } = 1;

            /// <summary>
            /// Indica se sia possibile impostare i pezzi pallet dal server, ed in tal caso moltiplica i pezzi letti dalla macchina per il valore pzPallet (vedi sopra)
            /// </summary>
            public bool SetPzPalletFromSrv { get; set; } = false;

            #endregion Public Properties
        }

        /// <summary>
        /// Generic communication session
        /// </summary>
        public class Session
        {
            #region Public Properties

            /// <summary>
            /// Session currently active
            /// </summary>
            public bool Active { get; set; } = false;

            /// <summary>
            /// Indicates that must be repeated/restarted when not active
            /// </summary>
            public bool Cycle { get; set; } = false;

            /// <summary>
            /// Session Description
            /// </summary>
            public string Description { get; set; } = "";

            /// <summary>
            /// Elenco dei file da processare
            /// </summary>
            public List<FileTask> FileList { get; set; } = new List<FileTask>();

            /// <summary>
            /// Numero MAX di esecuzioni da mantenere in HIST prima di eliminare i + vecchi (fare folder HIST\SESSnnnn)
            /// </summary>
            public int Max2Keep { get; set; } = 50;

            /// <summary>
            /// Session check passed (= not to repeat)
            /// </summary>
            public bool Passed { get; set; } = false;

            /// <summary>
            /// TAG per risposta con errori
            /// </summary>
            public string RespKo { get; set; } = "ERROR";

            /// <summary>
            /// Tag per risposta positiva
            /// </summary>
            public string RespOk { get; set; } = "PROCESSED";

            /// <summary>
            /// Seconds before do a new retry (default = 0 --> no veto)
            /// </summary>
            public double RetrySec { get; set; } = 0;

            /// <summary>
            /// Veto for retry operation
            /// </summary>
            public DateTime RetryVeto { get; set; } = DateTime.Now;

            /// <summary>
            /// Session Name
            /// </summary>
            public string SessionName { get; set; } = "SESS0000";

            /// <summary>
            /// Start of communication
            /// </summary>
            public DateTime SessionStarted { get; set; } = DateTime.Now.AddMinutes(-5);

            /// <summary>
            /// Session type
            /// </summary>
            public JobType SessionType { get; set; } = JobType.NA;

            /// <summary>
            /// Estimated Validity limit
            /// </summary>
            public DateTime SessionValidUntil { get; set; } = DateTime.Now.AddMinutes(-1);

            /// <summary>
            /// Set Step Order (=execution priority, 1...n)
            /// </summary>
            public int StepOrder { get; set; } = 999;

            /// <summary>
            /// Standard session validity time
            /// </summary>
            public double ValidityMinutes { get; set; } = 1;

            #endregion Public Properties
        }

        #endregion Public Classes
    }
}