using NodePipeline.Abstractions.Interfaces;
using NodePipeline.Abstractions.Models.Validation;
using NodePipeline.Abstractions.Validators;

namespace NodePipeline.Application;

public class FileImageSourceNodeValidator(IPipelineLocalizationProvider localizer)
    : NodeValidatorBase(localizer)
{
    public override NodeValidationResult Validate(Dictionary<string, object> nodeFields,
        IReadOnlyDictionary<string, List<NodeFieldValidationResult>> fieldResults,
        Dictionary<string, List<NodeFieldValidationResult>> portResults)
    {
        var path = Path.Combine((string)nodeFields["Directory"],
            string.Concat((string)nodeFields["FileName"], '.', ((string)nodeFields["Extension"]).TrimStart('.')));
        if (!Path.IsPathRooted(path)) path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
        return File.Exists(path)
            ? SuccessResult
            : new NodeValidationResult(ValidationResult.HasErrors,
                Localizer.GetLocalizedString("FileNotFound", Culture, path),
                fieldResults, portResults);
    }
}