#include "engine/declarations.h"

#include "declarations_app.h"
#include "galaxy.h"



int galaxy_create(struct universe *sp, unsigned long a) {
	struct galaxy *g;

	if((sp->ga[a] = malloc(sizeof(struct galaxy))) == NULL) {
		LOGWARN(ERROR_SLIGHT, SUBSYSTEM,
			_("Failed to allocate %zu bytes of memory"),
				sizeof(struct galaxy));

		return(-1);
	}

	g = sp->ga[a];

	g->id = a;
	g->pd = sp->id;

	g->solars = 0;

	(void) galaxy_create_spec(g);
	(void) galaxy_create_spot(g);
	(void) galaxy_create_draw(g);

	if(galaxy_create_name(g) != 0) {
		return(-1);
	}

	if(galaxy_create_nick(g) != 0) {
		return(-1);
	}

	return(0);
}

static void galaxy_create_spec(struct galaxy *ga) {
	unsigned int *a;

	double *b;

	ga->gs.arms = 2;

	ga->gs.radius = 20.0;
	ga->gs.length = 3.0;
	ga->gs.steps = 0.001;
	ga->gs.sigma = 0.25;
	ga->gs.dispersion = 8.0;

	if((b = (double *) conf_fetch("galaxy_radius")) != NULL) {
		if(!is_eq_double(*b, 0.0)) {
			ga->gs.radius = *b;
		}
	}

	if((a = (unsigned int *) conf_fetch("galaxy_arms")) != NULL) {
		if(*a != 0) {
			ga->gs.arms = *a;
		}
	}

	if((b = (double *) conf_fetch("galaxy_arm_length")) != NULL) {
		if(!is_eq_double(*b, 0.0)) {
			ga->gs.length = *b;
		}
	}

	if((b = (double *) conf_fetch("galaxy_steps")) != NULL) {
		if(!is_eq_double(*b, 0.0)) {
			ga->gs.steps = *b;
		}
	}

	if((b = (double *) conf_fetch("galaxy_sigma")) != NULL) {
		if(!is_eq_double(*b, 0.0)) {
			ga->gs.sigma = *b;
		}
	}

	if((b = (double *) conf_fetch("galaxy_dispersion")) != NULL) {
		if(!is_eq_double(*b, 0.0)) {
			ga->gs.dispersion = *b;
		}
	}

	ga->stars = (unsigned long) round((ga->gs.length * CONST_PI) / ga->gs.steps);

	(void) galaxy_create_spec_class(ga);
}

static void galaxy_create_spec_class(struct galaxy *ga) {
	char *s;

	if(ga->gs.arms > 2) {
		s = GALAXY_CLASS_SC;
	}
	else if(ga->gs.length > ga->gs.radius / 4) {
		s = GALAXY_CLASS_SB;
	}
	else {
		s = GALAXY_CLASS_SA;
	}

	(void) memcpy((void *) ga->class, (const void *) s, sizeof(ga->class));
}

static void galaxy_create_spot(struct galaxy *ga) {
	ga->gp.x = 0.0;
	ga->gp.y = 0.0;
	ga->gp.z = 0.0;

	ga->gd_a.x = 0.0;
	ga->gd_a.y = 0.0;
	ga->gd_a.z = 0.0;

	ga->gd_b.x = 0.0;
	ga->gd_b.y = 0.0;
	ga->gd_b.z = 0.0;
}

static void galaxy_create_draw(struct galaxy *ga) {
	ga->sd.sp_p = NULL;

	(void) memset((void *) &ga->sd.sp_f, 0, sizeof(ga->sd.sp_f));
	(void) memset((void *) &ga->sd.sp_s, 0, sizeof(ga->sd.sp_s));
}

static int galaxy_create_name(struct galaxy *ga) {
	char n[GALAXY_NAME_LEN_MAX];

	/* Generate galaxy name (GALAXY_ID) */
	(void) snprintf(n, sizeof(n), "%s-%lu", galaxy_name_p, ga->id);

	ga->name_t = strnlen(n, sizeof(n) - sizeof(char));

	if((ga->name = malloc((size_t) (ga->name_t + (sizeof(char) * 4)))) == NULL) {
		LOGWARN(ERROR_SLIGHT, SUBSYSTEM,
			_("Failed to allocate %zu bytes of memory"),
				(size_t) ga->name_t + sizeof(char));

		return(-1);
	}

	(void) memcpy((void *) ga->name, (const void *) n, ga->name_t);

	ga->name[ga->name_t] = 0;
	ga->name[ga->name_t + 1] = 0;
	ga->name[ga->name_t + 2] = 0;
	ga->name[ga->name_t + 3] = 0;

	return(0);
}

