// Copyright © 2013-2026 Jonathan Vasquez <jon@xyinn.org>
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.

const dvui = @import("dvui");

const Color = dvui.Color;
const Font = dvui.Font;
const Theme = dvui.Theme;
const Options = dvui.Options;

const shard_section_padding: ?dvui.Rect = .{ .y = 8, .h = 8 };

// The border for individual cells.
const shard_section_border: ?dvui.Rect = .{ .h = 1 };

// ----- Light Mode -----

pub const window_base_color = Color.fromHex("#ffffff");
pub const text_color = Color.fromHex("#333333");

// This affects the color of the checkboxes and border of a button when it's clicked.
pub const accent = Color.fromHex("#009688");
const accent_hsl = Color.HSLuv.fromColor(accent);
const err = Color.fromHex("#E01B24");
const err_hsl = Color.HSLuv.fromColor(err);

const light_accent_accent = accent_hsl.lighten(-16).color();
const light_accent_fill = accent_hsl.color();
const light_accent_fill_hover = accent_hsl.lighten(-11).color();
const light_accent_border = accent_hsl.lighten(-22).color();

const light_err_accent = err_hsl.lighten(-15).color();
const light_err_fill = err_hsl.color();
const light_err_fill_hover = err_hsl.lighten(-10).color();
const light_err_border = err_hsl.lighten(-20).color();

pub const light_disabled_color = light_grid_band.alt_cell_opts.color_fill.?;

pub const light_grid_band: dvui.GridWidget.CellStyle.Banded = .{
    .cell_opts = .{
        .padding = shard_section_padding,
        .border = shard_section_border,
        .color_fill = window_base_color,
        .color_fill_hover = Color.fromHex("#e0e0e0"),
        .color_border = window_base_color,
    },
    .alt_cell_opts = .{
        .padding = shard_section_padding,
        .border = shard_section_border,
        .color_fill = Color.fromHex("#ebebeb"),
        .color_border = window_base_color,
    },
};

pub const light_default_highlight_style: dvui.GridWidget.CellStyle.HoveredRow = .{
    .cell_opts = light_grid_band.cell_opts,
};

pub const light_selected_highlight_style: dvui.GridWidget.CellStyle.HoveredRow = .{
    .cell_opts = .{
        .color_fill = light_grid_band.cell_opts.color_fill,
        .padding = light_grid_band.cell_opts.padding,
        .border = light_grid_band.cell_opts.border,
        .color_fill_hover = Color.fromHex("#b2dfdb"),
        .color_border = window_base_color,
    },
};

// ----- Dark Mode -----

pub const dark_window_base_color = Color.fromHex("#1d1f21");
const dark_text_color = Color.fromHex("#c5c8c6");

const dark_accent = dark_grid_band.alt_cell_opts.color_fill.?;
const dark_accent_hsl = Color.HSLuv.fromColor(dark_accent);
const dark_err = Color.fromHex("#C01C28");
const dark_err_hsl = Color.HSLuv.fromColor(dark_err);

const dark_accent_accent = accent_hsl.lighten(12).color();
const dark_accent_fill_hover = accent_hsl.lighten(9).color();
const dark_accent_border = accent_hsl.lighten(17).color();

const dark_err_accent = dark_err_hsl.lighten(14).color();
const dark_err_fill_hover = err_hsl.lighten(9).color();
const dark_err_fill_press = err_hsl.lighten(16).color();
const dark_err_border = err_hsl.lighten(20).color();

pub const dark_disabled_color = Color.fromHex("#393c40");

pub const dark_grid_band: dvui.GridWidget.CellStyle.Banded = .{
    .cell_opts = .{
        .padding = shard_section_padding,
        .border = shard_section_border,
        .color_fill = dark_window_base_color,
        .color_fill_hover = Color.fromHex("#2f3336"),
        .color_border = dark_window_base_color,
    },
    .alt_cell_opts = .{
        .padding = shard_section_padding,
        .border = shard_section_border,
        .color_fill = Color.fromHex("#26292b"),
        .color_border = dark_window_base_color,
    },
};

pub const dark_default_highlight_style: dvui.GridWidget.CellStyle.HoveredRow = .{
    .cell_opts = dark_grid_band.cell_opts,
};

pub const dark_selected_highlight_style: dvui.GridWidget.CellStyle.HoveredRow = .{
    .cell_opts = .{
        .color_fill = light_grid_band.cell_opts.color_fill,
        .padding = light_grid_band.cell_opts.padding,
        .border = light_grid_band.cell_opts.border,
        .color_fill_hover = Color.fromHex("#39403d"),
        .color_border = dark_window_base_color,
    },
};

