using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using FastReport.Utils;

namespace FastReport.Controls
{
    /// <summary>
    /// Represents the label with line.
    /// </summary>
    [ToolboxItem(false)]
    public class LabelLine : Control
    {
        /// <inheritdoc/>
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            int x = 0;
            if (RightToLeft == RightToLeft.Yes)
            {
                if (!String.IsNullOrEmpty(Text))
                {
                    x = Width - TextRenderer.MeasureText(Text, Font).Width;
                    TextRenderer.DrawText(g, Text, Font, new Point(x, 0), ForeColor, TextFormatFlags.RightToLeft);
                    x -= this.LogicalToDevice(4);
                }
                g.DrawLine(Pens.Silver, 0, Height / 2, x, Height / 2);
            }
            else
            {
                if (!String.IsNullOrEmpty(Text))
                {
                    TextRenderer.DrawText(g, Text, Font, new Point(0, 0), ForeColor);
                    x += TextRenderer.MeasureText(Text, Font).Width + this.LogicalToDevice(4);
                }
                g.DrawLine(Pens.Silver, x, Height / 2, Width, Height / 2);
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LabelLine"/> class.
        /// </summary>
        public LabelLine()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        }
    }
}
