using System;
using System.ComponentModel;

namespace OpenCVStudio_v0
{
    /// <summary>
    /// Параметры блока ConvertScaleAbs.
    /// </summary>
    public sealed class ConvertScaleAbsBlockProperties
    {
        private readonly ProjectManager projectManager;
        private readonly ProjectBlock projectBlock;
        private double alpha;
        private double beta;

        public ConvertScaleAbsBlockProperties(ProjectManager projectManager, ProjectBlock projectBlock)
        {
            this.projectManager = projectManager ?? throw new ArgumentNullException(nameof(projectManager));
            this.projectBlock = projectBlock ?? throw new ArgumentNullException(nameof(projectBlock));
            alpha = projectManager.GetBlockParameter(projectBlock.Id, "Alpha", 1.0);
            beta = projectManager.GetBlockParameter(projectBlock.Id, "Beta", 0.0);
        }

        [Category("Параметры")]
        [DisplayName("Alpha")]
        [Description("Коэффициент масштаба значений пикселей. Значение 1,0 оставляет исходный масштаб.")]
        [DefaultValue(1.0)]
        public double Alpha
        {
            get => alpha;
            set
            {
                double correctedValue = Math.Clamp(value, 0.0, 100.0);
                if (Math.Abs(alpha - correctedValue) < double.Epsilon)
                {
                    return;
                }
                alpha = correctedValue;
                projectManager.SetBlockParameter(projectBlock.Id, "Alpha", correctedValue);
            }
        }

        [Category("Параметры")]
        [DisplayName("Beta")]
        [Description("Смещение, добавляемое к значениям пикселей после умножения на Alpha.")]
        [DefaultValue(0.0)]
        public double Beta
        {
            get => beta;
            set
            {
                double correctedValue = Math.Clamp(value, -10000.0, 10000.0);
                if (Math.Abs(beta - correctedValue) < double.Epsilon)
                {
                    return;
                }
                beta = correctedValue;
                projectManager.SetBlockParameter(projectBlock.Id, "Beta", correctedValue);
            }
        }

        [Category("Результат")]
        [DisplayName("Формула")]
        [ReadOnly(true)]
        public string Formula => "|Пиксель × Alpha + Beta|";

        [Category("Результат")]
        [DisplayName("Глубина")]
        [ReadOnly(true)]
        public string ResultDepth => "CV_8U";

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

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

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