namespace PerpetuumSoft.Widgets
{
    using PerpetuumSoft.Widgets.Licensing;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    [LicenseProvider(typeof(WidgetsLicenseProvider)), DefaultEvent("ValueChanged"), ToolboxBitmap(typeof(AdvancedSlider)), DefaultProperty("Value")]
    public class AdvancedSlider : AdvancedSliderBase
    {
        private Color activeBarColor = Color.Red;
        private Color activeLedColor = Color.LightGreen;
        private BevelStyle barBevel = BevelStyle.None;
        private Color inactiveBarColor = Color.Gainsboro;
        private Color inactiveLedColor = Color.Green;
        private Image sliderHotImage = null;
        private Image sliderImage = null;
        private Size sliderSize = new Size(20, 20);
        private PerpetuumSoft.Widgets.SliderStyle sliderStyle = PerpetuumSoft.Widgets.SliderStyle.Led;

        protected LinearGradientBrush CreateColorsGradient(Rectangle gradRect, float k)
        {
            Color color = GraphicsUtils.ScaleColor(Color.Lime, k);
            Color color2 = GraphicsUtils.ScaleColor(Color.Yellow, k);
            Color color3 = GraphicsUtils.ScaleColor(Color.Red, k);
            int num = (this.Orientation == Orientation.Horizontal) ? 180 : 90;
            LinearGradientBrush brush = new LinearGradientBrush(gradRect, color3, color, (float) num);
            ColorBlend blend = new ColorBlend(6);
            blend.get_Positions()[0] = 0f;
            blend.get_Positions()[1] = 0.1f;
            blend.get_Positions()[2] = 0.3f;
            blend.get_Positions()[3] = 0.45f;
            blend.get_Positions()[4] = 0.8f;
            blend.get_Positions()[5] = 1f;
            blend.get_Colors()[0] = color3;
            blend.get_Colors()[1] = color3;
            blend.get_Colors()[2] = color2;
            blend.get_Colors()[3] = color2;
            blend.get_Colors()[4] = color;
            blend.get_Colors()[5] = color;
            brush.set_InterpolationColors(blend);
            return brush;
        }

        protected GraphicsPath CreatePath(Rectangle ir)
        {
            GraphicsPath path = new GraphicsPath();
            if (this.Orientation == Orientation.Horizontal)
            {
                path.StartFigure();
                path.AddLine(ir.X, ir.Y, ir.X + ir.Width, ir.Y);
                path.AddLine(ir.X + ir.Width, ir.Y + ir.Height, ir.X, ir.Y + ir.Height);
                path.CloseFigure();
                return path;
            }
            path.StartFigure();
            path.AddLine(ir.X, ir.Y, ir.X, ir.Y + ir.Height);
            path.AddLine(ir.X + ir.Width, ir.Y + ir.Height, ir.X + ir.Width, ir.Y);
            path.CloseFigure();
            return path;
        }

        protected LinearGradientBrush CreateTransparencyGradient(Rectangle gradRect)
        {
            Color color = Color.FromArgb(0x80, 0, 0, 0);
            Color.FromArgb(0x80, 0xff, 0xff, 0xff);
            Color.FromArgb(0, 0, 0, 0);
            Color color2 = Color.FromArgb(100, 0, 0, 0);
            int num = (this.Orientation == Orientation.Horizontal) ? 90 : 0;
            LinearGradientBrush brush = new LinearGradientBrush(gradRect, color2, color, (float) num);
            ColorBlend blend = new ColorBlend(4);
            blend.get_Positions()[0] = 0f;
            blend.get_Positions()[1] = 0.25f;
            blend.get_Positions()[2] = 0.5f;
            blend.get_Positions()[3] = 1f;
            blend.get_Colors()[0] = Color.FromArgb(0x80, 0, 0, 0);
            blend.get_Colors()[1] = Color.FromArgb(0x80, 0xff, 0xff, 0xff);
            blend.get_Colors()[2] = Color.FromArgb(0, 0, 0, 0);
            blend.get_Colors()[3] = Color.FromArgb(100, 0, 0, 0);
            brush.set_InterpolationColors(blend);
            return brush;
        }

        protected override void DrawBar(Graphics g)
        {
            g.set_SmoothingMode(4);
            Rectangle barRectangle = base.BarRectangle;
            using (GraphicsPath path = this.CreatePath(barRectangle))
            {
                Rectangle gradRect = barRectangle;
                if (this.Orientation == Orientation.Horizontal)
                {
                    gradRect.Inflate(barRectangle.Height / 2, 0);
                }
                else
                {
                    gradRect.Inflate(0, barRectangle.Width / 2);
                }
                using (Brush brush = new SolidBrush(this.inactiveBarColor))
                {
                    g.FillPath(brush, path);
                }
                if (base.Value > this.Minimum)
                {
                    Rectangle ir = barRectangle;
                    if (this.Orientation == Orientation.Horizontal)
                    {
                        ir.Width = this.ValueToPoint(base.Value).X - ir.X;
                    }
                    else
                    {
                        ir.Y = this.ValueToPoint(base.Value).Y;
                        ir.Height = (barRectangle.Y + barRectangle.Height) - ir.Y;
                    }
                    using (GraphicsPath path2 = this.CreatePath(ir))
                    {
                        using (Brush brush2 = new SolidBrush(this.activeBarColor))
                        {
                            g.FillPath(brush2, path2);
                        }
                    }
                }
                using (Brush brush3 = this.CreateTransparencyGradient(gradRect))
                {
                    g.FillPath(brush3, path);
                }
                g.set_SmoothingMode(0);
                GraphicsUtils.DrawBevel(g, base.BarRectangle, this.barBevel);
            }
        }

