using EgwCApp.Core;
using Newtonsoft.Json;
using System.Diagnostics;

namespace EgwCApp.Testing
{
    public class FileProcMan
    {
        #region Public Constructors

        public FileProcMan(string toolDir, string exeFileName)
        {
            this.confFileName = "conf.json";
            this.baseDir = toolDir;
            this.exeName = exeFileName;
            appPath = Path.Combine(baseDir, exeName);
        }

        #endregion Public Constructors

        #region Public Methods

        /// <summary>
        /// Processa il singolo file e riporta tempo esecuzione
        /// </summary>
        /// <returns></returns>
        public TimeSpan doProcess(string fPath)
        {
            TimeSpan outVal = new TimeSpan();
            Stopwatch sw = new Stopwatch();
            // preparo file conf
            createConfFile(fPath);
            // avvio processing
            Console.WriteLine("calling ext app with args:");
            Console.WriteLine($"{appPath} {confFileName}");
            Console.WriteLine();

            ProcessStartInfo psi = new ProcessStartInfo
            {
                FileName = appPath,
                Arguments = $"{confFileName}",
                WindowStyle = ProcessWindowStyle.Minimized,
                //WindowStyle = ProcessWindowStyle.Hidden,
                UseShellExecute = false,
                //CreateNoWindow = true,
                RedirectStandardOutput = true,
                RedirectStandardInput = true,
            };

            sw.Start();

            Process p = Process.Start(psi);

            string q = "";
            while (!p.HasExited)
            {
                q += p.StandardOutput.ReadToEnd();
            }

            sw.Stop();
            outVal = sw.Elapsed;

            return outVal;
        }

        #endregion Public Methods

        #region Protected Fields

        protected string appPath = "";
        protected string baseDir = "";
        protected string confFileName = "";
        protected string exeName = "";

        #endregion Protected Fields

        #region Private Methods

        private void createConfFile(string item)
        {
            Dictionary<string, int> importParams = new Dictionary<string, int>();
            importParams.Add("Product", 3);
            importParams.Add("Variety", 9);
            importParams.Add("Supplier", 8);
            importParams.Add("ExtDoc", 2);
            importParams.Add("DateRif", 14);
            importParams.Add("QtyTot", 22);
            importParams.Add("NumPack", 21);
            importParams.Add("NumPed", 17);
            importParams.Add("PackPed", 18);
            importParams.Add("PesoPack", 20);
            // calcolo nome file conf specifico
            string outFileName = Path.GetFileName(item).Replace("xlsx", "json");
            confFileName = $"conf_{outFileName}";
            // calcolo outFIleName
            var newConf = new ConfigFile()
            {
                ArchiveDir = @"C:\temp\import\archive\",
                ConvertDir = @"C:\temp\import\convert\",
                Type = ImportType.Excel,
                FileInPath = item,
                FileOutPath = outFileName,
                Return = ReturnMode.File,
                ProcessParamInt = importParams,
                TargetName = "DB Loco"
            };
            // serializzo e salvo!
            var rawData = JsonConvert.SerializeObject(newConf, Formatting.Indented);
            File.WriteAllText(confFileName, rawData);
        }

        #endregion Private Methods
    }
}