using AppData;
using SteamWare;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NKC_WF.site
{
    public partial class DxfValidation : BasePage
    {
        #region Protected Fields

        /// <summary>
        /// Path base x nesting, area TEMP
        /// </summary>
        protected string cadBasePath = $"{memLayer.ML.CRS("nestBasePath")}{memLayer.ML.CRS("fileTestFolder")}/";

        /// <summary>
        /// Path base x server, area TEMP
        /// </summary>
        protected string srvCadBasePath = $"{memLayer.ML.CRS("servBasePath")}{memLayer.ML.CRS("fileTestFolder")}/";

        #endregion Protected Fields

        #region Private Methods

        private void Cmp_fileUpload_eh_FileUploaded(object sender, WebUserContols.FileUploadEventArgs e)
        {
            /*--------------------------------------------
            * Note Testing:
            * - copio file (sovrascrivendo) in area testing
            * - sul DB creo (se necessario) batch di testing (oppure update esistente in status 8)
            * - avvio validazione SE non ne ho altre in corso
            *
            *--------------------------------------------*/

            bool dataValidated = true;
            DateTime adesso = DateTime.Now;
            string dirFrom = e.LocalPath;
            int batchID = 0;
            string TaktCode = $"T{e.FileName.ToLower().Replace(".dxf", "")}";
            // copio su area testing
            try
            {
                // copio su folder...
                fileMover.obj.copiaFile(dirFrom, srvCadBasePath, e.FileName);
            }
            catch (Exception exc)
            {
                logger.lg.scriviLog($"{traduci("ErrFileTestImport01")}:{Environment.NewLine}{exc}");
                DLMan.taEL.insertQuery(DateTime.Now, "Copy DXF", $"{e.BatchName}", $"{e.BatchName}.{e.FileName}", $"Exception: {exc}");
            }
            // chiamo stored caricamento
            try
            {
                // chiamo procedura SQL x import...
                DLMan.taBL.createFileValBatch(e.FileName.ToLower().Replace(".dxf", ""), cadBasePath);
            }
            catch (Exception exc)
            {
                logger.lg.scriviLog($"{traduci("ErrFileTestImport02")}:{Environment.NewLine}{exc}");
                // salvo log errore...
                DLMan.taEL.insertQuery(DateTime.Now, "Import KIT TEST", $"{e.BatchName}", $"{e.BatchName}.{e.FileName}", $"Exception: {exc}");
                dataValidated = false;
            }
            try
            {
                var tabBatch = DLMan.taBL.getLastByTakt(TaktCode);
                if (tabBatch != null)
                {
                    if (tabBatch.Count > 0)
                    {
                        batchID = tabBatch[0].BatchID;
                    }
                }
            }
            catch (Exception exc)
            {
                logger.lg.scriviLog($"{traduci("ErrOrderCsvImp03")}:{Environment.NewLine}{exc}");
                dataValidated = false;
            }
            if (batchID > 0)
            {
                // FIX eventuali cadFilePath...
                DLMan.taIL.updateCadPath(cadBasePath, 0, false);
                // chiamo procedura x inserire come NON validato in FileValidation
                DLMan.taFV.upsertQuery(TaktCode, batchID, 8, 0);
                if (dataValidated)
                {
                    // avvio validazione!
                    bool newValidSent = ComLib.sendFirstValidationBatch();
                }
            }
            Response.Redirect(Request.RawUrl);
        }

        #endregion Private Methods

        #region Protected Methods

        protected void Page_Load(object sender, EventArgs e)
        {
            cmp_fileUpload.eh_FileUploaded += Cmp_fileUpload_eh_FileUploaded;
        }

        #endregion Protected Methods
    }
}