using Newtonsoft.Json;
using SteamWare;
using SteamWare.Logger;
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Http;

namespace NKC_WF.Controllers
{
    public class PrintQueueConfController : ApiController
    {
        #region Public Methods

        /// <summary>
        /// Restituisce un array JSon x le conf delle code di stampa
        /// GET: api/PrintQueueConf
        /// </summary>
        /// <returns>lista oggetto Json in formato SteamwareSDK.queueConf</returns>
        public List<queueConf> Get()
        {
            List<queueConf> answ = new List<queueConf>();
            string dirPath = HttpContext.Current.Server.MapPath("~/Reports/");
            string nomeFile = "queueConf.json";
            bool fileExist = fileMover.obj.fileExist(dirPath, nomeFile);
            if (fileExist)
            {
                string rawData = File.ReadAllText($"{dirPath}\\{nomeFile}");
                if (!string.IsNullOrEmpty(rawData))
                {
                    try
                    {
                        answ = JsonConvert.DeserializeObject<List<queueConf>>(rawData);
                    }
                    catch (Exception exc)
                    {
                        Logging.Instance.Error(exc, "Eccezione in decodifica file conf printQueue");
                    }
                }
            }
            // se vuoto... creo!
            if (answ.Count == 0)
            {
                queueConf currConf;
                string outForm = "EMF";
                string pagWidth = "8.5in";
                string pagHeigth = "11in";
                string margin = "0.5in";
                devInfoParam currDevInfoParam = new devInfoParam(outForm, pagHeigth, pagWidth, margin, margin, margin, margin);
                currConf = new queueConf()
                {
                    name = "default",
                    template = "missing.rdlc",
                    printerName = "Microsoft Print to PDF",
                    deviceInfoParam = currDevInfoParam
                };
                answ.Add(currConf);
            }
            return answ;
        }

        #endregion Public Methods
    }
}