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 ModernPanel : Panel

    {

        private ThemeColors _theme;

        private bool _elevated;



        public bool Elevated

        {

            get => _elevated;

            set

            {

                _elevated = value;

                Invalidate();

            }

        }



        public ModernPanel()

        {

            SetStyle(ControlStyles.AllPaintingInWmPaint |

                     ControlStyles.UserPaint |

                     ControlStyles.OptimizedDoubleBuffer |

                     ControlStyles.ResizeRedraw, true);

            Padding = new Padding(16);

        }



        public void ApplyTheme(ThemeColors theme)

        {

            _theme = theme;

            BackColor = Color.Transparent;

            ForeColor = theme.TextPrimary;

            Invalidate();

        }



        protected override void OnPaint(PaintEventArgs e)

        {

            var theme = _theme ?? ThemeManager.Colors;

            var g = e.Graphics;

            g.SmoothingMode = SmoothingMode.AntiAlias;



            var cardRect = new Rectangle(0, 0, Width - 1, Height - 1);

            var radius = theme.CornerRadiusMedium;

            var fill = _elevated ? theme.SurfaceElevated : theme.Surface;



            if (_elevated)

            {

                var shadowRect = new Rectangle(2, 3, Width - 5, Height - 5);

                using (var shadowBrush = new SolidBrush(Color.FromArgb(14, theme.Shadow)))

                {

                    GraphicsExtensions.FillRoundedRectangle(g, shadowBrush, shadowRect, radius);

                }

            }



            using (var brush = new SolidBrush(fill))

            {

                GraphicsExtensions.FillRoundedRectangle(g, brush, cardRect, radius);

            }



            using (var pen = new Pen(theme.Border, 1f))

            {

                GraphicsExtensions.DrawRoundedRectangle(g, pen, cardRect, radius);

            }

        }



        protected override void OnPaintBackground(PaintEventArgs e)

        {

        }

    }

}


