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

namespace NKC_WF.Controllers
{
    /// <summary>
    /// Classe controller per gestione eventi stats
    /// Riceve una lista di 1..n eventi con
    ///  * CodMacchina
    ///  * DataOra
    ///  * Codice (Evento/stato) - codice 00 = KeepAlive, imposto dataora/drift
    ///  * Valore/Descrizione
    ///
    /// e la salva su DB per successiva elaborazione
    /// </summary>
    public class MachineStatController : ApiController
    {
        #region Protected Fields

        /// <summary>
        /// oggetto static/singleton per fare chiamate sul datalayer
        /// </summary>
        protected DataLayer DLMan = new DataLayer();

        /// <summary>
        /// Codice macchina (HARD CODED)
        /// </summary>
        protected string machine = "WRK000";

        #endregion Protected Fields

        #region Public Methods

        /// <summary>
        /// Restituisce un array di stati macchina correnti
        /// GET: api/MachineStat
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public MachineStatData Get()
        {
            MachineStatData answ = new MachineStatData();
            try
            {
                answ = ComLib.prodMachStateDataGet(machine);
            }
            catch
            { }

            // se vuoto metto 1 finto...
            if (answ == null)
            {
                answ = new MachineStatData() { Machine = machine };
            }

            return answ;
        }

        /// <summary>
        /// Fornisce il record dello stato macchina corrente
        /// GET: api/MachineStat/WRK001
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public MachineStatData Get(string id)
        {
            MachineStatData answ = new MachineStatData() { Machine = id };
            try
            {
                answ = ComLib.prodMachStateDataGet(id);
            }
            catch
            { }

            return answ;
        }

        /************************************
         * METODI PUT
         *
         * per abilitare è necessario agire sulla conf di IIS:
         *
         * - modificare il file applicationHost.config che si trova in C:\Windows\System32\inetsrv\config
         * - disinstallare webDav oppure commentare le righe
         *  <!-- <add name="WebDAVModule" /> -->
         *  <!-- <add name="WebDAVModule" image="%windir%\System32\inetsrv\webdav.dll" /> -->
         *  <!-- <add name="WebDAV" path="*" verb="PROPFIND,PROPPATCH,MKCOL,PUT,COPY,DELETE,MOVE,LOCK,UNLOCK" modules="WebDAVModule" resourceType="Unspecified" requireAccess="None" /> -->
         * - aggiungere PUT/DELETE a handler:
         *  <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
         *  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="0" />
         *
         **************************************/


        /// <summary>
        /// Effettua la chiamata di update x un set di dati delle macchine
        /// </summary>
        /// <param name="updatedInfo">Oggetto con macchina + Elenco record da aggiornare</param>
        // PUT: api/Sheet
        [HttpPut]
        public void Put(MachineStatData updatedInfo)
        {
            // NB. decodifico direttamente come oggetto, vedere qui:
            // https://weblog.west-wind.com/posts/2013/dec/13/accepting-raw-request-body-content-with-aspnet-web-api
            // https://weblog.west-wind.com/posts/2017/sep/14/accepting-raw-request-body-content-in-aspnet-core-api-controllers

            if (updatedInfo != null)
            {
                // mi limito a chiamnare procedura di update...
                ComLib.prodMachStateDataInsert(updatedInfo);
            }
        }

        #endregion Public Methods
    }
}