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
{
    /// <summary>
    /// Экран «Загрузить» — карточка с пунктирной рамкой и кнопками (Pharma Trust).
    /// </summary>
    public class ModernUploadPanel : Panel
    {
        private readonly ModernButton _uploadBtn;
        private readonly ModernButton _downloadBtn;
        private ThemeColors _theme;
        private string _title = "Загрузка прайс-листа и синхронизация";
        private string _subtitle = "Импорт PriceList.xml или обмен данными с сервером поставщика.";

        public ModernButton UploadButton => _uploadBtn;
        public ModernButton DownloadButton => _downloadBtn;

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

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

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

            _uploadBtn = new ModernButton
            {
                Name = "uploadBtn",
                Text = "Загрузить на сервер",
                ButtonStyle = ModernButtonStyle.Primary,
                Width = 220,
                Height = 40
            };
            _downloadBtn = new ModernButton
            {
                Name = "downloadBtn",
                Text = "Скачать с сервера",
                ButtonStyle = ModernButtonStyle.Secondary,
                Width = 220,
                Height = 40
            };

            Controls.Add(_downloadBtn);
            Controls.Add(_uploadBtn);
            Resize += (_, __) => LayoutButtons();
            LayoutButtons();
        }

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

        private void LayoutButtons()
        {
            var card = GetCardRect();
            var totalWidth = _uploadBtn.Width + 12 + _downloadBtn.Width;
            var left = card.X + Math.Max(24, (card.Width - totalWidth) / 2);
            var top = card.Bottom - 64;

            _uploadBtn.Location = new Point(left, top);
            _downloadBtn.Location = new Point(left + _uploadBtn.Width + 12, top);
        }

        private Rectangle GetCardRect()
        {
            var margin = 20;
            return new Rectangle(
                margin,
                margin,
                Math.Max(200, Width - margin * 2),
                Math.Max(200, Height - margin * 2));
        }

        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 card = GetCardRect();
            using (var fill = new SolidBrush(theme.AccentPrimarySoft))
            {
                GraphicsExtensions.FillRoundedRectangle(g, fill, card, theme.CornerRadiusMedium);
            }

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

            var iconRect = new Rectangle(card.X + (card.Width - 40) / 2, card.Y + 28, 40, 40);
            using (var iconBrush = new SolidBrush(Color.FromArgb(40, theme.Success)))
            {
                GraphicsExtensions.FillRoundedRectangle(g, iconBrush, iconRect, 10);
            }

            using (var iconFont = new Font("Segoe MDL2 Assets", 20f))
            {
                TextRenderer.DrawText(
                    g,
                    "\uE896",
                    iconFont,
                    iconRect,
                    theme.Success,
                    TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine);
            }

            var titleRect = new Rectangle(card.X + 24, card.Y + 78, card.Width - 48, 32);
            TextRenderer.DrawText(
                g,
                _title,
                theme.FontSemibold16,
                titleRect,
                theme.Success,
                TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine | TextFormatFlags.EndEllipsis);

            var subRect = new Rectangle(card.X + 32, card.Y + 112, card.Width - 64, 48);
            TextRenderer.DrawText(
                g,
                _subtitle,
                theme.FontRegular13,
                subRect,
                theme.TextMuted,
                TextFormatFlags.HorizontalCenter | TextFormatFlags.WordBreak);
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            var theme = _theme ?? ThemeManager.Colors;
            using (var brush = new SolidBrush(theme.BackgroundPrimary))
            {
                e.Graphics.FillRectangle(brush, ClientRectangle);
            }
        }
    }
}
