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

namespace Elfisa.UI.Loading
{
    public sealed class LoadingOverlay : UserControl
    {
        private readonly Label _textLabel;
        private readonly Panel _card;
        private readonly ProgressBar _progressBar;
        private readonly AnimationTimer _fadeAnim;
        private ThemeColors _theme;
        private float _opacity;

        public LoadingOverlay()
        {
            Dock = DockStyle.Fill;
            Visible = false;
            BackColor = Color.Transparent;
            SetStyle(ControlStyles.AllPaintingInWmPaint |
                     ControlStyles.UserPaint |
                     ControlStyles.OptimizedDoubleBuffer |
                     ControlStyles.ResizeRedraw |
                     ControlStyles.SupportsTransparentBackColor, true);

            _fadeAnim = new AnimationTimer(v =>
            {
                _opacity = v;
                Invalidate();
            });

            _card = new Panel
            {
                Width = 320,
                Height = 120,
                BackColor = Color.Transparent,
                Tag = "keep-bg"
            };

            _textLabel = new Label
            {
                Text = "Загрузка...",
                AutoSize = false,
                TextAlign = ContentAlignment.MiddleCenter,
                Dock = DockStyle.Top,
                Height = 44,
                BackColor = Color.Transparent,
                Tag = "keep-bg"
            };

            _progressBar = new ProgressBar
            {
                Style = ProgressBarStyle.Marquee,
                MarqueeAnimationSpeed = 25,
                Width = 240,
                Height = 6
            };

            _card.Controls.Add(_progressBar);
            _card.Controls.Add(_textLabel);
            Controls.Add(_card);
            Resize += (_, __) => CenterCard();
        }

        public void ApplyTheme(ThemeColors theme)
        {
            _theme = theme;
            _textLabel.ForeColor = theme.TextPrimary;
            _textLabel.Font = theme.FontSemibold14;
            _card.BackColor = theme.SurfaceElevated;
            Invalidate();
        }

        public void Show(string text)
        {
            _textLabel.Text = text;
            Visible = true;
            BringToFront();
            CenterCard();
            _fadeAnim.AnimateTo(1f, 180);
        }

        public void HideOverlay()
        {
            _fadeAnim.AnimateTo(0f, 150, () => Visible = false);
        }

        private void CenterCard()
        {
            _card.Left = (Width - _card.Width) / 2;
            _card.Top = (Height - _card.Height) / 2;
            _progressBar.Left = (_card.Width - _progressBar.Width) / 2;
            _progressBar.Top = 72;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (_opacity <= 0.01f)
            {
                return;
            }

            var theme = _theme ?? ThemeManager.Colors;
            var scrimAlpha = (int)(Color.FromArgb(theme.OverlayScrim.A, theme.OverlayScrim).A * _opacity);
            if (scrimAlpha < 0) scrimAlpha = 0;
            if (scrimAlpha > 255) scrimAlpha = 255;
            using (var scrim = new SolidBrush(Color.FromArgb(scrimAlpha, theme.OverlayScrim.R, theme.OverlayScrim.G, theme.OverlayScrim.B)))
            {
                e.Graphics.FillRectangle(scrim, ClientRectangle);
            }

            var cardRect = new Rectangle(_card.Left, _card.Top, _card.Width - 1, _card.Height - 1);
            var cardAlpha = (int)(255 * _opacity);
            using (var fill = new SolidBrush(Color.FromArgb(cardAlpha, theme.SurfaceElevated)))
            {
                GraphicsExtensions.FillRoundedRectangle(e.Graphics, fill, cardRect, theme.CornerRadiusMedium);
            }

            using (var pen = new Pen(Color.FromArgb(cardAlpha, theme.Border)))
            {
                GraphicsExtensions.DrawRoundedRectangle(e.Graphics, pen, cardRect, theme.CornerRadiusMedium);
            }
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                _fadeAnim.Dispose();
            }

            base.Dispose(disposing);
        }
    }
}
