#include "engine/declarations.h"
#include "engine/cmdline_def.h"
#include "engine/cmdline_str.h"

#include "declarations_app.h"
#include "cmdline_app.h"



static void cmd_app_galaxy_radius(char *s) {
	(void) conf_update_float_ext("galaxy_radius", s);
}

static void cmd_app_galaxy_radius_def(void) {
	(void) fprintf(stdout, "20.0");
}

static void cmd_app_galaxy_arms(char *s) {
	(void) conf_update_integer_ext("galaxy_arms", s);
}

static void cmd_app_galaxy_arms_def(void) {
	(void) fprintf(stdout, "2");
}

static void cmd_app_galaxy_arm_length(char *s) {
	(void) conf_update_float_ext("galaxy_arm_length", s);
}

static void cmd_app_galaxy_arm_length_def(void) {
	(void) fprintf(stdout, "3.0");
}

static void cmd_app_galaxy_steps(char *s) {
	(void) conf_update_float_ext("galaxy_steps", s);
}

static void cmd_app_galaxy_steps_def(void) {
	(void) fprintf(stdout, "0.001");
}

static void cmd_app_galaxy_sigma(char *s) {
	(void) conf_update_float_ext("galaxy_sigma", s);
}

static void cmd_app_galaxy_sigma_def(void) {
	(void) fprintf(stdout, "0.25");
}

static void cmd_app_galaxy_dispersion(char *s) {
	(void) conf_update_float_ext("galaxy_dispersion", s);
}

static void cmd_app_galaxy_dispersion_def(void) {
	(void) fprintf(stdout, "8.0");
}

/* This parses and returns application defined command line switches defined
   in cmdline_app.h. Engine calls this, please do not touch below or remove
   any of the include lines above. */

void cmd_ext_help(__UNUSED__ char *s, int k, int c) {
	unsigned int i;

	void (*def)(void);

	if(cmdline_str_cnf_t[0].o_s == NULL) {
		return;
	}

	if(k != 0) {
		if(c == IS_YES) {
			(void) fprintf(stdout, "\x1b[1m");

			(void) fprintf(stdout, _("OPTIONS FOR %s"),
				APPLICATION_NAME);

			(void) fprintf(stdout, "\x1b[22m%c",
				CONFIG_LINE_FEED);
		}
		else {
			(void) fprintf(stdout, _("OPTIONS FOR %s%c"),
				APPLICATION_NAME, CONFIG_LINE_FEED);
		}
	}

	for(i = 0; ; i++) {
		if(cmdline_str_cnf_t[i].o_s == NULL) {
			break;
		}

		if(cmdline_str_cnf_t[i].o_s[0] == 0) {
			if(c == IS_YES) {
				(void) fprintf(stdout,
					"%c\x1b[1m%s:\x1b[22m%c",
						CONFIG_LINE_FEED,
						cmdline_str_cnf_t[i].o_d,
						CONFIG_LINE_FEED);
			}
			else {
				(void) fprintf(stdout, "%c%s:%c",
					CONFIG_LINE_FEED,
					cmdline_str_cnf_t[i].o_d,
					CONFIG_LINE_FEED);
			}

			continue;
		}

		if(c == IS_YES) {
			(void) fprintf(stdout,
				" \x1b[1m%s\x1b[22m, \x1b[1m%s\x1b[22m",
					cmdline_str_cnf_t[i].o_s,
					cmdline_str_cnf_t[i].o_l);
		}
		else {
			(void) fprintf(stdout, " %s, %s",
				cmdline_str_cnf_t[i].o_s,
				cmdline_str_cnf_t[i].o_l);
		}

		if(cmdline_str_cnf_t[i].o_r == CONFIG_OPTION_REQUIRED) {
			if(c == IS_YES) {
				(void) fprintf(stdout, " <\x1b[4m");
			}
			else {
				(void) fprintf(stdout, " <");
			}

			(void) fprintf(stdout, _("parameter, "));

			switch(conf_get_type_app(cmdline_str_cnf_t[i].o_l)) {
				case CONFIG_TYPE_BOOLEAN:
					(void) fprintf(stdout, _("boolean"));

					break;
				case CONFIG_TYPE_INTEGER:
					(void) fprintf(stdout, _("integer"));

					break;
				case CONFIG_TYPE_FLOAT:
					(void) fprintf(stdout, _("float"));

					break;
				case CONFIG_TYPE_STRING:
					(void) fprintf(stdout, _("string"));

					break;
				case CONFIG_TYPE_COLOR:
					(void) fprintf(stdout, _("color"));

					break;
				default:
					break;
			}

			if(c == IS_YES) {
				(void) fprintf(stdout, "\x1b[24m>");
			}
			else {
				(void) fprintf(stdout, ">");
			}
		}

		def = *cmdline_str_cnf_t[i].dp;

		if(def != NULL) {
			(void) fprintf(stdout, _("%c        (default: "),
				CONFIG_LINE_FEED);

			(void) (*def)();

			(void) fprintf(stdout, ")");
		}

		(void) fprintf(stdout, "%c        %s%c",
			CONFIG_LINE_FEED,
			cmdline_str_cnf_t[i].o_d,
			CONFIG_LINE_FEED);
	}

	(void) fprintf(stdout, "%c", CONFIG_LINE_FEED);
}

struct cmdline_str_cnf *cmd_ext_get_cmd(void) {
	return(cmdline_str_cnf_t);
}

int cmd_ext_get_cmd_c(void) {
	if(cmdline_str_cnf_t[0].o_s == NULL) {
		return(0);
	}

	return(1);
}
