using System.Runtime.InteropServices;
using Decal.Interop.Controls;
using Decal.Interop.Inject;

namespace Decal.DecalControls
{
    [ComVisible(true)]
    [Guid("5D14557A-1268-43C6-A283-3B5B271359AA")]
    [ClassInterface(ClassInterfaceType.None)]
    [ComSourceInterfaces("Decal.Interop.Controls.ISliderEvents\0\0")]
    [ProgId("DecalControls.Slider")]
    public class SliderImpl : ControlBase, ISlider
    {
        public event ISliderEvents_DestroyEventHandler Destroy;
        public event ISliderEvents_ChangeEventHandler Change;

        private IFontCache _font;
        private int _textColor;
        private int _position;
        private int _minimum;
        private int _maximum = 100;

        public IFontCache Font { get => _font; set { _font = value; Invalidate(); } }
        public int TextColor { get => _textColor; set { _textColor = value; Invalidate(); } }

        public int SliderPosition
        {
            get => _position;
            set
            {
                int clamped = value < _minimum ? _minimum : (value > _maximum ? _maximum : value);
                if (_position != clamped)
                {
                    _position = clamped;
                    Invalidate();
                    Change?.Invoke(ID, _position);
                }
            }
        }

        public int Minimum { get => _minimum; set { _minimum = value; Invalidate(); } }
        public int Maximum { get => _maximum; set { _maximum = value; Invalidate(); } }

        protected override void OnDestroy() => Destroy?.Invoke(ID);
    }
}
