using System;
using System.Drawing;
using System.Windows.Forms;
using AutoIngest.Core;

namespace AutoIngest.App
{
    /// <summary>
    /// First-run welcome dialog, shown only on a brand-new install (no config file at load time).
    /// Two pages: a welcome screen (Skip / Customize) and the branding editor. Finishing either
    /// path sets <c>SetupCompleted = true</c> and persists config so the wizard never re-appears.
    /// Skipping leaves branding off (defaults) — the user can always opt in later via the tray.
    /// </summary>
    public class WelcomeForm : Form
    {
        readonly AppConfig _config;
        BrandingEditorPanel? _editor;

        public WelcomeForm(AppConfig config)
        {
            _config = config;
            BuildUi();
            ShowWelcomePage();
        }

        void BuildUi()
        {
            Text = "Welcome to AutoIngest";
            StartPosition = FormStartPosition.CenterScreen;
            ClientSize = new Size(760, 640);
            FormBorderStyle = FormBorderStyle.FixedDialog;
            MaximizeBox = false;
            MinimizeBox = false;
            BackColor = Theme.BG;
            ForeColor = Theme.TEXT;
            Font = new Font("Segoe UI", 9F);
        }

        void ShowWelcomePage()
        {
            Controls.Clear();
            var host = new Panel { Dock = DockStyle.Fill, BackColor = Theme.BG };
            Controls.Add(host);

            var title = new Label
            {
                Text = "Welcome to AutoIngest",
                Dock = DockStyle.Top,
                Height = 40,
                Font = new Font("Segoe UI", 16F, FontStyle.Bold),
                ForeColor = Theme.ACCENT,
                BackColor = Theme.BG,
                TextAlign = ContentAlignment.MiddleLeft,
                Padding = new Padding(24, 0, 24, 0)
            };
            host.Controls.Add(title);

            var body = new Label
            {
                Dock = DockStyle.Top,
                Height = 180,
                ForeColor = Theme.TEXT,
                BackColor = Theme.BG,
                Padding = new Padding(24, 8, 24, 8),
                Text =
                    "AutoIngest moves and converts photos off your camera or phone into a dated\r\n" +
                    "folder, ready to attach to your listings.\r\n\r\n" +
                    "Optionally, you can brand every converted photo with your store name, a logo\r\n" +
                    "watermark, and a copyright notice — great for a consistent eBay storefront.\r\n\r\n" +
                    "Want to set that up now? You can skip and enable it any time from the tray\r\n" +
                    "menu (Options ▸ Branding).",
                TextAlign = ContentAlignment.TopLeft
            };
            host.Controls.Add(body);

            var buttonRow = new Panel { Dock = DockStyle.Bottom, Height = 48, BackColor = Theme.BG };
            host.Controls.Add(buttonRow);

            var customize = new Button
            {
                Text = "Customize branding",
                FlatStyle = FlatStyle.Flat,
                BackColor = Theme.ACCENT,
                ForeColor = Color.White,
                Location = new Point(ClientSize.Width - 350, 10),
                Size = new Size(160, 30)
            };
            customize.Click += (_, _) => ShowBrandingPage();

            var skip = new Button
            {
                Text = "Skip for now",
                FlatStyle = FlatStyle.Flat,
                BackColor = Theme.BG2,
                ForeColor = Theme.TEXT,
                Location = new Point(ClientSize.Width - 175, 10),
                Size = new Size(160, 30)
            };
            skip.Click += (_, _) => Finish();

            buttonRow.Controls.Add(customize);
            buttonRow.Controls.Add(skip);
            AcceptButton = customize;
        }

        void ShowBrandingPage()
        {
            Controls.Clear();

            var header = new Label
            {
                Text = "  Set up your branding",
                Dock = DockStyle.Top,
                Height = 32,
                Font = new Font("Segoe UI", 11F, FontStyle.Bold),
                ForeColor = Theme.ACCENT,
                BackColor = Theme.BG2,
                TextAlign = ContentAlignment.MiddleLeft
            };
            Controls.Add(header);

            _editor = new BrandingEditorPanel(_config);
            Controls.Add(_editor);

            var bottom = new Panel { Dock = DockStyle.Bottom, Height = 44, BackColor = Theme.BG };
            Controls.Add(bottom);

            var back = new Button
            {
                Text = "Back",
                FlatStyle = FlatStyle.Flat,
                BackColor = Theme.BG2,
                ForeColor = Theme.TEXT,
                Location = new Point(12, 8),
                Size = new Size(90, 28)
            };
            back.Click += (_, _) => { _editor = null; ShowWelcomePage(); };

            var finish = new Button
            {
                Text = "Finish",
                FlatStyle = FlatStyle.Flat,
                BackColor = Theme.ACCENT,
                ForeColor = Color.White,
                Location = new Point(ClientSize.Width - 200, 8),
                Size = new Size(85, 28)
            };
            finish.Click += (_, _) =>
            {
                _editor?.Save();
                Finish();
            };

            var skip = new Button
            {
                Text = "Skip",
                FlatStyle = FlatStyle.Flat,
                BackColor = Theme.BG2,
                ForeColor = Theme.TEXT,
                Location = new Point(ClientSize.Width - 105, 8),
                Size = new Size(85, 28)
            };
            skip.Click += (_, _) => Finish();

            bottom.Controls.Add(back);
            bottom.Controls.Add(finish);
            bottom.Controls.Add(skip);
            AcceptButton = finish;

            // Render an initial preview once the page is laid out.
            BeginInvoke((Action)(() => _editor?.RenderPreview()));
        }

        /// <summary>Mark setup complete and close. Always persists so the wizard never re-shows.</summary>
        void Finish()
        {
            _config.SetupCompleted = true;
            ConfigManager.SaveConfig(_config);
            DialogResult = DialogResult.OK;
            Close();
        }
    }
}
