using System;
using System.ComponentModel;

namespace OpenCVStudio_v0
{
    public sealed class MagnitudeBlockProperties
    {
        private readonly ProjectManager projectManager;
        private readonly ProjectBlock block;
        private MagnitudeDirection direction;
        private int kernelSize;
        private bool useBlur;
        private int blurSize;
        private double scale;

        public MagnitudeBlockProperties(ProjectManager projectManager, ProjectBlock block)
        {
            this.projectManager = projectManager ?? throw new ArgumentNullException(nameof(projectManager));
            this.block = block ?? throw new ArgumentNullException(nameof(block));
            direction = projectManager.GetBlockParameter(block.Id, "Direction", MagnitudeDirection.Both);
            kernelSize = projectManager.GetBlockParameter(block.Id, "KernelSize", 3);
            useBlur = projectManager.GetBlockParameter(block.Id, "UseBlur", true);
            blurSize = projectManager.GetBlockParameter(block.Id, "BlurSize", 3);
            scale = projectManager.GetBlockParameter(block.Id, "Scale", 1.0);
        }

        [Category("Градиент")]
        [DisplayName("Направление")]
        public MagnitudeDirection Direction
        {
            get => direction;
            set
            {
                direction = value;
                Save("Direction", value);
            }
        }

        [Category("Sobel")]
        [DisplayName("Размер ядра")]
        public int KernelSize
        {
            get => kernelSize;
            set
            {
                kernelSize = value % 2 == 0 ? value + 1 : Math.Max(3, value);
                Save("KernelSize", kernelSize);
            }
        }

        [Category("Размытие")]
        [DisplayName("Использовать Blur")]
        public bool UseBlur
        {
            get => useBlur;
            set
            {
                useBlur = value;
                Save("UseBlur", value);
            }
        }

        [Category("Размытие")]
        [DisplayName("Размер Blur")]
        public int BlurSize
        {
            get => blurSize;
            set
            {
                blurSize = value % 2 == 0 ? value + 1 : Math.Max(3, value);
                Save("BlurSize", blurSize);
            }
        }

        [Category("Sobel")]
        [DisplayName("Scale")]
        public double Scale
        {
            get => scale;
            set
            {
                scale = value;
                Save("Scale", value);
            }
        }

        private void Save(string name, object value)
        {
            projectManager.SetBlockParameter(block.Id, name, value);
        }

        [Category("Блок")]
        [ReadOnly(true)]
        public string Name => block.Name;
    }
}