using EgwCApp.Core;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static EgwCApp.Core.WharehouseData;

namespace EgwCApp.XmlProc
{
    public class ImportProc
    {
        #region Public Constructors

        /// <summary>
        /// Init oggetto per import
        /// </summary>
        /// <param name="confFileName"></param>
        public ImportProc(string confFileName)
        {
            if (!string.IsNullOrEmpty(confFileName))
            {
                fileConfName = confFileName;
            }
        }

        #endregion Public Constructors

        #region Public Methods

        /// <summary>
        /// Decodifica configurazione
        /// </summary>
        /// <returns></returns>
        public bool decodeConfig()
        {
            bool answ = false;
            if (!string.IsNullOrEmpty(fileConfName))
            {
                // deserializzo config
                if (!File.Exists(fileConfName))
                {
                    Console.WriteLine($"Error: ConfigFile not found | {fileConfName}");
                }
                else
                {
                    string rawData = File.ReadAllText(fileConfName);
                    // se ho contenuto procedo
                    if (string.IsNullOrEmpty(rawData))
                    {
                        Console.WriteLine($"Error: ConfigFile empty! | {fileConfName}");
                    }
                    else
                    {
                        // deserializzo
                        taskConfig = JsonConvert.DeserializeObject<ConfigFile>(rawData);
                        answ = taskConfig != null;
                    }
                }
            }
            return answ;
        }

        /// <summary>
        /// Esegue import (se possibile)
        /// </summary>
        /// <returns></returns>
        public bool doProcess()
        {
            bool answ = false;
            if (taskConfig != null)
            {
                // verifico esista il file...
                if (string.IsNullOrEmpty(taskConfig.FileInPath) && File.Exists(taskConfig.FileInPath))
                {
                    // manca file ingresso!!! esco!
                }
                else
                {
                    // verifico il tipo di process necessario...
                    switch (taskConfig.Type)
                    {
                        case ImportType.CSV:
                            fileReturnData = File.ReadAllText(taskConfig.FileInPath);
                            answ = true;
                            break;

                        case ImportType.Excel:
                            fileReturnData = processExcelImport(taskConfig.FileInPath);
                            answ = true;
                            break;

                        case ImportType.ND:
                        default:
                            break;
                    }
                }
            }
            return answ;
        }

        /// <summary>
        /// Esecuzione ritorno informazioni secondo configurazione...
        /// </summary>
        /// <returns></returns>
        public bool doReturn()
        {
            bool answ = false;
            if (taskConfig != null)
            {
                // verifico il tipo di return necessario...
                switch (taskConfig.Return)
                {
                    case ReturnMode.Console:
                        Console.WriteLine(fileReturnData);
                        answ = true;
                        break;

                    case ReturnMode.Redis:
                        break;

                    case ReturnMode.File:
                        // verifico path ci sia... sennò creo
                        string outPath = string.IsNullOrEmpty(taskConfig.FileOutPath) ? "FileOut.txt" : taskConfig.FileOutPath;
                        // verifico se vadano salvati in una folder differente...
                        if (!string.IsNullOrEmpty(taskConfig.ConvertDir))
                        {
                            if (!Directory.Exists(taskConfig.ConvertDir))
                            {
                                Directory.CreateDirectory(taskConfig.ConvertDir);
                            }
                            outPath = Path.Combine(taskConfig.ConvertDir, Path.GetFileName(outPath));
                        }
                        // salvo il file!
                        File.WriteAllText(outPath, fileReturnData);
                        answ = true;
                        break;

                    case ReturnMode.ND:
                    default:
                        break;
                }
                // se fatto eventualmente archivio
                if (answ)
                {
                    if (!string.IsNullOrEmpty(taskConfig.ArchiveDir))
                    {
                        // verifico cartella archivio
                        if (!Directory.Exists(taskConfig.ArchiveDir))
                        {
                            Directory.CreateDirectory(taskConfig.ArchiveDir);
                        }
                        // sposto file
                        string fName = Path.GetFileName(taskConfig.FileInPath);
                        File.Move(taskConfig.FileInPath, Path.Combine(taskConfig.ArchiveDir, fName), true);
                    }
                }
            }
            return answ;
        }

