using EgwProxy.Ftp;
using IOB_UT_NEXT;
using IOB_UT_NEXT.Config;
using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace IOB_WIN_FTP.Iob
{
    public class GenericNext : IOB_WIN_FORM.Iob.Generic
    {
        /// <summary>
        /// Adapter generico del progetto IOB specifico
        /// </summary>
        /// <param name="caller">Form chiamante</param>
        /// <param name="IobConfFull">Configurazione (v 4.x)</param>
        public GenericNext(AdapterFormNext caller, IobConfTree IobConfFull) : base((IOB_WIN_FORM.AdapterForm)caller, IobConfFull)
        {
        }

        /// <summary>
        /// Effettua upload verso server FTP della macchina dei files nella folder indicata
        /// </summary>
        /// <param name="folderDir"></param>
        /// <returns></returns>
        protected override bool iobSendFTP(string folderDir)
        {
            bool answ = false;
            //leggo CONF
            string ftpServ = getOptPar("FTP_SERVER");
            string ftpUser = getOptPar("FTP_USER");
            string ftpPass = getOptPar("FTP_PWD");
            string ftpCert = getOptPar("FTP_CERT");
            string doSkip = getOptPar("FTP_SKIP");
            string remDir = getOptPar("FTP_REM_DIR");
            string locDir = getOptPar("FTP_LOC_DIR");
            // procedo SE TROVO conf...
            if (string.IsNullOrEmpty($"{ftpServ}{ftpUser}{ftpPass}"))
            {
                lgError($"Impossibile eseguire iobSendFTP x mancanza parametri | ftpServ: {ftpServ} | ftpUser: {ftpUser} | ftpPass: {ftpPass}");
            }
            else
            {
                bool ftpSkip = false;
                bool.TryParse(doSkip, out ftpSkip);
                var ftpClient = new Manager(ftpServ, ftpUser, ftpPass, ftpCert, ftpSkip);
                var testServer = ftpClient.ServerOk();
                if (testServer)
                {
                    lgInfo($"FTP: server found at {ftpServ}");
                    var srvType = ftpClient.ServerType();
                    lgInfo($"FTP Server type: {srvType}");

                    var preTest = ftpClient.DirExists(remDir);
                    if (!preTest)
                    {
                        var dirCreate = ftpClient.CreateDir(remDir);
                        lgInfo($"FTP: created remote dir {remDir}");
                    }

                    // test directory...
                    string basePath = Application.StartupPath;

                    string localPath = Path.Combine(basePath, locDir);
                    lgInfo($"basePath: {basePath} | localPath: {localPath}");
                    var fileList = Directory.GetFiles(localPath, "*.csv");
                    int numfile = 0;
                    foreach (var file in fileList)
                    {
                        string fName = file.Split('\\').Last();
                        string remName = $"{remDir}\\{fName}";
                        ftpClient.SendFile(file, remName);
                        numfile++;
                    }
                    var dirUploaded = (numfile == fileList.Count());
                    if (dirUploaded)
                    {
                        lgInfo($"FTP: uploaded dir content {locDir} --> {remDir}");
                    }
                    // se ok --> sposto invio
                    DateTime adesso = DateTime.Now;
                    string archBasePath = Path.Combine(basePath, "DATA", "HIST", $"{adesso:yyyy}");
                    baseUtils.checkDir(archBasePath);
                    string archPath = Path.Combine(archBasePath, $"{adesso:MMddHHmmss}");
                    baseUtils.checkDir(archPath);
                    lgInfo($"Richiesta Dir Move | {localPath} | {archPath}");
                    foreach (var item in fileList)
                    {
                        string fName = item.Split('\\').Last();
                        string destName = $"{archPath}\\{fName}";
                        File.Move(item, destName);
                    }
                    //Directory.Move(localPath, archBasePath);
                    lgInfo($"FTP: Archived dir content {localPath} --> {archPath}");
                }
            }
            return answ;
        }
    }
}