using AppData;
using Newtonsoft.Json;
using NKC_SDK;
using SteamWare;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web.Http;

namespace NKC_WF.Controllers
{
    public class PrintQueueLenController : ApiController
    {
        /// <summary>
        /// oggetto static/singleton per fare chiamate sul datalayer
        /// </summary>
        protected DataLayer DLMan = new DataLayer();

        #region classi gestione PJQ

        /// <summary>
        /// Conteggio elementi in attesa stampa da DB
        /// </summary>
        protected int countWaitingDb
        {
            get
            {
                int answ = 0;
                // resetto conteggio in redis...
                DS_Report.PrintJobQueueDataTable tabPJQ = DLMan.taPJQ.getWaiting();
                if (tabPJQ != null)
                {
                    answ = tabPJQ.Count;
                }
                return answ;
            }
        }
        /// <summary>
        /// Conteggio elementi in attesa stampa da DB che fanno parte di un SET di queue
        /// </summary>
        protected int countWaitingDbQueueMult(string queueSet)
        {
            int answ = 0;
            // resetto conteggio in redis...
            DS_Report.PrintJobQueueDataTable tabPJQ = DLMan.taPJQ.getWaiting();
            if (tabPJQ != null)
            {
                var selQueue = tabPJQ.Where(x => queueSet.Contains($"|{x.prtName}|")).ToList();
                if (selQueue != null)
                {
                    answ = selQueue.Count;
                }
            }
            return answ;
        }
        
        #endregion

        #region REST api call

        /// <summary>
        /// Restituisce numero jobs aperti (stato = 0...), se 0 = NESSUNO
        /// GET: api/PrintQueue
        /// </summary>
        /// <returns></returns>
        public int Get()
        {
            // restituisco...
            int answ = 0;
            string redVal = memLayer.ML.getRSV(ComLib.redQueueCount);
            // cerco in redis se ci sia chiave..
            if (!string.IsNullOrEmpty(redVal))
            {
                // recupero
                int.TryParse(redVal, out answ);
            }
            else
            {
                // recupero da db e salvo
                answ = countWaitingDb;
                memLayer.ML.setRSV(ComLib.redQueueCount, answ.ToString(), memLayer.ML.CRI("cacheQueueSec"));
                // chiudo gli zombie (stampe non chiuse)...
                DLMan.taPJQ.chiudiZoombie(DateTime.Now.AddMilliseconds(-memLayer.ML.CRI("zombieMsTime")));
            }
            return answ;
        }

  

        /// <summary>
        /// Restituisce numero jobs aperti (stato = 0...), se 0 = NESSUNO
        /// GET: api/PrintQueue?queueList=|queueOffline|
        /// </summary>
        /// <param name="queueList">Elenco code delimitate da |..|</param>
        /// <returns></returns>
        public int Get(string queueList)
        {
            // restituisco...
            int answ = 0;
            string redVal = memLayer.ML.getRSV($"{ComLib.redQueueCountSet}:{queueList}");
            // cerco in redis se ci sia chiave..
            if (!string.IsNullOrEmpty(redVal))
            {
                // recupero
                int.TryParse(redVal, out answ);
            }
            else
            {
                // recupero da db e salvo
                answ = countWaitingDbQueueMult(queueList);
                memLayer.ML.setRSV($"{ComLib.redQueueCountSet}:{queueList}", answ.ToString(), memLayer.ML.CRI("cacheQueueSec"));
                // chiudo gli zombie (stampe non chiuse)...
                DLMan.taPJQ.chiudiZoombie(DateTime.Now.AddMilliseconds(-memLayer.ML.CRI("zombieMsTime")));
            }
            return answ;
        }

     
        #endregion
    }
}
