using System.Drawing.Drawing2D;

namespace TravelAgencyWinForms.Utils;

public static class AppTheme
{
    public static Color Background => Color.FromArgb(243, 251, 248);
    public static Color Surface => Color.White;
    public static Color SurfaceAlt => Color.FromArgb(234, 247, 243);
    public static Color Primary => Color.FromArgb(129, 203, 188);
    public static Color PrimaryDark => Color.FromArgb(54, 121, 111);
    public static Color PrimarySoft => Color.FromArgb(214, 240, 233);
    public static Color Border => Color.FromArgb(202, 227, 220);
    public static Color Text => Color.FromArgb(44, 67, 62);
    public static Color Muted => Color.FromArgb(99, 127, 121);
    public static Color Danger => Color.FromArgb(205, 110, 110);

    public static void ApplyForm(Form form, bool dialog = false)
    {
        form.BackColor = Background;
        form.Font = new Font("Segoe UI", 10F, FontStyle.Regular, GraphicsUnit.Point);
        form.ForeColor = Text;
        if (dialog)
            form.FormBorderStyle = FormBorderStyle.FixedDialog;
    }

    public static void StylePrimaryButton(Button button)
    {
        StyleButton(button, Primary, PrimaryDark, Color.White);
    }

    public static void StyleSecondaryButton(Button button)
    {
        StyleButton(button, Surface, Border, Text);
    }

    public static void StyleSelectedButton(Button button)
    {
        StyleButton(button, Primary, PrimaryDark, Color.White);
    }

    public static void StyleDangerButton(Button button)
    {
        StyleButton(button, Color.FromArgb(251, 235, 235), Danger, Danger);
    }

    private static void StyleButton(Button button, Color backColor, Color borderColor, Color foreColor)
    {
        button.FlatStyle = FlatStyle.Flat;
        button.FlatAppearance.BorderSize = 1;
        button.FlatAppearance.BorderColor = borderColor;
        button.BackColor = backColor;
        button.ForeColor = foreColor;
        button.Cursor = Cursors.Hand;
        button.Margin = new Padding(4);
        button.Padding = new Padding(8, 4, 8, 4);
        button.AutoSize = false;
    }

    public static void StyleMenuButton(Button button)
    {
        button.FlatStyle = FlatStyle.Flat;
        button.FlatAppearance.BorderSize = 0;
        button.BackColor = Color.Transparent;
        button.ForeColor = Color.White;
        button.Cursor = Cursors.Hand;
        button.TextAlign = ContentAlignment.MiddleLeft;
        button.Font = new Font("Segoe UI Semibold", 10F, FontStyle.Regular, GraphicsUnit.Point);
        button.Padding = new Padding(16, 0, 0, 0);
    }

    public static void StyleTextBox(TextBox textBox)
    {
        textBox.BorderStyle = BorderStyle.FixedSingle;
        textBox.BackColor = Color.White;
        textBox.ForeColor = Text;
        textBox.Margin = new Padding(4);
    }

    public static void StyleComboBox(ComboBox comboBox)
    {
        comboBox.FlatStyle = FlatStyle.Flat;
        comboBox.BackColor = Color.White;
        comboBox.ForeColor = Text;
        comboBox.Margin = new Padding(4);
    }

    public static void StyleGrid(DataGridView grid)
    {
        grid.BackgroundColor = Surface;
        grid.BorderStyle = BorderStyle.None;
        grid.GridColor = Border;
        grid.EnableHeadersVisualStyles = false;
        grid.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
        grid.ColumnHeadersDefaultCellStyle.BackColor = PrimarySoft;
        grid.ColumnHeadersDefaultCellStyle.ForeColor = Text;
        grid.ColumnHeadersDefaultCellStyle.SelectionBackColor = Primary;
        grid.ColumnHeadersDefaultCellStyle.SelectionForeColor = Color.White;
        grid.ColumnHeadersDefaultCellStyle.Font = new Font("Segoe UI Semibold", 10F);
        grid.DefaultCellStyle.SelectionBackColor = Color.FromArgb(224, 244, 239);
        grid.DefaultCellStyle.SelectionForeColor = Text;
        grid.DefaultCellStyle.BackColor = Color.White;
        grid.DefaultCellStyle.ForeColor = Text;
        grid.DefaultCellStyle.WrapMode = DataGridViewTriState.False;
        grid.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(249, 253, 252);
        grid.RowHeadersVisible = false;
        grid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
        grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        grid.MultiSelect = false;
        grid.AllowUserToResizeRows = false;
        grid.AllowUserToAddRows = false;
        grid.AllowUserToDeleteRows = false;
        grid.ReadOnly = true;
    }

