using NodePipeline.Abstractions.Attributes;
using NodePipeline.Abstractions.Interfaces.Nodes;
using NodePipeline.Abstractions.Models;

namespace NodePipeline.Application.Nodes;

public interface IFileNameProvider
{
    string GetNextFileName();
}

public class SaveImage2Node : INode
{
    private readonly IFileNameProvider _fileNameProvider;


    public SaveImage2Node(IFileNameProvider fileNameProvider)
    {
        _fileNameProvider = fileNameProvider;
    }

    [NodeField(FieldDirection.Input)] public NodeField<ImageBuffer> Input { get; set; } = null!;

    public void Execute()
    {
        var img = Input.Value;
        var fileName = _fileNameProvider.GetNextFileName();
        var directory = Path.GetDirectoryName(fileName);
        if (!Directory.Exists(directory)) Directory.CreateDirectory(directory!);
        ImageSaver.Save(img, fileName);
    }
}