using System.Drawing;
using System.Windows.Forms;
using Elfisa.UI.Notifications;

namespace Elfisa.UI.Notifications
{
    public sealed class ToastService
    {
        private readonly Form _host;
        private int _stackOffset;

        public ToastService(Form host)
        {
            _host = host;
            host.Resize += (_, __) => RepositionToasts();
        }

        public void Show(string title, string message, ToastType type)
        {
            var toast = new ToastControl(title, message, type)
            {
                Width = 360,
                Height = 88
            };
            _host.Controls.Add(toast);
            toast.BringToFront();
            PositionToast(toast);
            toast.Start();
            _stackOffset += 96;
        }

        private void PositionToast(ToastControl toast)
        {
            toast.Location = new Point(
                _host.ClientSize.Width - toast.Width - 20,
                _host.ClientSize.Height - toast.Height - 20 - _stackOffset);
        }

        private void RepositionToasts()
        {
            _stackOffset = 0;
            foreach (Control control in _host.Controls)
            {
                if (control is ToastControl toast)
                {
                    PositionToast(toast);
                    _stackOffset += 96;
                }
            }
        }
    }
}
