/*
 * wallpaper-gen.c - Zygaena marine-dark wallpaper generator.
 *
 * Renders a 1920x1080 PNG: deep-ocean vertical gradient, low-alpha
 * concentric sonar arcs from a lower-left origin, a low-alpha stylized
 * hammerhead-shark silhouette, and the HAMMERHEAD/ZYGAENA wordmark in
 * the lower-right corner.
 *
 * Build: gcc wallpaper-gen.c $(pkg-config --cflags --libs cairo) -lm -o wg
 * Usage: ./wg /path/to/output.png
 */
#include <cairo.h>
#include <math.h>
#include <stdio.h>

#define W 1920
#define H 1080

/* Zygaena marine-dark palette */
#define BG_TOP_R    0x05/255.0
#define BG_TOP_G    0x08/255.0
#define BG_TOP_B    0x0d/255.0
#define BG_BOT_R    0x0a/255.0
#define BG_BOT_G    0x27/255.0
#define BG_BOT_B    0x33/255.0
#define TEAL_R      0x1f/255.0
#define TEAL_G      0xb6/255.0
#define TEAL_B      0xb6/255.0
#define TEAL_BR_R   0x3f/255.0
#define TEAL_BR_G   0xd0/255.0
#define TEAL_BR_B   0xd0/255.0
#define MUTED_R     0x5a/255.0
#define MUTED_G     0x75/255.0
#define MUTED_B     0x75/255.0

static void
draw_background(cairo_t *cr)
{
    cairo_pattern_t *grad = cairo_pattern_create_linear(0, 0, 0, H);
    cairo_pattern_add_color_stop_rgb(grad, 0.0, BG_TOP_R, BG_TOP_G, BG_TOP_B);
    cairo_pattern_add_color_stop_rgb(grad, 1.0, BG_BOT_R, BG_BOT_G, BG_BOT_B);
    cairo_set_source(cr, grad);
    cairo_paint(cr);
    cairo_pattern_destroy(grad);
}

static void
draw_sonar_arcs(cairo_t *cr)
{
    /* Origin lower-left, off canvas a bit so rings sweep across the
     * bottom-left quadrant. */
    double cx = -80, cy = H + 80;
    int rings = 5;
    double base_r = 260;
    double step_r = 220;

    for (int i = 0; i < rings; i++) {
        double alpha = 0.12 - (i * (0.06 / (rings - 1)));
        if (alpha < 0.06) alpha = 0.06;
        cairo_set_source_rgba(cr, TEAL_R, TEAL_G, TEAL_B, alpha);
        cairo_set_line_width(cr, 2.0);
        cairo_arc(cr, cx, cy, base_r + i * step_r, -M_PI / 2.2, 0.15);
        cairo_stroke(cr);
    }
}

/*
 * Stylized hammerhead-shark silhouette: a wide flattened "hammer" head
 * bar with an eye-dot at each end, a tapering dorsal body, and a
 * crescent tail - all in one low-alpha teal fill, large and off-center
 * (upper-left-of-center so it doesn't collide with the wordmark).
 */
static void
draw_hammerhead_motif(cairo_t *cr)
{
    double ox = 560, oy = 430;   /* motif origin (center of the head bar) */
    double scale = 1.55;

    cairo_save(cr);
    cairo_translate(cr, ox, oy);
    cairo_scale(cr, scale, scale);
    cairo_set_source_rgba(cr, TEAL_R, TEAL_G, TEAL_B, 0.10);

    /* Hammer head: a wide, slightly bowed bar. */
    cairo_move_to(cr, -260, 0);
    cairo_curve_to(cr, -180, -34,  180, -34,  260, 0);
    cairo_curve_to(cr,  180,  34, -180,  34, -260, 0);
    cairo_close_path(cr);
    cairo_fill(cr);

    /* Tapering body/tail sweeping down-right from the head's center. */
    cairo_move_to(cr, -40, 18);
    cairo_curve_to(cr, 120, 60,  260, 160,  360, 340);
    cairo_curve_to(cr, 380, 380,  340, 400,  300, 380);
    cairo_curve_to(cr, 220, 220,  90, 100, -40, 54);
    cairo_close_path(cr);
    cairo_fill(cr);

    /* Tail fluke. */
    cairo_move_to(cr, 340, 320);
    cairo_curve_to(cr, 400, 330, 440, 300, 470, 250);
    cairo_curve_to(cr, 450, 310, 420, 360, 380, 400);
    cairo_curve_to(cr, 360, 370, 345, 345, 340, 320);
    cairo_close_path(cr);
    cairo_fill(cr);

    /* Eye-dots at each end of the hammer bar. */
    cairo_arc(cr, -235, 0, 10, 0, 2 * M_PI);
    cairo_fill(cr);
    cairo_arc(cr, 235, 0, 10, 0, 2 * M_PI);
    cairo_fill(cr);

    cairo_restore(cr);
}

