using AppData;
using SteamWare;
using System;
using System.Web.Http;

namespace NKC_WF.Controllers
{
    public class getMUCssRevController : ApiController
    {
        // GET api/getMUCssRev
        public int Get()
        {
            int answ = getRevBySheet(0);
            return answ;
        }
        // GET api/getMUCssRev/5
        public int Get(int id)
        {
            int answ = getRevBySheet(id);
            return answ;
        }
        /// <summary>
        /// Recupera da REDIS info indice revisione x foglio
        /// </summary>
        /// <param name="SheetID"></param>
        /// <returns></returns>
        private static int getRevBySheet(int SheetID)
        {
            int answ = 0;
            if (SheetID > 0)
            {
                // recupero da REDIS!
                string redKeyExp = memLayer.ML.redHash($"DataExp");
                string redKey = $"{ComLib.machineUnloadArea(SheetID)}:Css";
                string redKeyRev = $"{ComLib.machineUnloadArea(SheetID)}:CssRev";
                // controllo expiry globale... se manca SVUOTO area
                string rawData = memLayer.ML.getRSV(redKeyExp);
                if (string.IsNullOrEmpty(rawData))
                {
                    // svuoto e scrivo...
                    memLayer.ML.redFlushKey(memLayer.ML.redHash($"MachineUnload"));
                    memLayer.ML.redFlushKey(memLayer.ML.redHash($"TabSheets"));
                    memLayer.ML.setRSV(redKeyExp, $"Reload Data {DateTime.Now}", 3600);
                }
                // SE fosse scaduto CSS --> aggiorno revisione...
                if (!memLayer.ML.redKeyPresent(redKey))
                {
                    // incremento...
                    memLayer.ML.setRCntI(redKeyRev);
                }
                answ = memLayer.ML.getRCnt(redKeyRev);
                // se > 999 --> resetto
                if (answ > 999)
                {
                    memLayer.ML.resetRCnt(redKeyRev);
                }
            }
            return answ;
        }

    }
}