static int galaxy_create_nick(struct galaxy *ga) {
	char *a, *b, *c;

	char n[GALAXY_NAME_LEN_MAX];

	a = galaxy_name_a[maths_rand(GALAXY_NAME_A)];
	b = galaxy_name_b[maths_rand(GALAXY_NAME_B)];
	c = galaxy_name_c[maths_rand(GALAXY_NAME_C)];

	if(a[0] == ' ') {
		(void) snprintf(n, sizeof(n), "%s %s", b, c);

		n[0] = (char) toupper((int) n[0]);
	}
	else {
		(void) snprintf(n, sizeof(n), "%s %s %s", a, b, c);

		n[0] = (char) toupper((int) n[0]);

		n[str_len(a, STRING_ASCII) + sizeof(char)] = (char) toupper((int) n[str_len(a,
			STRING_ASCII) + sizeof(char)]);
	}

	ga->nick_t = strnlen(n, sizeof(n) - sizeof(char));

	if((ga->nick = malloc((size_t) ga->nick_t + (sizeof(char) * 4))) == NULL) {
		LOGWARN(ERROR_SLIGHT, SUBSYSTEM,
			_("Failed to allocate %zu bytes of memory"),
				(size_t) ga->nick_t + sizeof(char));

		return(-1);
	}

	(void) memcpy((void *) ga->nick, (const void *) n, ga->nick_t);

	ga->nick[ga->nick_t] = 0;
	ga->nick[ga->nick_t + 1] = 0;
	ga->nick[ga->nick_t + 2] = 0;
	ga->nick[ga->nick_t + 3] = 0;

	return(0);
}

void galaxy_free(struct galaxy *ga) {
	unsigned long i;

	if(ga != NULL) {
		(void) galaxy_live_free(ga);

		if(ga->name != NULL) {
			(void) free(ga->name);
		}

		if(ga->nick != NULL) {
			(void) free(ga->nick);
		}

		for(i = 0; i < ga->stars; i++) {
			(void) star_free(ga->sa[i]);
		}

		(void) free(ga);
	}
}

void galaxy_run(struct galaxy *ga) {
	unsigned long i, j;

	double a, b, c;

	struct star *sa, *sb;

	struct position e, f;

	/* Star velocity deacceleration factor */
	c = 0.000000001;

	for(i = 0; i < ga->stars; i++) {
		sa = ga->sa[i];

		f.x = 0.0;
		f.y = 0.0;
		f.z = 0.0;

		for(j = 0; j < ga->stars; j++) {
			if(i == j) {
				continue;
			}

			sb = ga->sa[j];

			e.x = sb->sp.x - sa->sp.x;
			e.y = sb->sp.y - sa->sp.y;
			e.z = sb->sp.z - sa->sp.z;

			b = coords_mag_3d(&e);
			a = (CONST_GRAVITY * ((sa->ss.mass * sb->ss.mass) / (b * b))) / sa->ss.mass;
			a *= (c * clock_get_multiplier());

			f.x += (e.x * a);
			f.y += (e.y * a);
			f.z += (e.z * a);
		}

		sa->sv.x = f.x;
		sa->sv.y = f.y;
		sa->sv.z = f.z;

		sa->sp.x += sa->sv.x;
		sa->sp.y += sa->sv.y;
		sa->sp.z += sa->sv.z;
	}
}

