using System;
using System.Collections.Generic;
using System.Text;

using System.ComponentModel;
namespace OpenCVStudio_v0
{
    public sealed class SimpleThresholdBlockProperties
    {
        private readonly ProjectManager projectManager; private readonly ProjectBlock projectBlock;
        private double thresholdValue; private double maxValue; private SimpleThresholdType thresholdType;
        public SimpleThresholdBlockProperties(ProjectManager projectManager, ProjectBlock projectBlock)
        {
            this.projectManager = projectManager ?? throw new ArgumentNullException(nameof(projectManager));
            this.projectBlock = projectBlock ?? throw new ArgumentNullException(nameof(projectBlock));
            thresholdValue = projectManager.GetBlockParameter(projectBlock.Id, "ThresholdValue", 128.0);
            maxValue = projectManager.GetBlockParameter(projectBlock.Id, "MaxValue", 255.0);
            thresholdType = projectManager.GetBlockParameter(projectBlock.Id, "ThresholdType", SimpleThresholdType.Binary);
        }
        [Category("Порог")]
        [DisplayName("Тип порога")]
        [Description("Способ выполнения пороговой обработки.")]
        public SimpleThresholdType ThresholdType
        {
            get => thresholdType;
            set
            {
                if (thresholdType == value) return;
                thresholdType = value;
                projectManager.SetBlockParameter(projectBlock.Id, "ThresholdType", value);
            }
        }
        [Category("Порог")]
        [DisplayName("Значение порога")]
        [Description("Порог яркости от 0 до 255.")]
        public double ThresholdValue
        {
            get => thresholdValue;
            set
            {
                ValidateByteRange(value, "Значение порога");
                if (thresholdValue == value) return;
                thresholdValue = value;
                projectManager.SetBlockParameter(projectBlock.Id, "ThresholdValue", value);
            }
        }
        [Category("Порог")]
        [DisplayName("Максимальное значение")]
        [Description("Значение белого пикселя после бинаризации.")]
        public double MaxValue
        {
            get => maxValue;
            set
            {
                ValidateByteRange(value, "Максимальное значение");
                if (maxValue == value) return;
                maxValue = value;
                projectManager.SetBlockParameter(projectBlock.Id, "MaxValue", value);
            }
        }
        [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;
        private static void ValidateByteRange(double value, string parameterName)
        {
            if (value < 0 || value > 255) throw new ArgumentOutOfRangeException(nameof(value), value, $"{parameterName} должно быть от 0 до 255.");
        }
    }
}