const fonts: []const Font.Source = &.{
    .{
        .family = Font.array("Cactus"),
        .bytes = @embedFile("../../src/fonts/Roboto-Regular.ttf"),
    },
    .{
        .family = Font.array("Cactus"),
        .weight = .bold,
        .bytes = @embedFile("../../src/fonts/Roboto-Bold.ttf"),
    },
    .{
        .family = Font.array("Cactus"),
        .style = .italic,
        .bytes = @embedFile("../../src/fonts/Roboto-Italic.ttf"),
    },
    .{
        .family = Font.array("Cactus Mono"),
        .bytes = @embedFile("../../src/fonts/RobotoMono-Regular.ttf"),
    },
    .{
        .family = Font.array("Cactus Mono"),
        .weight = .bold,
        .bytes = @embedFile("../../src/fonts/RobotoMono-Bold.ttf"),
    },
    .{
        .family = Font.array("Cactus Mono"),
        .style = .italic,
        .bytes = @embedFile("../../src/fonts/RobotoMono-Italic.ttf"),
    },
};

pub const light = light: {
    @setEvalBranchQuota(3123);
    break :light Theme{
        .name = "Cactus Light",
        .dark = false,

        .embedded_fonts = fonts,

        .font_body = .find(.{ .family = "Cactus", .size = dvui.Font.DefaultSize + 1 }),
        .font_heading = .find(.{ .family = "Cactus", .weight = .bold }),
        .font_title = .find(.{ .family = "Cactus", .size = dvui.Font.DefaultSize + 2 }),
        .font_mono = .find(.{ .family = "Cactus Mono" }),

        .focus = accent,

        .fill = Color.white,
        .fill_hover = (Color.HSLuv{ .s = 0, .l = 82 }).color(),
        .fill_press = (Color.HSLuv{ .s = 0, .l = 72 }).color(),
        .text = text_color,
        .text_select = .{ .r = 0x91, .g = 0xbc, .b = 0xf0 },
        .border = (Color.HSLuv{ .s = 0, .l = 63 }).color(),

        // Normal Controls (like Buttons)
        .control = .{
            .fill = accent,
            .text = Color.white,
        },

        .window = .{
            .fill = .{ .r = 0xf0, .g = 0xf0, .b = 0xf0 },
        },

        .highlight = .{
            .fill = light_accent_fill,
            .fill_hover = light_accent_fill_hover,
            .fill_press = light_accent_accent,
            .text = Color.white,
            .border = light_accent_border,
        },

        .err = .{
            .fill = light_err_fill,
            .fill_hover = light_err_fill_hover,
            .fill_press = light_err_accent,
            .text = Color.white,
            .border = light_err_border,
        },
    };
};

pub const dark = dark: {
    @setEvalBranchQuota(3023);
    break :dark Theme{
        .name = "Cactus Dark",
        .dark = true,

        .embedded_fonts = fonts,

        .font_body = .find(.{ .family = "Cactus", .size = dvui.Font.DefaultSize + 1 }),
        .font_heading = .find(.{ .family = "Cactus", .weight = .bold }),
        .font_title = .find(.{ .family = "Cactus", .size = dvui.Font.DefaultSize + 2 }),
        .font_mono = .find(.{ .family = "Cactus Mono" }),

        .focus = dark_text_color,

        .fill = dark_accent,
        .fill_hover = dark_accent_hsl.lighten(21).color(),
        .fill_press = dark_accent_hsl.lighten(30).color(),
        .text = dark_text_color,
        .text_select = .{ .r = 0x32, .g = 0x60, .b = 0x98 },
        .border = dark_accent_hsl.lighten(39).color(),

        // Normal Controls (like Buttons)
        .control = .{
            .fill = dark_selected_highlight_style.cell_opts.color_fill_hover.?,
            .text = dark_text_color,
        },

        .window = .{
            .fill = .{ .r = 0x2b, .g = 0x2b, .b = 0x2b },
        },

        .highlight = .{
            .fill = dark_accent,
            .fill_hover = dark_accent_fill_hover,
            .fill_press = dark_accent_accent,
            .text = dark_text_color,
            .border = dark_accent_border,
        },

        .err = .{
            .fill = dark_err,
            .fill_hover = dark_err_fill_hover,
            .fill_press = dark_err_fill_press,
            .text = dark_text_color,
            .border = dark_err_border,
        },
    };
};
