using System;
using System.ComponentModel;

namespace OpenCVStudio_v0
{
    /// <summary>
    /// Свойства блока добавления значения.
    /// </summary>
    public sealed class AddScalarBlockProperties
    {
        private readonly ProjectManager projectManager;
        private readonly ProjectBlock projectBlock;
        private double value;

        public AddScalarBlockProperties(ProjectManager projectManager, ProjectBlock projectBlock)
        {
            this.projectManager = projectManager ?? throw new ArgumentNullException(nameof(projectManager));
            this.projectBlock = projectBlock ?? throw new ArgumentNullException(nameof(projectBlock));
            value = projectManager.GetBlockParameter(projectBlock.Id, "Value", 0.0);
        }

        [Category("Параметры")]
        [DisplayName("Значение")]
        [Description("Значение, добавляемое к каждому пикселю.")]
        public double Value
        {
            get => value;
            set
            {
                double newValue = Math.Clamp(value, -255, 255);
                if (Math.Abs(this.value - newValue) < double.Epsilon)
                {
                    return;
                }
                this.value = newValue;
                projectManager.SetBlockParameter(projectBlock.Id, "Value", newValue);
            }
        }

        [Category("Информация")]
        [DisplayName("Описание")]
        [ReadOnly(true)]
        public string Description => "Добавляет постоянное значение к изображению.";

        [Category("Блок")]
        [DisplayName("Название")]
        [ReadOnly(true)]
        public string BlockName => projectBlock.Name ?? string.Empty;

        [Category("Блок")]
        [DisplayName("Тип")]
        [ReadOnly(true)]
        public string BlockType => projectBlock.BlockType ?? string.Empty;

        [Category("Блок")]
        [DisplayName("Позиция")]
        [ReadOnly(true)]
        public int Position => projectBlock.Order + 1;
    }
}