using System;
using System.ComponentModel;

namespace OpenCVStudio_v0
{
    public sealed class FastNlMeansDenoisingColoredBlockProperties
    {
        private readonly ProjectManager projectManager;
        private readonly ProjectBlock block;
        private float h;
        private float hColor;
        private int templateWindowSize;
        private int searchWindowSize;

        public FastNlMeansDenoisingColoredBlockProperties(ProjectManager projectManager, ProjectBlock block)
        {
            this.projectManager = projectManager ?? throw new ArgumentNullException(nameof(projectManager));
            this.block = block ?? throw new ArgumentNullException(nameof(block));
            h = projectManager.GetBlockParameter(block.Id, "H", 10f);
            hColor = projectManager.GetBlockParameter(block.Id, "HColor", 10f);
            templateWindowSize = projectManager.GetBlockParameter(block.Id, "TemplateWindowSize", 7);
            searchWindowSize = projectManager.GetBlockParameter(block.Id, "SearchWindowSize", 21);
        }

        [Category("Шум")]
        [DisplayName("Яркость H")]
        public float H
        {
            get => h;
            set
            {
                h = Math.Max(0, value);
                Save("H", h);
            }
        }

        [Category("Шум")]
        [DisplayName("Цвет H")]
        public float HColor
        {
            get => hColor;
            set
            {
                hColor = Math.Max(0, value);
                Save("HColor", hColor);
            }
        }

        [Category("Окно")]
        [DisplayName("Размер шаблона")]
        public int TemplateWindowSize
        {
            get => templateWindowSize;
            set
            {
                templateWindowSize = Math.Max(3, value | 1);
                Save("TemplateWindowSize", templateWindowSize);
            }
        }

        [Category("Окно")]
        [DisplayName("Размер поиска")]
        public int SearchWindowSize
        {
            get => searchWindowSize;
            set
            {
                searchWindowSize = Math.Max(3, value | 1);
                Save("SearchWindowSize", searchWindowSize);
            }
        }

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

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