using System.IO;

namespace Shared;

public class FilePathGeneratorInfo
{

    public string To { get; protected set; }
    public string From { get; protected set; }
    public bool IsErrorFile { get; protected set; }
    public string SubFolderPath { get; protected set; }
    public string FirstDirectory { get; protected set; }
    public string ReportFullPath { get; protected set; }
    public string ResolvedFileLocation { get; protected set; }

    public FilePathGeneratorInfo(object originalFilePathGenerator, string reportFullPath, bool isErrorFile, System.Collections.Generic.Dictionary<string, string> fileParameter)
    {
        ReportFullPath = reportFullPath;
        IsErrorFile = isErrorFile;
        if (originalFilePathGenerator is null || originalFilePathGenerator is not FilePathGenerator original)
        {
            FirstDirectory = string.Empty;
            ResolvedFileLocation = string.Empty;
            To = string.Empty;
            From = string.Empty;
        }
        else
        {
            string directorySeparatorChar = Path.DirectorySeparatorChar.ToString();
            FilePathGenerator filePathGenerator = new(original.FileConnectorConfiguration, reportFullPath, isErrorFile);
            SubFolderPath = filePathGenerator.GetSubFolderPath();
            if (string.IsNullOrEmpty(SubFolderPath))
                FirstDirectory = SubFolderPath;
            else
                FirstDirectory = SubFolderPath.Split(Path.DirectorySeparatorChar)[0];
            ResolvedFileLocation = filePathGenerator.GetTargetFolder();
            if (string.IsNullOrEmpty(ResolvedFileLocation) && string.IsNullOrEmpty(SubFolderPath))
                To = string.Empty;
            else if (string.IsNullOrEmpty(SubFolderPath))
                To = ResolvedFileLocation;
            else
                To = string.Concat(ResolvedFileLocation.Replace(SubFolderPath, string.Empty), Path.DirectorySeparatorChar, FirstDirectory);
#if (true)
            if (string.IsNullOrEmpty(original.FileConnectorConfiguration.DefaultPlaceHolderValue))
                original.FileConnectorConfiguration.DefaultPlaceHolderValue = "NA";
            if (fileParameter is not null && fileParameter.Count == 1 && To.Contains(original.FileConnectorConfiguration.DefaultPlaceHolderValue))
            {
                foreach (System.Collections.Generic.KeyValuePair<string, string> keyValuePair in fileParameter)
                    To = To.Replace(string.Concat(original.FileConnectorConfiguration.DefaultPlaceHolderValue), keyValuePair.Value);
            }
#endif
            if (original.FileConnectorConfiguration.SourceFileLocation.EndsWith(directorySeparatorChar))
                From = string.Concat(original.FileConnectorConfiguration.SourceFileLocation, FirstDirectory);
            else
                From = string.Concat(original.FileConnectorConfiguration.SourceFileLocation, Path.DirectorySeparatorChar, FirstDirectory);
            if (From.EndsWith(directorySeparatorChar) && !To.EndsWith(directorySeparatorChar))
                To = string.Concat(To, Path.DirectorySeparatorChar);
            else if (To.EndsWith(directorySeparatorChar) && !From.EndsWith(directorySeparatorChar))
                To = To.Remove(To.Length - 1, 1);
        }
    }

}