/*
 * The weather glyphs, drawn rather than loaded: the shapes on Weather's
 * live tile (the shell's forecast template) and in the Weather app, here
 * so the two look alike.
 */
#define _GNU_SOURCE
#include <math.h>
#include <string.h>
#include "c8ui-private.h"

static void cloud_path(cairo_t *cr, double s) {
	cairo_new_sub_path(cr);
	cairo_arc(cr, -0.14 * s, 0.04 * s, 0.15 * s, 0, 2 * M_PI);
	cairo_new_sub_path(cr);
	cairo_arc(cr, 0.04 * s, -0.08 * s, 0.22 * s, 0, 2 * M_PI);
	cairo_new_sub_path(cr);
	cairo_arc(cr, 0.24 * s, 0.05 * s, 0.14 * s, 0, 2 * M_PI);
	cairo_rectangle(cr, -0.14 * s, 0.04 * s, 0.38 * s, 0.15 * s);
}

static void draw_cloud(cairo_t *cr, double cx, double cy, double s,
		double alpha) {
	cairo_save(cr);
	cairo_translate(cr, cx, cy);
	cairo_push_group(cr);
	cairo_set_source_rgb(cr, 1, 1, 1);
	cloud_path(cr, s);
	cairo_fill(cr);
	cairo_pop_group_to_source(cr);
	cairo_paint_with_alpha(cr, alpha);
	cairo_restore(cr);
}

static void draw_sun(cairo_t *cr, double cx, double cy, double s,
		double alpha) {
	cairo_set_source_rgba(cr, 1, 1, 1, alpha);
	cairo_arc(cr, cx, cy, 0.2 * s, 0, 2 * M_PI);
	cairo_fill(cr);
	cairo_set_line_width(cr, fmax(1.5, 0.06 * s));
	cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
	for (int i = 0; i < 8; i++) {
		double a = i * M_PI / 4;
		cairo_move_to(cr, cx + cos(a) * 0.3 * s, cy + sin(a) * 0.3 * s);
		cairo_line_to(cr, cx + cos(a) * 0.42 * s, cy + sin(a) * 0.42 * s);
	}
	cairo_stroke(cr);
}

static void draw_moon(cairo_t *cr, double cx, double cy, double s,
		double alpha) {
	cairo_save(cr);
	/* Everything but a bite out of the upper right */
	cairo_rectangle(cr, cx - s, cy - s, 2 * s, 2 * s);
	cairo_new_sub_path(cr);
	cairo_arc(cr, cx + 0.16 * s, cy - 0.12 * s, 0.26 * s, 0, 2 * M_PI);
	cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
	cairo_clip(cr);
	cairo_set_source_rgba(cr, 1, 1, 1, alpha);
	cairo_arc(cr, cx, cy, 0.3 * s, 0, 2 * M_PI);
	cairo_fill(cr);
	cairo_restore(cr);
}

static void draw_drops(cairo_t *cr, double cx, double cy, double s,
		int n, bool flakes, double alpha) {
	cairo_set_source_rgba(cr, 1, 1, 1, alpha);
	cairo_set_line_width(cr, fmax(1.5, 0.055 * s));
	cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
	for (int i = 0; i < n; i++) {
		double x = cx + (i - (n - 1) / 2.0) * 0.2 * s;
		double y = cy + (i % 2) * 0.06 * s;
		if (flakes) {
			cairo_arc(cr, x, y + 0.06 * s, 0.045 * s, 0, 2 * M_PI);
			cairo_fill(cr);
		} else {
			cairo_move_to(cr, x + 0.03 * s, y);
			cairo_line_to(cr, x - 0.03 * s, y + 0.13 * s);
			cairo_stroke(cr);
		}
	}
}

void c8ui_weather_glyph(cairo_t *cr, const char *name, double cx, double cy,
		double s, double alpha, uint32_t bg) {
	if (strcmp(name, "clear") == 0) {
		draw_sun(cr, cx, cy, s, alpha);
	} else if (strcmp(name, "night") == 0) {
		draw_moon(cr, cx, cy, s, alpha);
	} else if (strcmp(name, "partly") == 0 || strcmp(name, "partly-night") == 0) {
		if (name[6] == '-') {
			draw_moon(cr, cx + 0.14 * s, cy - 0.14 * s, s * 0.8, alpha);
		} else {
			draw_sun(cr, cx + 0.14 * s, cy - 0.14 * s, s * 0.8, alpha);
		}
		/* A cut round the cloud, so it reads in front of the sun */
		cairo_save(cr);
		cairo_translate(cr, cx - 0.06 * s, cy + 0.14 * s);
		cairo_set_line_width(cr, 0.08 * s);
		cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
		c8ui_set_source(cr, bg, 1);
		cloud_path(cr, s);
		cairo_stroke(cr);
		cairo_restore(cr);
		draw_cloud(cr, cx - 0.06 * s, cy + 0.14 * s, s, alpha);
	} else if (strcmp(name, "cloudy") == 0) {
		draw_cloud(cr, cx - 0.02 * s, cy + 0.02 * s, s * 1.1, alpha);
	} else if (strcmp(name, "fog") == 0) {
		cairo_set_source_rgba(cr, 1, 1, 1, alpha);
		cairo_set_line_width(cr, fmax(2, 0.07 * s));
		cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
		for (int i = 0; i < 4; i++) {
			double y = cy + (i - 1.5) * 0.2 * s;
			double inset = (i % 2) * 0.1 * s;
			cairo_move_to(cr, cx - 0.36 * s + inset, y);
			cairo_line_to(cr, cx + 0.36 * s - inset, y);
		}
		cairo_stroke(cr);
	} else if (strcmp(name, "thunder") == 0) {
		draw_cloud(cr, cx, cy - 0.12 * s, s, alpha);
		cairo_set_source_rgba(cr, 1, 1, 1, alpha);
		cairo_move_to(cr, cx + 0.06 * s, cy + 0.02 * s);
		cairo_line_to(cr, cx - 0.1 * s, cy + 0.24 * s);
		cairo_line_to(cr, cx + 0.0 * s, cy + 0.24 * s);
		cairo_line_to(cr, cx - 0.06 * s, cy + 0.42 * s);
		cairo_line_to(cr, cx + 0.14 * s, cy + 0.18 * s);
		cairo_line_to(cr, cx + 0.04 * s, cy + 0.18 * s);
		cairo_close_path(cr);
		cairo_fill(cr);
	} else {
		/* rain, sleet and snow: a cloud with what falls from it */
		draw_cloud(cr, cx, cy - 0.12 * s, s, alpha);
		bool snow = strcmp(name, "snow") == 0;
		draw_drops(cr, cx, cy + 0.2 * s, s, 3, snow, alpha * 0.9);
	}
}
