using System;
using System.ComponentModel;

namespace OpenCVStudio_v0
{
    public sealed class GetRectSubPixBlockProperties
    {
        private readonly ProjectManager projectManager;
        private readonly ProjectBlock block;
        private double centerX;
        private double centerY;
        private int width;
        private int height;

        public GetRectSubPixBlockProperties(ProjectManager projectManager, ProjectBlock block)
        {
            this.projectManager = projectManager ?? throw new ArgumentNullException(nameof(projectManager));
            this.block = block ?? throw new ArgumentNullException(nameof(block));
            centerX = projectManager.GetBlockParameter(block.Id, "CenterX", 100.0);
            centerY = projectManager.GetBlockParameter(block.Id, "CenterY", 100.0);
            width = projectManager.GetBlockParameter(block.Id, "Width", 100);
            height = projectManager.GetBlockParameter(block.Id, "Height", 100);
        }

        [Category("Центр")]
        [DisplayName("X")]
        public double CenterX
        {
            get => centerX;
            set
            {
                centerX = value;
                Save("CenterX", value);
            }
        }

        [Category("Центр")]
        [DisplayName("Y")]
        public double CenterY
        {
            get => centerY;
            set
            {
                centerY = value;
                Save("CenterY", value);
            }
        }

        [Category("Размер")]
        [DisplayName("Ширина")]
        public int Width
        {
            get => width;
            set
            {
                width = Math.Max(1, value);
                Save("Width", width);
            }
        }

        [Category("Размер")]
        [DisplayName("Высота")]
        public int Height
        {
            get => height;
            set
            {
                height = Math.Max(1, value);
                Save("Height", height);
            }
        }

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

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