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 SheetStatsController : ApiController
    {
        #region Protected Fields

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

        #endregion Protected Fields

        #region Private Methods

        /// <summary>
        /// Metodo effettivo recupero dati
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private List<chartJsTSerie> getDataFilt(int id)
        {
            List<chartJsTSerie> answ = new List<chartJsTSerie>();
            var tabDati = DLMan.taSP.GetData(id);
            DateTime startTime = DateTime.Now.AddYears(1);
            // ciclo
            foreach (var item in tabDati)
            {
                if (item.PrevTime < startTime)
                {
                    startTime = item.PrevTime;
                }
                answ.Add(new chartJsTSerie() { x = item.PrevTime, y = item.NumParts });
            }
            // restituisco
            return answ;
        }

        /// <summary>
        /// Metodo effettivo recupero dati
        /// </summary>
        /// <param name="id"></param>
        /// <param name="maxHour"></param>
        /// <returns></returns>
        private List<chartJsTSerie> getDataFilt(int id, int maxHour)
        {
            List<chartJsTSerie> answ = new List<chartJsTSerie>();
            var tabDati = DLMan.taSP.GetData(id);
            DateTime startTime = DateTime.Now.AddYears(1);
            // ciclo
            foreach (var item in tabDati)
            {
                if (item.PrevTime < startTime)
                {
                    startTime = item.PrevTime;
                }
                answ.Add(new chartJsTSerie() { x = item.PrevTime, y = item.NumParts });
                if (item.PrevTime > startTime.AddHours(maxHour))
                {
                    break;
                }
            }
            // restituisco
            return answ;
        }

        #endregion Private Methods

        #region Public Methods

        // GET api/TempRil
        public string Get()
        {
            return "NA";
        }

        /// <summary>
        /// Recupero dati x batch selezionato
        /// GET api/SheetStats/5
        /// </summary>
        /// <param name="id">BatchID</param>
        /// <returns></returns>
        public List<chartJsTSerie> Get(int id)
        {
            DateTime dtRif = DateTime.Today;
            // restituisco oggetto!
            return getDataFilt(id);
        }

        /// <summary>
        /// Recupero dati x batch selezionato
        /// GET api/SheetStats/5
        /// </summary>
        /// <param name="id">BatchID</param>
        /// <param name="maxHour">num max ore da mostrare</param>
        /// <returns></returns>
        public List<chartJsTSerie> Get(int id, int MaxHour)
        {
            List<chartJsTSerie> answ = new List<chartJsTSerie>();
            // se maxHour < 0 --> mostro tutto
            if (MaxHour < 0)
            {
                answ = getDataFilt(id);
            }
            else
            {
                answ = getDataFilt(id, MaxHour);
            }
            // restituisco oggetto!
            return answ;
        }

        #endregion Public Methods
    }
}