namespace InstrumentationControls
{
	using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    [ToolboxBitmap(typeof(Toggle))]
    public class Toggle : ButtonBase
    {
        private Color activeLedColor = Color.LightGreen;
        private FaceStyle faceStyle = InstrumentationControls.FaceStyle.RaisedNotch;
        private Color inactiveLedColor = Color.Green;

        public Toggle()
        {
            this.Behaviour = ButtonBehaviour.CheckBox;
        }

        protected override void Draw(Graphics g, Rectangle r)
        {
            base.Draw(g, r);
            this.DrawToggle(g);
        }

        private void DrawToggle(Graphics g)
        {
            Brush brush2;
            Rectangle workRectangle = this.WorkRectangle;
            workRectangle.Height /= 2;
            if (!base.Pressed)
            {
                workRectangle.Y += workRectangle.Height;
            }
            workRectangle.Width--;
            workRectangle.Height--;
            Point location = workRectangle.Location;
            Point point2 = workRectangle.Location + workRectangle.Size;
            Pen pen = SystemPens.ControlLightLight;
            Pen pen2 = SystemPens.ControlDark;
            Rectangle rectangle3 = workRectangle;
            Brush brush = new LinearGradientBrush(rectangle3, Color.White, Color.Gray, 0f);
            g.FillRectangle(brush, workRectangle);
            brush.Dispose();
            g.DrawLine(pen, location.X, location.Y, location.X, point2.Y);
            g.DrawLine(pen, location.X, location.Y, point2.X, location.Y);
            g.DrawLine(pen2, location.X, point2.Y, point2.X, point2.Y);
            g.DrawLine(pen2, point2.X, location.Y, point2.X, point2.Y);
            if (this.FaceStyle != InstrumentationControls.FaceStyle.Smooth)
            {
                for (int i = 8; i <= (workRectangle.Height - 4); i += 3)
                {
                    if (this.FaceStyle == InstrumentationControls.FaceStyle.LoweredNotch)
                    {
                        g.DrawLine(pen2, workRectangle.X + 2, workRectangle.Y + i, (workRectangle.X + workRectangle.Width) - 4, workRectangle.Y + i);
                        g.DrawLine(pen, workRectangle.X + 2, (workRectangle.Y + i) + 1, (workRectangle.X + workRectangle.Width) - 4, (workRectangle.Y + i) + 1);
                    }
                    else
                    {
                        g.DrawLine(pen, workRectangle.X + 2, workRectangle.Y + i, (workRectangle.X + workRectangle.Width) - 4, workRectangle.Y + i);
                        g.DrawLine(pen2, workRectangle.X + 2, (workRectangle.Y + i) + 1, (workRectangle.X + workRectangle.Width) - 4, (workRectangle.Y + i) + 1);
                    }
                }
            }
            if (base.Pressed)
            {
                brush2 = new SolidBrush(this.activeLedColor);
            }
            else
            {
                brush2 = new SolidBrush(this.inactiveLedColor);
            }
            Rectangle rect = new Rectangle(workRectangle.X + 3, workRectangle.Y + 2, workRectangle.Width - 7, 4);
            g.FillRectangle(brush2, rect);
        }

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

        [DefaultValue(1)]
        public override ButtonBehaviour Behaviour
        {
            get
            {
                return base.Behaviour;
            }
            set
            {
                base.Behaviour = value;
            }
        }

        protected override Size DefaultSize
        {
            get
            {
                return new Size(0x20, 0x30);
            }
        }

        [Category("Appearance"), DefaultValue(1), Description("Control face appearrance")]
        public FaceStyle FaceStyle
        {
            get
            {
                return this.faceStyle;
            }
            set
            {
                if (this.faceStyle != value)
                {
                    this.faceStyle = value;
                    base.Invalidate();
                }
            }
        }

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