        protected override void DrawSlider(Graphics g)
        {
            Point point = this.ValueToPoint(base.Value);
            Rectangle rect = new Rectangle(new Point(point.X - (this.sliderSize.Width / 2), point.Y - (this.sliderSize.Height / 2)), this.sliderSize);
            if (this.sliderStyle == PerpetuumSoft.Widgets.SliderStyle.Image)
            {
                if (this.sliderImage != null)
                {
                    Image sliderImage = this.sliderImage;
                    if (base.Capture && (this.sliderHotImage != null))
                    {
                        sliderImage = this.sliderHotImage;
                    }
                    g.DrawImageUnscaled(sliderImage, point.X - (sliderImage.Size.Width / 2), point.Y - (sliderImage.Size.Height / 2));
                }
            }
            else
            {
                using (Brush brush = new LinearGradientBrush(rect, Color.White, Color.Gray, (float) (0x2d + ((base.Capture && (this.sliderStyle == PerpetuumSoft.Widgets.SliderStyle.Value)) ? 180 : 0))))
                {
                    g.FillRectangle(brush, rect);
                }
                GraphicsUtils.DrawBevel(g, rect, BevelStyle.Raised);
                if (this.sliderStyle == PerpetuumSoft.Widgets.SliderStyle.Led)
                {
                    Brush brush2;
                    if (base.Capture)
                    {
                        brush2 = new SolidBrush(this.activeLedColor);
                    }
                    else
                    {
                        brush2 = new SolidBrush(this.inactiveLedColor);
                    }
                    Rectangle rectangle2 = rect;
                    rectangle2.Inflate(-3, -3);
                    g.FillRectangle(brush2, rectangle2);
                    brush2.Dispose();
                }
                if (this.sliderStyle == PerpetuumSoft.Widgets.SliderStyle.Value)
                {
                    StringFormat format = new StringFormat();
                    format.LineAlignment = StringAlignment.Center;
                    format.Alignment = StringAlignment.Center;
                    using (Brush brush3 = new SolidBrush(this.ForeColor))
                    {
                        g.DrawString(base.FormatValue(base.Value), this.Font, brush3, rect, format);
                    }
                }
            }
        }

        [Category("Appearance"), Description("Color of the active part of bar"), DefaultValue(typeof(Color), "Red")]
        public Color ActiveBarColor
        {
            get
            {
                return this.activeBarColor;
            }
            set
            {
                if (value != this.activeBarColor)
                {
                    this.activeBarColor = value;
                    base.Invalidate();
                }
            }
        }

        [Description("Led color in active state"), Category("Appearance"), DefaultValue(typeof(Color), "LightGreen")]
        public Color ActiveLedColor
        {
            get
            {
                return this.activeLedColor;
            }
            set
            {
                if (value != this.activeLedColor)
                {
                    this.activeLedColor = value;
                    base.Invalidate();
                }
            }
        }

        [Description("Style of slider bar belel"), Category("Appearance"), DefaultValue(0)]
        public BevelStyle BarBevel
        {
            get
            {
                return this.barBevel;
            }
            set
            {
                if (value != this.barBevel)
                {
                    this.barBevel = value;
                    base.Invalidate();
                }
            }
        }

        protected override Size DefaultSize
        {
            get
            {
                return new Size(0xc0, 100);
            }
        }

        [DefaultValue(typeof(Color), "Gainsboro"), Description("Color of the inactive part of bar"), Category("Appearance")]
        public Color InactiveBarColor
        {
            get
            {
                return this.inactiveBarColor;
            }
            set
            {
                if (value != this.inactiveBarColor)
                {
                    this.inactiveBarColor = value;
                    base.Invalidate();
                }
            }
        }

        [DefaultValue(typeof(Color), "Green"), Description("Led color in inactive state"), Category("Appearance")]
        public Color InactiveLedColor
        {
            get
            {
                return this.inactiveLedColor;
            }
            set
            {
                if (value != this.inactiveLedColor)
                {
                    this.inactiveLedColor = value;
                    base.Invalidate();
                }
            }
        }

        [Category("Appearance"), Description("Image for slider in hot state"), DefaultValue((string) null)]
        public Image SliderHotImage
        {
            get
            {
                return this.sliderHotImage;
            }
            set
            {
                if (value != this.sliderHotImage)
                {
                    this.sliderHotImage = value;
                    base.Invalidate();
                }
            }
        }

        [DefaultValue((string) null), Category("Appearance"), Description("Image for slider")]
        public Image SliderImage
        {
            get
            {
                return this.sliderImage;
            }
            set
            {
                if (value != this.sliderImage)
                {
                    this.sliderImage = value;
                    base.Invalidate();
                }
            }
        }

        [Category("Appearance"), Description("Size of the slider")]
        public Size SliderSize
        {
            get
            {
                return this.sliderSize;
            }
            set
            {
                if (value != this.sliderSize)
                {
                    this.sliderSize = value;
                    base.Invalidate();
                }
            }
        }

        [Description("Style of slider "), Category("Appearance"), DefaultValue(0)]
        public PerpetuumSoft.Widgets.SliderStyle SliderStyle
        {
            get
            {
                return this.sliderStyle;
            }
            set
            {
                if (value != this.sliderStyle)
                {
                    this.sliderStyle = value;
                    base.Invalidate();
                }
            }
        }
    }
}
