using System.ComponentModel.DataAnnotations;
using NodePipeline.Abstractions.Attributes;
using NodePipeline.Abstractions.Attributes.Validation.NodeField;
using NodePipeline.Abstractions.Interfaces.Nodes;
using NodePipeline.Abstractions.Models;

namespace NodePipeline.Application.Nodes;

[NodeType("FileImageDestination")]
public class SaveImageNode : INode
{
    [NodeField(FieldDirection.Input, "input")]
    public NodeField<ImageBuffer> Input { get; set; } = null!;

    [NodeField(FieldDirection.Parameter)]
    [Required]
    public NodeField<string> FileName { get; set; } = null!;

    [NodeField(FieldDirection.Parameter)]
    [Required]
    [HasDefaultValue(".jpg")]
    [HasValidator(typeof(ExtensionValidator))]
    public NodeField<string> Extension { get; set; } = null!;

    [NodeField(FieldDirection.Parameter)]
    [Required]
    public NodeField<string> Directory { get; set; } = null!;

    public void Execute()
    {
        var img = Input.Value;
        var directory = !string.IsNullOrWhiteSpace(Directory.Value)
            ? Directory.Value
            : AppDomain.CurrentDomain.BaseDirectory;
        // var path = Path.Combine(Directory.Value, FileName.Value + "." + Extension.Value.TrimStart('.'));
        var path = Path.Combine(directory, FileName.Value + "." + Extension.Value.TrimStart('.'));
        ImageSaver.Save(img, path);
    }
}