using System.Drawing;

using System.Drawing.Drawing2D;

using System.Windows.Forms;

using Elfisa.UI.Helpers;

using Elfisa.UI.Theming;



namespace Elfisa.UI.Controls

{

    public class ModernBrandHeader : Panel

    {

        private ThemeColors _theme;

        private const string TitleText = "Электронная Фармация";

        private const string BadgeText = "ЭльФиСА";



        public ModernBrandHeader()

        {

            Height = 44;

            Dock = DockStyle.Top;

            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.SmoothingMode = SmoothingMode.AntiAlias;

            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;



            var rect = new Rectangle(0, 0, Width, Height);

            using (var brush = new SolidBrush(theme.TitleBarTop))

            {

                g.FillRectangle(brush, rect);

            }



            var iconSize = 22;

            var iconY = (Height - iconSize) / 2;

            var iconRect = new Rectangle(16, iconY, iconSize, iconSize);

            using (var iconBrush = new SolidBrush(theme.AccentPrimary))

            {

                GraphicsExtensions.FillRoundedRectangle(g, iconBrush, iconRect, 6);

            }



            using (var crossPen = new Pen(Color.White, 2f) { StartCap = LineCap.Round, EndCap = LineCap.Round })

            {

                var cx = iconRect.X + iconRect.Width / 2;

                var cy = iconRect.Y + iconRect.Height / 2;

                g.DrawLine(crossPen, cx - 5, cy, cx + 5, cy);

                g.DrawLine(crossPen, cx, cy - 5, cx, cy + 5);

            }



            TextRenderer.DrawText(

                g,

                TitleText,

                theme.FontSemibold14,

                new Rectangle(46, 0, Width - 200, Height),

                Color.White,

                TextFormatFlags.VerticalCenter | TextFormatFlags.Left | TextFormatFlags.SingleLine);



            var badgeSize = TextRenderer.MeasureText(BadgeText, theme.FontRegular12);

            var badgeRect = new Rectangle(Width - badgeSize.Width - 36, (Height - 24) / 2, badgeSize.Width + 16, 24);

            using (var badgeBrush = new SolidBrush(Color.FromArgb(40, 255, 255, 255)))

            {

                GraphicsExtensions.FillRoundedRectangle(g, badgeBrush, badgeRect, 12);

            }



            TextRenderer.DrawText(

                g,

                BadgeText,

                theme.FontRegular12,

                badgeRect,

                Color.FromArgb(200, 230, 220),

                TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine);

        }



        protected override void OnPaintBackground(PaintEventArgs e)

        {

        }

    }

}


