#include "engine/declarations.h"

#include "declarations_app.h"
#include "universe.h"



int universe_init(void) {
	(void) universe_set_message("");

	if((sp = malloc(sizeof(struct universe))) == NULL) {
		LOGWARN(ERROR_SLIGHT, SUBSYSTEM,
			_("Failed to allocate %zu bytes of memory"),
				sizeof(struct universe));

		return(-1);
	}

	if(universe_create_random() != 0) {
		(void) universe_free();

		return(-1);
	}

	return(0);
}

void universe_free(void) {
	unsigned long i;

	if(sp != NULL) {
		for(i = 0; i < sp->galaxies; i++) {
			(void) galaxy_free(sp->ga[i]);
		}

		(void) free(sp);

		sp = NULL;
	}
}

void universe_run(void) {
	unsigned long a, b, c, d;

	struct galaxy *ga;
	struct star *sa;
	struct planet *pa, *ca;

	D_TIME_TM tv;

	(void) timer_get_date_by_time(&tv, (D_TIME) round(clock_get_ticks()));
#if defined(__sun__)
	(void) snprintf(dt, sizeof(dt),
		"%d.%d.%d %.2d:%.2d",
			tv.tm_mday, tv.tm_mon + 1, tv.tm_year + 1900,
			tv.tm_hour, tv.tm_min);
#else
	(void) snprintf(dt, sizeof(dt),
		"%d.%d.%d %s %.2d:%.2d",
			tv.tm_mday, tv.tm_mon + 1, tv.tm_year + 1900, tv.tm_zone,
			tv.tm_hour, tv.tm_min);
#endif
	for(a = 0; a < sp->galaxies; a++) {
		ga = sp->ga[a];

		(void) galaxy_run(ga);

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

			(void) star_run(sa);

			for(c = 0; c < sa->planets; c++) {
				pa = sa->pa[c];

				(void) planet_run(pa);

				for(d = 0; d < pa->satellites; d++) {
					ca = pa->ca[d];

					(void) planet_run(ca);
				}
			}
		}
	}
}

void universe_set_message(char *s) {
	(void) snprintf(ms, sizeof(ms), "%s", s);

	++mc;
}

unsigned int universe_chk_message(void) {
	return(mc);
}

char *universe_get_message(void) {
	return(ms);
}

char *universe_get_date(void) {
	return(dt);
}

struct galaxy *universe_get_galaxy_by_id(unsigned long a) {
	if(a > sp->galaxies) {
		return(NULL);
	}

	return(sp->ga[a]);
}

struct star *universe_get_star_by_id(unsigned long a, unsigned long b) {
	if(a > sp->galaxies || b > sp->ga[a]->stars) {
		return(NULL);
	}

	return(sp->ga[a]->sa[b]);
}

struct planet *universe_get_planet_by_id(unsigned long a, unsigned long b, unsigned long c) {
	if(a > sp->galaxies || b > sp->ga[a]->stars ||
		c > sp->ga[a]->sa[b]->planets || sp->ga[a]->sa[b]->planets == 0) {

		return(NULL);
	}

	return(sp->ga[a]->sa[b]->pa[c]);
}

struct planet *universe_get_satellite_by_id(unsigned long a, unsigned long b, unsigned long c, unsigned long d) {
	if(a > sp->galaxies || b > sp->ga[a]->stars || c > sp->ga[a]->sa[b]->planets ||
		d > sp->ga[a]->sa[b]->pa[c]->satellites || sp->ga[a]->sa[b]->pa[c]->satellites == 0) {

		return(NULL);
	}

	return(sp->ga[a]->sa[b]->pa[c]->ca[d]);
}

static int universe_create_random(void) {
	unsigned long a;

	sp->id = 0;
	sp->galaxies = UNIVERSE_GALAXIES_PER_SPACE;

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

		return(-1);
	}

	/* Populate universe */
	for(a = 0; a < sp->galaxies; a++) {
		if(galaxy_create(sp, a) != 0) {
			return(-1);
		}

		if(universe_create_random_stars(sp->ga[a]) != 0) {
			return(-1);
		}
	}

	return(0);
}

