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

namespace Elfisa.UI.Notifications
{
    public sealed class ToastControl : UserControl
    {
        private readonly Label _titleLabel;
        private readonly Label _messageLabel;
        private readonly Timer _timer;
        private readonly AnimationTimer _fadeAnim;
        private float _opacity = 1f;

        public ToastControl(string title, string message, ToastType type)
        {
            Width = 360;
            Height = 88;
            Padding = new Padding(14);
            var theme = ThemeManager.Colors;
            BackColor = ResolveBackground(theme, type);

            _titleLabel = new Label
            {
                Text = title,
                Dock = DockStyle.Top,
                Height = 24,
                Font = theme.FontSemibold14,
                ForeColor = theme.TextPrimary,
                BackColor = Color.Transparent
            };
            _messageLabel = new Label
            {
                Text = message,
                Dock = DockStyle.Fill,
                Font = theme.FontRegular12,
                ForeColor = theme.TextSecondary,
                BackColor = Color.Transparent
            };

            Controls.Add(_messageLabel);
            Controls.Add(_titleLabel);

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

            _timer = new Timer { Interval = 4200 };
            _timer.Tick += (_, __) => BeginFadeOut();
            SetStyle(ControlStyles.AllPaintingInWmPaint |
                     ControlStyles.UserPaint |
                     ControlStyles.OptimizedDoubleBuffer |
                     ControlStyles.ResizeRedraw, true);
        }

        public void Start()
        {
            _opacity = 0f;
            _fadeAnim.AnimateTo(1f, 180);
            _timer.Start();
        }

        private void BeginFadeOut()
        {
            _timer.Stop();
            _fadeAnim.AnimateTo(0f, 200, () =>
            {
                Parent?.Controls.Remove(this);
                Dispose();
            });
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            var theme = ThemeManager.Colors;
            e.Graphics.Clear(Parent?.BackColor ?? theme.BackgroundPrimary);
            var rect = new Rectangle(0, 0, Width - 1, Height - 1);
            using (var brush = new SolidBrush(Color.FromArgb((int)(_opacity * 255), BackColor)))
            {
                GraphicsExtensions.FillRoundedRectangle(e.Graphics, brush, rect, theme.CornerRadiusMedium);
            }

            using (var pen = new Pen(Color.FromArgb((int)(_opacity * 255), theme.Border)))
            {
                GraphicsExtensions.DrawRoundedRectangle(e.Graphics, pen, rect, theme.CornerRadiusMedium);
            }

            base.OnPaint(e);
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
        }

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

            base.Dispose(disposing);
        }

        private static Color ResolveBackground(ThemeColors theme, ToastType type)
        {
            switch (type)
            {
                case ToastType.Success: return theme.SuccessSoft;
                case ToastType.Error: return theme.DangerSoft;
                case ToastType.Warning: return theme.WarningSoft;
                default: return theme.InfoSoft;
            }
        }
    }
}
