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

namespace Elfisa.UI.Dialogs
{
    public sealed class ModernMessageForm : Form
    {
        public ModernMessageForm(string title, string message, bool showCancel)
            : this(title, message, showCancel ? ModernMessageButtons.YesNoCancel : ModernMessageButtons.Ok)
        {
        }

        public ModernMessageForm(string title, string message, ModernMessageButtons buttons)
        {
            FormBorderStyle = FormBorderStyle.None;
            StartPosition = FormStartPosition.CenterParent;
            ShowInTaskbar = false;
            Size = new Size(440, 220);
            Padding = new Padding(20);

            var titleLabel = new Label
            {
                Text = title,
                Dock = DockStyle.Top,
                Height = 34,
                Font = ThemeManager.Colors.FontSemibold16,
                BackColor = Color.Transparent
            };
            var messageLabel = new Label
            {
                Text = message,
                Dock = DockStyle.Fill,
                Font = ThemeManager.Colors.FontRegular14,
                BackColor = Color.Transparent
            };

            var buttonPanel = new FlowLayoutPanel
            {
                Dock = DockStyle.Bottom,
                Height = 48,
                FlowDirection = FlowDirection.RightToLeft,
                WrapContents = false
            };

            switch (buttons)
            {
                case ModernMessageButtons.YesNo:
                    AddButton(buttonPanel, "Нет", ModernButtonStyle.Secondary, DialogResult.No);
                    AddButton(buttonPanel, "Да", ModernButtonStyle.Primary, DialogResult.Yes);
                    break;
                case ModernMessageButtons.YesNoCancel:
                    AddButton(buttonPanel, "Отмена", ModernButtonStyle.Secondary, DialogResult.Cancel);
                    AddButton(buttonPanel, "Нет", ModernButtonStyle.Secondary, DialogResult.No);
                    AddButton(buttonPanel, "Да", ModernButtonStyle.Primary, DialogResult.Yes);
                    break;
                default:
                    AddButton(buttonPanel, "OK", ModernButtonStyle.Primary, DialogResult.OK);
                    break;
            }

            Controls.Add(messageLabel);
            Controls.Add(buttonPanel);
            Controls.Add(titleLabel);

            ApplyTheme(ThemeManager.Colors);
        }

        private void ApplyTheme(ThemeColors theme)
        {
            BackColor = theme.Surface;
            ForeColor = theme.TextPrimary;

            foreach (Control control in Controls)
            {
                if (control is Label label)
                {
                    label.ForeColor = theme.TextPrimary;
                    label.BackColor = Color.Transparent;
                }
                else if (control is FlowLayoutPanel panel)
                {
                    panel.BackColor = theme.Surface;
                    panel.ForeColor = theme.TextPrimary;
                    foreach (Control child in panel.Controls)
                    {
                        if (child is ModernButton button)
                        {
                            button.ApplyTheme(theme);
                        }
                    }
                }
            }
        }

        private void AddButton(FlowLayoutPanel panel, string text, ModernButtonStyle style, DialogResult result)
        {
            var button = new ModernButton { Text = text, Width = 100, ButtonStyle = style };
            button.ApplyTheme(ThemeManager.Colors);
            button.Click += (_, __) =>
            {
                DialogResult = result;
                Close();
            };
            panel.Controls.Add(button);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            using (var pen = new Pen(ThemeManager.Colors.Border))
            {
                e.Graphics.DrawRectangle(pen, 0, 0, Width - 1, Height - 1);
            }
        }
    }
}
