using System;
using OpenCvSharp;

namespace OpenCVStudio_v0
{
    public sealed class CopyMakeBorderExecutor : IImageBlockExecutor
    {
        public string BlockType => "CopyMakeBorder";

        public Mat Execute(Mat input, ProjectBlock block, ProjectManager projectManager)
        {
            if (input == null)
                throw new ArgumentNullException(nameof(input));
            if (input.Empty())
                throw new InvalidOperationException("CopyMakeBorder: изображение пустое.");
            int top = projectManager.GetBlockParameter(block.Id, "Top", 10);
            int bottom = projectManager.GetBlockParameter(block.Id, "Bottom", 10);
            int left = projectManager.GetBlockParameter(block.Id, "Left", 10);
            int right = projectManager.GetBlockParameter(block.Id, "Right", 10);
            BorderTypeMode mode = projectManager.GetBlockParameter(block.Id, "BorderType", BorderTypeMode.Constant);
            int value = projectManager.GetBlockParameter(block.Id, "Value", 0);
            BorderTypes borderType = mode switch
            {
                BorderTypeMode.Replicate => BorderTypes.Replicate,
                BorderTypeMode.Reflect => BorderTypes.Reflect,
                BorderTypeMode.Reflect101 => BorderTypes.Reflect101,
                BorderTypeMode.Wrap => BorderTypes.Wrap,
                _ => BorderTypes.Constant
            };
            Mat result = new Mat();
            Cv2.CopyMakeBorder(input, result, top, bottom, left, right, borderType, new Scalar(value));
            return result;
        }
    }
}