void galaxy_live(unsigned long a, double u, double v, double s, unsigned int h) {
	unsigned int x, y, w;
	unsigned long i, j;

	double d, e, z;

	struct galaxy *ga;
	struct star *sa;

	if((ga = universe_get_galaxy_by_id(a)) == NULL) {
		return;
	}

	if(ga->stars == 0) {
		return;
	}

	if(galaxy_live_op(ga) != 0) {
		return;
	}

	x = window_get_active_width(h);
	y = window_get_active_height(h);

	w = VALUE_MIN2(x, y) - 1;

	x /= 2;
	y /= 2;

	/* Get galaxy dimensions for scaling it down */
	d = ga->gd_b.x - ga->gd_a.x;

	if(ga->gd_b.y - ga->gd_a.y > d) {
		d = ga->gd_b.y - ga->gd_a.y;
	}

	/* Set zoom factor */
	s = maths_clamp(s, 0.0, GALAXY_LIVE_ZOOM_STEPS - 0.1);
	z = d / GALAXY_LIVE_ZOOM_STEPS;

	for(i = 0, j = 0; i < ga->stars; i++) {
		sa = ga->sa[i];

		/* Apply zooming */
		e = d - (z * s);

		ga->sd.sp_p[j].x = maths_map(sa->sp.x, 0.0, e, 0.0, (double) w) + x + u;
		ga->sd.sp_p[j].y = maths_map(sa->sp.y, 0.0, e, 0.0, (double) w) + y + v;

		ga->sd.sp_p[j].p.pixel.p = sa->ss.rgb.pixel.p;

		++j;
	}

	ga->sd.sp_s.c = (unsigned int) j;
	ga->sd.sp_s.p = ga->sd.sp_p;

	(void) galaxy_live_title(ga, h);

	(void) gui_draw_subpixels_alpha(h, &ga->sd.sp_s);
}

static int galaxy_live_op(struct galaxy *ga) {
	size_t t;

	if(ga->sd.sp_p == NULL) {
		t = sizeof(struct subpoint_2) * ga->stars;

		if((ga->sd.sp_p = malloc(t)) == NULL) {
			LOGWARN(ERROR_SLIGHT, SUBSYSTEM,
				_("Failed to allocate %zu bytes of memory"),
					t);

			return(-1);
		}

		(void) memset((void *) ga->sd.sp_p, 0, t);
	}

	return(0);
}

static void galaxy_live_title(struct galaxy *ga, unsigned int h) {
	char n[CONFIG_LINE_LENGTH];

	struct pixel_rgb_8 p;

	if(ga->sd.sp_f[0] == 0) {
		if((ga->sd.sp_f[0] = fonts_open("share/detroit/fonts/NotoSans-Regular.ttf", 18, 0)) == 0) {
			return;
		}
	}

	if(ga->nick != NULL) {
		(void) snprintf(n, sizeof(n), "%s, class %s%c%c%c%c",
			ga->nick, ga->class, 0, 0, 0, 0);
	}
	else {
		(void) snprintf(n, sizeof(n), "%s, class %s%c%c%c%c",
			ga->name, ga->class, 0, 0, 0, 0);
	}

	p.r = 255;
	p.g = 215;
	p.b = 105;

	(void) fonts_render(ga->sd.sp_f[0], "UTF-8", n, str_len(n, STRING_ASCII), &p, 0);

	ga->sd.sp_t[0].x = 10;
	ga->sd.sp_t[0].y = 10;

	ga->sd.sp_t[0].f = ga->sd.sp_f[0];

	(void) gui_draw_text(h, &ga->sd.sp_t[0]);
}

void galaxy_live_free(struct galaxy *ga) {
	unsigned long i;

	for(i = 0; i < sizeof(ga->sd.sp_f) / sizeof(ga->sd.sp_f[0]); i++) {
		(void) fonts_close(ga->sd.sp_f[i]);
	}

	if(ga->sd.sp_p != NULL) {
		(void) free(ga->sd.sp_p);

		ga->sd.sp_p = NULL;
	}
}

unsigned long galaxy_live_closest(unsigned long a, double u, double v) {
	unsigned long i, r;

	double e, f;

	struct galaxy *ga;

	struct position p, q;

	if((ga = universe_get_galaxy_by_id(a)) == NULL) {
		return(0);
	}

	r = 0;
	f = ga->gd_b.x * 2.0;

	q.x = u;
	q.y = v;

	for(i = 0; i < (unsigned long) ga->sd.sp_s.c; i++) {
		p.x = ga->sd.sp_p[i].x;
		p.y = ga->sd.sp_p[i].y;

		e = coords_difference_2d(&p, &q);

		if(e < f) {
			f = e;
			r = i + 1;
		}
	}

	return(r);
}

