using System;
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 ModernUploadZone : Panel
    {
        private readonly Label _title;
        private readonly Label _subtitle;
        private ThemeColors _theme;

        public ModernUploadZone()
        {
            Dock = DockStyle.Fill;
            Padding = new Padding(40);
            SetStyle(ControlStyles.AllPaintingInWmPaint |
                     ControlStyles.UserPaint |
                     ControlStyles.OptimizedDoubleBuffer |
                     ControlStyles.ResizeRedraw, true);

            var stack = new Panel
            {
                Dock = DockStyle.None,
                Width = 520,
                Height = 120,
                BackColor = Color.Transparent,
                Tag = "keep-bg"
            };

            _title = new Label
            {
                Text = "Загрузка прайс-листа и синхронизация",
                Dock = DockStyle.Top,
                Height = 34,
                TextAlign = ContentAlignment.MiddleCenter,
                BackColor = Color.Transparent
            };
            _subtitle = new Label
            {
                Text = "Перетащите файл XML или выберите с диска. Данные отправятся на сервер после проверки.",
                Dock = DockStyle.Top,
                Height = 52,
                TextAlign = ContentAlignment.TopCenter,
                BackColor = Color.Transparent,
                Tag = "muted"
            };
            stack.Controls.Add(_subtitle);
            stack.Controls.Add(_title);
            Controls.Add(stack);

            Resize += (_, __) => CenterStack(stack);
            CenterStack(stack);
        }

        public void ApplyTheme(ThemeColors theme)
        {
            _theme = theme;
            _title.Font = theme.FontSemibold16;
            _title.ForeColor = theme.AccentPrimary;
            _subtitle.Font = theme.FontRegular13;
            _subtitle.ForeColor = theme.TextMuted;
            Invalidate();
        }

        private void CenterStack(Control stack)
        {
            stack.Left = Math.Max(40, (Width - stack.Width) / 2);
            stack.Top = Math.Max(40, (Height - stack.Height) / 2 - 20);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            var theme = _theme ?? ThemeManager.Colors;
            var g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;

            var rect = new Rectangle(32, 24, Width - 64, Height - 48);
            using (var fill = new SolidBrush(theme.AccentPrimarySoft))
            {
                GraphicsExtensions.FillRoundedRectangle(g, fill, rect, theme.CornerRadiusMedium);
            }

            using (var pen = new Pen(theme.AccentPrimaryBorder, 2f))
            {
                pen.DashStyle = DashStyle.Dash;
                GraphicsExtensions.DrawRoundedRectangle(g, pen, rect, theme.CornerRadiusMedium);
            }
        }

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