using System;
using OpenCvSharp;

namespace OpenCVStudio_v0
{
    public sealed class CompareScalarExecutor : IImageBlockExecutor
    {
        public string BlockType => "CompareScalar";

        public Mat Execute(Mat input, ProjectBlock block, ProjectManager projectManager)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }
            if (block == null)
            {
                throw new ArgumentNullException(nameof(block));
            }
            if (projectManager == null)
            {
                throw new ArgumentNullException(nameof(projectManager));
            }
            if (input.Empty())
            {
                throw new InvalidOperationException("CompareScalar: входное изображение пустое.");
            }
            CompareOperation operation = projectManager.GetBlockParameter(block.Id, "Operation", CompareOperation.Greater);
            double value = projectManager.GetBlockParameter(block.Id, "Value", 128.0);
            CmpTypes compareType = operation switch
            {
                CompareOperation.Less => CmpTypes.LT,
                CompareOperation.LessEqual => CmpTypes.LE,
                CompareOperation.Equal => CmpTypes.EQ,
                CompareOperation.Greater => CmpTypes.GT,
                CompareOperation.GreaterEqual => CmpTypes.GE,
                CompareOperation.NotEqual => CmpTypes.NE,
                _ => CmpTypes.GT
            };
            Mat result = new Mat();
            Cv2.Compare(input, new Scalar(value), result, compareType);
            return result;
        }
    }
}