using SteamWare;
using System;
using System.IO;
using System.Web.UI;

namespace NKC_WF
{
    public partial class KitRequestImporter : BasePage
    {
        #region Protected Fields

        /// <summary>
        /// Folder REMOTA x copia verso SQL
        /// </summary>
        protected string _SqlCopyDir = memLayer.ML.CRS("_SqlCopyDir");

        /// <summary>
        /// Folder x SQL import
        /// </summary>
        protected string _SqlImportDir = memLayer.ML.CRS("_SqlImportDir");

        #endregion Protected Fields

        #region Private Methods

        private void Cmp_fileUpload_eh_doRefresh(object sender, EventArgs e)
        {
            doUpdate();
        }

        /// <summary>
        /// Ora continuo processing
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Cmp_fileUpload_eh_FileUploaded(object sender, WebUserContols.FileUploadEventArgs e)
        {
            cmp_kitImpCheck.Visible = true;
            DateTime adesso = DateTime.Now;
            string dirFrom = e.LocalPath;
            string dirTo = $"{_SqlCopyDir}{adesso.ToString("yyyy-MM")}\\";
            string dirImport = $"{_SqlImportDir}{adesso.ToString("yyyy-MM")}\\";
            // copio su server SQL
            try
            {
                // copio su SQL...
                fileMover.obj.copiaFile(dirFrom, dirTo, e.FileName);
            }
            catch (Exception exc)
            {
                logger.lg.scriviLog($"{traduci("ErrKitReqImpCopyError")}:{Environment.NewLine}{exc}");
            }
            // salvo dati
            cmp_kitImpCheck.fileName = e.FileName;
            cmp_kitImpCheck.batchName = e.BatchName;

            // pulizia eventuali vecchi file errore in "dirTo"
            string[] file2del = Directory.GetFiles(dirTo, "error*");
            if (file2del.Length > 0)
            {
                logger.lg.scriviLog($"Trovati {file2del.Length} file errore, li elimino");
                foreach (var item in file2del)
                {
                    File.Delete(item);
                    logger.lg.scriviLog($"Eliminato {item}");
                }
            }

            // chiamo procedure check...
            cmp_kitImpCheck.tryImport(true);
            doUpdate();
        }

        private void Cmp_kitImpCheck_eh_doRefresh(object sender, EventArgs e)
        {
            doUpdate();
        }

        private void cmp_kitImpCheck_eh_doReset(object sender, EventArgs e)
        {
            Response.Redirect(Request.RawUrl);
        }

        #endregion Private Methods

        #region Protected Methods

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                doUpdate();
                cmp_kitImpCheck.Visible = false;
            }
            cmp_kitImpCheck.eh_doRefresh += Cmp_kitImpCheck_eh_doRefresh;
            cmp_kitImpCheck.eh_doReset += cmp_kitImpCheck_eh_doReset;
            cmp_fileUpload.eh_doRefresh += Cmp_fileUpload_eh_doRefresh;
            cmp_fileUpload.eh_FileUploaded += Cmp_fileUpload_eh_FileUploaded;
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            doUpdate();
        }

        #endregion Protected Methods

        #region Public Methods

        public void doUpdate()
        {
            cmp_kitList.doUpdate();
        }

        #endregion Public Methods
    }
}