using System;
using System.ComponentModel;

namespace OpenCVStudio_v0
{
    public sealed class CopyMakeBorderBlockProperties
    {
        private readonly ProjectManager projectManager;
        private readonly ProjectBlock block;
        private int top;
        private int bottom;
        private int left;
        private int right;
        private BorderTypeMode borderType;
        private int value;

        public CopyMakeBorderBlockProperties(ProjectManager projectManager, ProjectBlock block)
        {
            this.projectManager = projectManager ?? throw new ArgumentNullException(nameof(projectManager));
            this.block = block ?? throw new ArgumentNullException(nameof(block));
            top = projectManager.GetBlockParameter(block.Id, "Top", 10);
            bottom = projectManager.GetBlockParameter(block.Id, "Bottom", 10);
            left = projectManager.GetBlockParameter(block.Id, "Left", 10);
            right = projectManager.GetBlockParameter(block.Id, "Right", 10);
            borderType = projectManager.GetBlockParameter(block.Id, "BorderType", BorderTypeMode.Constant);
            value = projectManager.GetBlockParameter(block.Id, "Value", 0);
        }

        [Category("Размер")]
        [DisplayName("Сверху")]
        public int Top
        {
            get => top;
            set
            {
                top = Math.Max(0, value);
                Save("Top", top);
            }
        }

        [Category("Размер")]
        [DisplayName("Снизу")]
        public int Bottom
        {
            get => bottom;
            set
            {
                bottom = Math.Max(0, value);
                Save("Bottom", bottom);
            }
        }

        [Category("Размер")]
        [DisplayName("Слева")]
        public int Left
        {
            get => left;
            set
            {
                left = Math.Max(0, value);
                Save("Left", left);
            }
        }

        [Category("Размер")]
        [DisplayName("Справа")]
        public int Right
        {
            get => right;
            set
            {
                right = Math.Max(0, value);
                Save("Right", right);
            }
        }

        [Category("Заполнение")]
        [DisplayName("Тип рамки")]
        public BorderTypeMode BorderType
        {
            get => borderType;
            set
            {
                borderType = value;
                Save("BorderType", value);
            }
        }

        [Category("Заполнение")]
        [DisplayName("Значение")]
        public int Value
        {
            get => value;
            set
            {
                this.value = Math.Clamp(value, 0, 255);
                Save("Value", this.value);
            }
        }

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

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