using PixiEditor.ChangeableDocument.Changeables.Graph.Context;
using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
using PixiEditor.ChangeableDocument.Rendering;
using Drawie.Backend.Core;
using Drawie.Backend.Core.ColorsImpl;
using Drawie.Backend.Core.Shaders.Generation.Expressions;
using Drawie.Backend.Core.Surfaces;
using Drawie.Numerics;

namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;

[NodeInfo("ModifyImageLeft")]
[PairNode(typeof(ModifyImageRightNode), "ModifyImageZone", true)]
public class ModifyImageLeftNode : Node, IPairNode, IPreviewRenderable
{
    public InputProperty<Texture?> Image { get; }

    public FuncOutputProperty<Float2> Coordinate { get; }

    public FuncOutputProperty<Half4> Color { get; }

    public Guid OtherNode { get; set; }
    
    public ModifyImageLeftNode()
    {
        Image = CreateInput<Texture?>("Surface", "IMAGE", null);
        Coordinate = CreateFuncOutput("Coordinate", "UV", ctx => ctx.OriginalPosition);
        Color = CreateFuncOutput("Color", "COLOR", GetColor);
    }
    
    private Half4 GetColor(FuncContext context)
    {
        context.ThrowOnMissingContext();
        
        if(Image.Value == null)
        {
            return new Half4("") { ConstantValue = Colors.Transparent };
        }

        return context.SampleSurface(Image.Value.DrawingSurface, context.SamplePosition);
    }

    protected override void OnExecute(RenderContext context)
    {
    }

    public override Node CreateCopy() => new ModifyImageLeftNode();
    public RectD? GetPreviewBounds(int frame, string elementToRenderName = "")
    {
        if(Image.Value == null)
        {
            return null;
        } 
        
        return new RectD(0, 0, Image.Value.Size.X, Image.Value.Size.Y);
    }

    public bool RenderPreview(DrawingSurface renderOn, ChunkResolution resolution, int frame, string elementToRenderName)
    {
        if(Image.Value is null)
        {
            return false;
        }

        renderOn.Canvas.DrawSurface(Image.Value.DrawingSurface, 0, 0); 
        return true;
    }
}
