using System.Drawing;
using System.Windows.Forms;
using Elfisa.UI.Theming;

namespace Elfisa.UI.Controls
{
    /// <summary>
    /// Полоска вкладки как в оригинале («Прайс-лист»). Текст рисуется через OnPaint.
    /// </summary>
    public class ModernViewTab : Panel
    {
        private ThemeColors _theme;
        private string _tabText = string.Empty;

        public string TabText
        {
            get => _tabText;
            set
            {
                _tabText = value ?? string.Empty;
                Invalidate();
            }
        }

        public ModernViewTab()
        {
            Height = 32;
            Dock = DockStyle.Top;
            Padding = new Padding(12, 0, 12, 0);
            SetStyle(ControlStyles.AllPaintingInWmPaint |
                     ControlStyles.UserPaint |
                     ControlStyles.OptimizedDoubleBuffer |
                     ControlStyles.ResizeRedraw, true);
        }

        public void ApplyTheme(ThemeColors theme)
        {
            _theme = theme;
            Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            var theme = _theme ?? ThemeManager.Colors;
            var g = e.Graphics;
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

            using (var brush = new SolidBrush(theme.Surface))
            {
                g.FillRectangle(brush, ClientRectangle);
            }

            using (var pen = new Pen(theme.Border))
            {
                g.DrawLine(pen, 0, Height - 1, Width, Height - 1);
            }

            if (!string.IsNullOrEmpty(_tabText))
            {
                var textRect = new Rectangle(Padding.Left, 0, Width - Padding.Horizontal, Height);
                TextRenderer.DrawText(
                    g,
                    _tabText,
                    theme.FontSemibold14,
                    textRect,
                    theme.TextPrimary,
                    TextFormatFlags.VerticalCenter | TextFormatFlags.Left | TextFormatFlags.SingleLine);
            }
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
        }
    }
}