static int universe_create_random_stars(struct galaxy *ga) {
	unsigned long b;

	if(ga->stars > UNIVERSE_STARS_PER_GALAXY_MAX) {
		(void) flush_error();

		LOGWARN(ERROR_NOERROR, SUBSYSTEM,
			_("Star count %lu exceeds star limit of %u, limiting star count to %u"),
				ga->stars,
				(unsigned int) UNIVERSE_STARS_PER_GALAXY_MAX,
				(unsigned int) UNIVERSE_STARS_PER_GALAXY_MAX);

		ga->stars = UNIVERSE_STARS_PER_GALAXY_MAX;
	}

	if((ga->sa = malloc(sizeof(struct star *) * ga->stars)) == NULL) {
		LOGWARN(ERROR_SLIGHT, SUBSYSTEM,
			_("Failed to allocate %zu bytes of memory"),
				sizeof(struct star *) * ga->stars);

		return(-1);
	}

	LOGINFO(ERROR_NOERROR, SUBSYSTEM,
		_("Generating %lu solar systems, this may take a while"),
			ga->stars);

	/* First star is always a black hole at the center of the galaxy */
	if(star_create_blackhole(ga, 0) != 0) {
		return(-1);
	}

	(void) star_create_spot_blackhole(ga, 0);

	for(b = 1; b < ga->stars; b++) {
		switch(maths_rand(1000)) {
			case 1:
				if(star_create_blackhole(ga, b) != 0) {
					return(-1);
				}

				break;
			case 2:
			case 3:
			case 4:
				if(star_create_neutron(ga, b) != 0) {
					return(-1);
				}

				break;
			default:
				if(star_create(ga, b) != 0) {
					return(-1);
				}

				if(universe_create_random_planets(ga, b) != 0) {
					return(-1);
				}

				break;
		}

		(void) star_create_spot(ga, b);
	}

	return(0);
}

static int universe_create_random_planets(struct galaxy *ga, unsigned long b) {
	unsigned long c;

	/* Create planets for parent star only if star is lucky enough (50% out of 100) */
	if(maths_rand(100) > 50) {
		return(0);
	}

	ga->sa[b]->planets = UNIVERSE_PLANETS_PER_STAR_MAX - UNIVERSE_PLANETS_PER_STAR_MIN;
	ga->sa[b]->planets = (unsigned long) maths_rand((unsigned int) ga->sa[b]->planets) +
		UNIVERSE_PLANETS_PER_STAR_MIN;

	ga->sa[b]->created = ga->sa[b]->planets;

	if((ga->sa[b]->pa = malloc(sizeof(struct planet *) * ga->sa[b]->planets)) == NULL) {
		LOGWARN(ERROR_SLIGHT, SUBSYSTEM,
			_("Failed to allocate %zu bytes of memory"),
				sizeof(struct planet *) * ga->sa[b]->planets);

		return(-1);
	}

	/* Parent star is a center of solar system now because it has planets */
	if(star_create_solar(ga, b) != 0) {
		return(-1);
	}

	for(c = 0; c < ga->sa[b]->planets; c++) {
		if(planet_create(ga->sa[b], c) != 0) {
			return(-1);
		}

		if(universe_create_random_satellites(ga->sa[b], c) != 0) {
			return(-1);
		}
	}

	return(0);
}

static int universe_create_random_satellites(struct star *sa, unsigned long c) {
	unsigned long d;

	/* Create satellites for parent planet if planet is lucky enough (75% out of 100) */
	if(maths_rand(100) > 75) {
		return(0);
	}

	sa->pa[c]->satellites = UNIVERSE_COMPS_PER_PLANET_MAX - UNIVERSE_COMPS_PER_PLANET_MIN;
	sa->pa[c]->satellites = (unsigned long) maths_rand((unsigned int) sa->pa[c]->satellites) +
		UNIVERSE_COMPS_PER_PLANET_MIN;

	if((sa->pa[c]->ca = malloc(sizeof(struct planet *) * sa->pa[c]->satellites)) == NULL) {
		LOGWARN(ERROR_SLIGHT, SUBSYSTEM,
			_("Failed to allocate %zu bytes of memory"),
				sizeof(struct planet *) * sa->pa[c]->satellites);

		return(-1);
	}

	for(d = 0; d < sa->pa[c]->satellites; d++) {
		if(planet_satellite_create(sa, c, d) != 0) {
			return(-1);
		}
	}

	return(0);
}

static void universe_print_human(void) {
	(void) fprintf(stdout, _("$ Universe %lu, with %lu galaxies%c"), sp->id, sp->galaxies, CONFIG_LINE_FEED);
	(void) fprintf(stdout, "%c", CONFIG_LINE_FEED);
}

static void universe_header_print_human(void) {
	(void) fprintf(stdout, _("# This is %s version %s human-readable catalog%c"), APPLICATION_NAME, APPLICATION_VERSION, CONFIG_LINE_FEED);
	(void) fprintf(stdout, "%c", CONFIG_LINE_FEED);
}
