using Hawkeye.VisionBuilder.Workflow.Datatypes;
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
using OpenCvSharp;

namespace Hawkeye.VisionBuilder.Workflow.Operations.Filters;

[Category("Filters")]
public class GaborFilterOperation : BaseOperation
{
    public GaborFilterOperation()
    {
        CanHaveProcessingError = false;
    }

    public int KSize { get; set; } = 35;
    public double Sigma { get; set; } = 3;
    public int NumFilters { get; set; } = 1;
    public double Lambda { get; set; } = 10;
    public double Gamma { get; set; } = 0.5;
    public double ThetaFromAngle { get; set; } = 0;
    public double ThetaToAngle { get; set; } = 0;

    protected override void InterpretInternal(Context context)
    {
        Result = true;
        if (!CheckImageExists(context)) return;

        var thetaFrom = ThetaFromAngle / 180 * Math.PI;
        var thetaTo = ThetaToAngle / 180 * Math.PI;


        double[] steps;
        if (NumFilters == 1)
        {
            steps=new double[] {thetaFrom};
        }
        else
        {
            // check theta not equal
            if (thetaFrom == thetaTo)
            {
                Status = "ThetaFrom and ThetaTo must be different";
                Result = false;
                return;
            }
            var step = (thetaTo - thetaFrom) / NumFilters;

            steps = Enumerable.Range(0, NumFilters).Select(i => thetaFrom + i * step).ToArray();
        }
        
        var image = context.ActiveImage.ImageData;
        var newimage = new Mat(image.Size(), image.Type(), Scalar.All(0));

        foreach (double theta in steps)
        {
            var kernel = Cv2.GetGaborKernel(new Size(KSize, KSize), Sigma, theta, Lambda, Gamma, 0, MatType.CV_64F);
            kernel /= 1 * kernel.Sum().Val0;
            var filtered = new Mat(image.Size(), image.Type(), Scalar.All(0));
            Cv2.Filter2D(image, filtered, -1, kernel);
            Cv2.Max(newimage, filtered, newimage);
        }

        context.ActiveImage = new HawkeyeImage()
        {
            ImageData = newimage
        };

        Result = true;

    }

    public override void Save(BinaryWriter bw)
    {
        base.Save(bw);
        bw.Write(KSize);
        bw.Write(Sigma);
        bw.Write(NumFilters);
        bw.Write(Lambda);
        bw.Write(Gamma);
        bw.Write(ThetaFromAngle);
        bw.Write(ThetaToAngle);
        
    }

    public override void Load(BinaryReader br)
    {
        base.Load(br);
        KSize = br.ReadInt32();
        Sigma = br.ReadDouble();
        NumFilters = br.ReadInt32();
        Lambda = br.ReadDouble();
        Gamma = br.ReadDouble();
        ThetaFromAngle = br.ReadDouble();
        ThetaToAngle = br.ReadDouble();
        
    }

    public override void Save(Dictionary<string, object> dict)
    {
        base.Save(dict);
        dict[nameof(KSize)] = KSize;
        dict[nameof(Sigma)] = Sigma;
        dict[nameof(NumFilters)] = NumFilters;
        dict[nameof(Lambda)] = Lambda;
        dict[nameof(Gamma)] = Gamma;
        dict[nameof(ThetaFromAngle)] = ThetaFromAngle;
        dict[nameof(ThetaToAngle)] = ThetaToAngle;
    }

    public override void Load(Dictionary<string, object> dict)
    {
        base.Load(dict);
        if (dict.ContainsKey(nameof(KSize)))
            KSize = Convert.ToInt32(dict[nameof(KSize)]);
        if (dict.ContainsKey(nameof(Sigma)))
            Sigma = Convert.ToDouble(dict[nameof(Sigma)]);
        if (dict.ContainsKey(nameof(NumFilters)))
            NumFilters = Convert.ToInt32(dict[nameof(NumFilters)]);
        if (dict.ContainsKey(nameof(Lambda)))
            Lambda = Convert.ToDouble(dict[nameof(Lambda)]);
        if (dict.ContainsKey(nameof(Gamma)))
            Gamma = Convert.ToDouble(dict[nameof(Gamma)]);
        if (dict.ContainsKey(nameof(ThetaFromAngle)))
            ThetaFromAngle = Convert.ToDouble(dict[nameof(ThetaFromAngle)]);
        if (dict.ContainsKey(nameof(ThetaToAngle)))
            ThetaToAngle = Convert.ToDouble(dict[nameof(ThetaToAngle)]);
    }
}