        #endregion Public Methods

        #region Protected Properties

        /// <summary>
        /// Nome del file config da processare
        /// </summary>
        protected string fileConfName { get; set; } = "";

        /// <summary>
        /// Contenuto del file da restituire come return data (serializzato)
        /// </summary>
        protected string fileReturnData { get; set; } = "";

        /// <summary>
        /// Configurazione del task da eseguire
        /// </summary>
        protected ConfigFile? taskConfig { get; set; } = new ConfigFile();

        #endregion Protected Properties

        #region Protected Methods

        /// <summary>
        /// Estrae da una riga l'i-esimo elemento
        /// </summary>
        /// <param name="riga"></param>
        /// <param name="col"></param>
        /// <returns></returns>
        protected string getCellVal(System.Data.DataRow? riga, int col)
        {
            string answ = "";
            if (riga != null)
            {
                try
                {
                    answ = $"{riga.ItemArray[col]}".Trim();
                }
                catch
                { }
            }
            return answ;
        }

        /// <summary>
        /// Cleanup stringa x impiego tipo ident da char dubbi
        /// </summary>
        /// <param name="origData"></param>
        /// <returns></returns>
        protected string strFixId(string origData)
        {
            return origData.Replace(".", "").Replace(" ", "_");
        }

        #endregion Protected Methods

        #region Private Methods