char *galaxy_get_name(unsigned long a) {
	struct galaxy *ga;

	if((ga = universe_get_galaxy_by_id(a)) == NULL) {
		return(NULL);
	}

	return(ga->name);
}

char *galaxy_get_nick(unsigned long a) {
	struct galaxy *ga;

	if((ga = universe_get_galaxy_by_id(a)) == NULL) {
		return(NULL);
	}

	return(ga->nick);
}

int galaxy_print_catalog(struct galaxy *ga) {
	int r;

	char n;

	r = catalog_entry_open();

	r += catalog_entry_item_save((void *) &ga->id, sizeof(ga->id));
	r += catalog_entry_item_save((void *) &ga->pd, sizeof(ga->pd));

	r += catalog_entry_item_save((void *) &ga->stars, sizeof(ga->stars));
	r += catalog_entry_item_save((void *) &ga->solars, sizeof(ga->solars));

	r += catalog_entry_item_save((void *) &ga->class, sizeof(ga->class));

	/* Galaxy position and dimensions */
	r += catalog_entry_item_save((void *) &ga->gp.x, sizeof(ga->gp.x));
	r += catalog_entry_item_save((void *) &ga->gp.y, sizeof(ga->gp.y));
	r += catalog_entry_item_save((void *) &ga->gp.z, sizeof(ga->gp.z));

	r += catalog_entry_item_save((void *) &ga->gd_a.x, sizeof(ga->gd_a.x));
	r += catalog_entry_item_save((void *) &ga->gd_a.y, sizeof(ga->gd_a.y));
	r += catalog_entry_item_save((void *) &ga->gd_a.z, sizeof(ga->gd_a.z));

	r += catalog_entry_item_save((void *) &ga->gd_b.x, sizeof(ga->gd_b.x));
	r += catalog_entry_item_save((void *) &ga->gd_b.y, sizeof(ga->gd_b.y));
	r += catalog_entry_item_save((void *) &ga->gd_b.z, sizeof(ga->gd_b.z));

	/* Galaxy specs */
	r += catalog_entry_item_save((void *) &ga->gs.radius, sizeof(ga->gs.radius));
	r += catalog_entry_item_save((void *) &ga->gs.length, sizeof(ga->gs.length));
	r += catalog_entry_item_save((void *) &ga->gs.steps, sizeof(ga->gs.steps));
	r += catalog_entry_item_save((void *) &ga->gs.sigma, sizeof(ga->gs.sigma));
	r += catalog_entry_item_save((void *) &ga->gs.dispersion, sizeof(ga->gs.dispersion));

	r += catalog_entry_item_save((void *) &ga->gs.arms, sizeof(ga->gs.arms));

	/* Galaxy name */
	n = 0;

	if(ga->name != NULL) {
		r += catalog_entry_item_save((void *) ga->name, ga->name_t + sizeof(char));
	}
	else {
		r += catalog_entry_item_save((void *) &n, sizeof(n));
	}

	if(ga->nick != NULL) {
		r += catalog_entry_item_save((void *) ga->nick, ga->nick_t + sizeof(char));
	}
	else {
		r += catalog_entry_item_save((void *) &n, sizeof(n));
	}

	return(r);
}

void galaxy_print(unsigned long a) {
	struct galaxy *ga;

	if((ga = universe_get_galaxy_by_id(a)) == NULL) {
		return;
	}

	(void) galaxy_print_human(ga);
}

void galaxy_print_human(struct galaxy *ga) {
	if(ga->nick != NULL) {
		(void) fprintf(stdout, "@ %s %lu, ", ga->nick, ga->id);
	}
	else {
		(void) fprintf(stdout, "@ %s %lu, ", ga->name, ga->id);
	}

	(void) fprintf(stdout, _("class %s, "), ga->class);
	(void) fprintf(stdout, _("with %lu stars and %lu solar systems%c"), ga->stars, ga->solars, CONFIG_LINE_FEED);
	(void) fprintf(stdout, "%c", CONFIG_LINE_FEED);
}