/* Draw text with manual letter-spacing (cairo toy text API has no
 * built-in tracking control). */
static void
draw_tracked_text(cairo_t *cr, const char *s, double x, double y, double tracking)
{
    cairo_text_extents_t ext;
    double cx = x;
    char buf[2] = {0, 0};

    for (const char *p = s; *p; p++) {
        buf[0] = *p;
        cairo_text_extents(cr, buf, &ext);
        cairo_move_to(cr, cx, y);
        cairo_show_text(cr, buf);
        cx += ext.x_advance + tracking;
    }
}

static void
draw_wordmark(cairo_t *cr)
{
    cairo_text_extents_t ext;

    cairo_select_font_face(cr, "sans-serif", CAIRO_FONT_SLANT_NORMAL,
        CAIRO_FONT_WEIGHT_BOLD);
    cairo_set_font_size(cr, 64);
    cairo_set_source_rgb(cr, TEAL_R, TEAL_G, TEAL_B);

    /* Measure full tracked width to right-align at x = W - 120. */
    const char *word = "HAMMERHEAD";
    double tracking = 8.0;
    double total_w = 0;
    char buf[2] = {0, 0};
    for (const char *p = word; *p; p++) {
        buf[0] = *p;
        cairo_text_extents(cr, buf, &ext);
        total_w += ext.x_advance + tracking;
    }
    total_w -= tracking;

    double x = W - 120 - total_w;
    double y = H - 150;
    draw_tracked_text(cr, word, x, y, tracking);

    /* Subtitle */
    cairo_select_font_face(cr, "sans-serif", CAIRO_FONT_SLANT_NORMAL,
        CAIRO_FONT_WEIGHT_NORMAL);
    cairo_set_font_size(cr, 22);
    cairo_set_source_rgb(cr, MUTED_R, MUTED_G, MUTED_B);

    const char *sub = "ZYGAENA";
    double sub_tracking = 6.0;
    double sub_w = 0;
    for (const char *p = sub; *p; p++) {
        buf[0] = *p;
        cairo_text_extents(cr, buf, &ext);
        sub_w += ext.x_advance + sub_tracking;
    }
    sub_w -= sub_tracking;

    draw_tracked_text(cr, sub, W - 120 - sub_w, y + 40, sub_tracking);
}

int
main(int argc, char **argv)
{
    const char *outpath = (argc > 1) ? argv[1] : "/tmp/hammerhead.png";

    cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, W, H);
    cairo_t *cr = cairo_create(surface);

    draw_background(cr);
    draw_sonar_arcs(cr);
    draw_hammerhead_motif(cr);
    draw_wordmark(cr);

    cairo_status_t st = cairo_surface_write_to_png(surface, outpath);
    if (st != CAIRO_STATUS_SUCCESS) {
        fprintf(stderr, "write_to_png failed: %s\n", cairo_status_to_string(st));
        cairo_destroy(cr);
        cairo_surface_destroy(surface);
        return 1;
    }

    printf("wrote %s (%dx%d)\n", outpath, W, H);

    cairo_destroy(cr);
    cairo_surface_destroy(surface);
    return 0;
}