        /// <summary>
        /// Importa un file excel e restituisce una
        /// </summary>
        /// <param name="fileItem"></param>
        /// <returns></returns>
        private string processExcelImport(string fileItem)
        {
            string outVal = "";
            int numErr = 0;
            // test procedura di import files excel x Giacovelli...
            var currExcel = new ExcelMan(fileItem);
            // creo lista dati in formato RegGiacenze...
            Dictionary<string, BatchRec> listaGiac = new Dictionary<string, BatchRec>();
            var dtSet = currExcel.getDataSet();
            if (dtSet != null && dtSet.Tables != null && dtSet.Tables.Count > 0)
            {
                string nomeFile = Path.GetFileName(fileItem);
                nomeFile = nomeFile.Substring(0, nomeFile.LastIndexOf("."));
                var elSheet = dtSet.Tables;
                int idxTab = 0;
                // cerco lo sheet corretto se > 1
                if (dtSet.Tables.Count > 1)
                {
                    bool found = false;
                    for (int i = 0; i < dtSet.Tables.Count; i++)
                    {
                        if (nomeFile.Contains(dtSet.Tables[i].TableName))
                        {
                            idxTab = i;
                            found = true;
                            break;
                        }
                        // controllo parametro opzionale...
                        if (!found && taskConfig != null && !string.IsNullOrEmpty(taskConfig.TargetName))
                        {
                            if (dtSet.Tables[i].TableName == taskConfig.TargetName)
                            {
                                idxTab = i;
                                break;
                            }
                        }
                    }
                }
                var tabella = dtSet.Tables[idxTab];
                int numRighe = tabella.Rows.Count;
                int idxODL = taskConfig != null ? taskConfig.IdxODL : 0;
                for (int i = 0; i < numRighe; i++)
                {
                    if (taskConfig != null && taskConfig.ProcessParamInt != null && taskConfig.ProcessParamInt.Count > 5)
                    {
                        if (numErr < numRighe / 5)
                        {
                            try
                            {
                                // variabili di appoggio...
                                DateTime dtRif = DateTime.Today;
                                double qtyTot = 0;
                                int numPack = 0;
                                var riga = tabella.Rows[i];
                                if (riga != null)
                                {
                                    string ddt = getCellVal(riga, taskConfig.ProcessParamInt["ExtDoc"]);
                                    string sDate = getCellVal(riga, taskConfig.ProcessParamInt["DateRif"]);
                                    string prod = getCellVal(riga, taskConfig.ProcessParamInt["Product"]);
                                    // verifiche x import: header, data e DDT (vuoti o "-") --> SKIP!
                                    bool checkHeaderKo = (ddt == "DDT" || prod == "PRODOTTO");
                                    bool checkEmptyDdt = (string.IsNullOrEmpty(ddt) || ddt == "-");
                                    bool checkEmptyDate = (string.IsNullOrEmpty(sDate) || sDate == "-");
                                    if (checkHeaderKo)
                                    {
                                        //lgTrace($"SKIP header");
                                    }
                                    else if (checkEmptyDdt || checkEmptyDate)
                                    {
                                        //lgTrace($"SKIP linea vuota | i: {i} | ddt: {ddt} | date: {sDate} | prod: {prod}");
                                    }
                                    else
                                    {
                                        string variety = getCellVal(riga, taskConfig.ProcessParamInt["Variety"]);
                                        string suppl = getCellVal(riga, taskConfig.ProcessParamInt["Supplier"]);
                                        string sQty = getCellVal(riga, taskConfig.ProcessParamInt["QtyTot"]);
                                        string sNum = getCellVal(riga, taskConfig.ProcessParamInt["NumPack"]);
                                        string numPed = getCellVal(riga, taskConfig.ProcessParamInt["NumPed"]);
                                        string packPed = getCellVal(riga, taskConfig.ProcessParamInt["PackPed"]);
                                        string pesoPack = getCellVal(riga, taskConfig.ProcessParamInt["PesoPack"]);
                                        DateTime.TryParse(sDate, out dtRif);
                                        int.TryParse(sNum, out numPack);
                                        double.TryParse(sQty, out qtyTot);
                                        string identRG = ddt.Length > 2 ? $"{strFixId(ddt)}.{strFixId(prod)}.{strFixId(variety)}.{strFixId(suppl)}" : $"{dtRif:yyyyMMdd}.{strFixId(prod)}.{strFixId(variety)}.{strFixId(suppl)}";
                                        string notes = $"{numPed}x{packPed}x{pesoPack}";
                                        // verifico di avere dati per proseguire...
                                        bool checkIdent = !string.IsNullOrEmpty($"{prod}{variety}{suppl}");
                                        if (checkIdent)
                                        {
                                            BatchRec newRow = new BatchRec()
                                            {
                                                IdxODL = idxODL,
                                                IdentRG = identRG,
                                                DateRif = dtRif,
                                                ExtDoc = ddt,
                                                Product = prod,
                                                Variety = variety,
                                                Supplier = suppl,
                                                NumPack = numPack,
                                                QtyTot = qtyTot,
                                                Notes = notes
                                            };
                                            // verifico: se manca aggiungo
                                            if (!listaGiac.ContainsKey(identRG))
                                            {
                                                listaGiac.Add(identRG, newRow);
                                            }
                                            else
                                            {
                                                // altrimenti aggiorno giacenza con valori numerici
                                                listaGiac[identRG].NumPack += newRow.NumPack;
                                                listaGiac[identRG].QtyTot += newRow.QtyTot;
                                            }
                                        }
                                        else
                                        {
                                            //lgError($"Errore verifica identità riga | prod: {prod} | variety: {variety} | suppl: {suppl}");
                                            numErr++;
                                        }
                                    }
                                }
                            }
                            catch (Exception exc)
                            {
                                numErr++;
                            }
                        }
                    }
                }
            }
            if (listaGiac.Count > 0)
            {
                // converto in una nuova lista...
                int rCounter = 1;
                Dictionary<int, BatchRec> list2Send = new Dictionary<int, BatchRec>();
                foreach (var item in listaGiac)
                {
                    list2Send.Add(rCounter, item.Value);
                    rCounter++;
                }
                // serializzo e restituisco file JSON...
                var serVal = JsonConvert.SerializeObject(list2Send);
                if (serVal != null && !string.IsNullOrEmpty(serVal))
                {
                    outVal = serVal;
                }
            }
            return outVal;
        }

        #endregion Private Methods
    }
}