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

namespace NodePipeline.Application.Nodes;

[HasNodeValidator(typeof(FileImageSourceNodeValidator))]
public class FileImageSourceNode : INode
{
    [NodeField(FieldDirection.Parameter)]
    [Required]
    [HasMinLength(1)]
    public NodeField<string> FileName { get; set; } = null!; //FileName

    [NodeField(FieldDirection.Parameter)]
    [Required]
    [HasLengthBetween(1, 10)]
    public NodeField<string> Extension { get; set; } = null!;

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

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

    [NodeField(FieldDirection.Output)] public NodeField<bool> ImageLoaded { get; set; } = null!;

    public void Execute()
    {
        var path = Path.Combine(Directory.Value, FileName.Value + "." + Extension.Value.TrimStart('.'));
        try
        {
            var image = ImageLoader.Load(path);
            Output.Value = image;
            ImageLoaded.Value = true;
        }
        catch
        {
            Output.Value = null;
            ImageLoaded.Value = false;
        }
    }
}