    public static Panel CreateCard(string title, string value, string? subtitle = null)
    {
        var panel = new Panel
        {
            Width = 220,
            Height = 108,
            BackColor = Surface,
            BorderStyle = BorderStyle.FixedSingle,
            Margin = new Padding(6),
            Padding = new Padding(14)
        };

        var titleLabel = new Label
        {
            Text = title,
            Dock = DockStyle.Top,
            Height = 22,
            ForeColor = Muted,
            Font = new Font("Segoe UI", 9.5F, FontStyle.Regular)
        };

        var valueLabel = new Label
        {
            Text = value,
            Dock = DockStyle.Top,
            Height = 38,
            ForeColor = Text,
            Font = new Font("Segoe UI Semibold", 20F, FontStyle.Regular)
        };

        panel.Controls.Add(valueLabel);
        panel.Controls.Add(titleLabel);

        if (!string.IsNullOrWhiteSpace(subtitle))
        {
            var subtitleLabel = new Label
            {
                Text = subtitle,
                Dock = DockStyle.Bottom,
                Height = 18,
                ForeColor = Muted,
                Font = new Font("Segoe UI", 8.5F, FontStyle.Regular)
            };
            panel.Controls.Add(subtitleLabel);
        }

        return panel;
    }

    public static GroupBox CreateSection(string title)
    {
        return new GroupBox
        {
            Text = title,
            Dock = DockStyle.Fill,
            Padding = new Padding(12),
            ForeColor = Text,
            Font = new Font("Segoe UI Semibold", 10F, FontStyle.Regular)
        };
    }

    public static PictureBox CreatePictureBox(Image image, Size size)
    {
        return new PictureBox
        {
            Image = image,
            SizeMode = PictureBoxSizeMode.Zoom,
            Size = size,
            BackColor = Color.Transparent
        };
    }

    public static Panel CreateHeaderPanel(string title, string subtitle)
    {
        var panel = new Panel
        {
            Height = 86,
            Dock = DockStyle.Top,
            BackColor = Surface,
            Padding = new Padding(20, 16, 20, 16),
            Margin = new Padding(0, 0, 0, 10),
            BorderStyle = BorderStyle.FixedSingle
        };

        var titleLabel = new Label
        {
            Text = title,
            Dock = DockStyle.Top,
            Height = 30,
            Font = new Font("Segoe UI Semibold", 18F, FontStyle.Regular),
            ForeColor = Text
        };

        var subtitleLabel = new Label
        {
            Text = subtitle,
            Dock = DockStyle.Fill,
            Font = new Font("Segoe UI", 9.5F, FontStyle.Regular),
            ForeColor = Muted
        };

        panel.Controls.Add(subtitleLabel);
        panel.Controls.Add(titleLabel);
        return panel;
    }

    public static Image? LoadImage(string fileName)
    {
        var candidates = new[]
        {
            Path.Combine(AppContext.BaseDirectory, "Assets", fileName),
            Path.Combine(Application.StartupPath, "Assets", fileName),
            Path.Combine(AppContext.BaseDirectory, fileName),
            Path.Combine(Directory.GetCurrentDirectory(), "Assets", fileName),
            Path.Combine(Directory.GetCurrentDirectory(), "TravelAgencyWinForms", "Assets", fileName)
        };

        foreach (var path in candidates)
        {
            if (!File.Exists(path))
                continue;

            using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            using var image = Image.FromStream(stream);
            return new Bitmap(image);
        }

        return null;
    }
}
