/** @license 2023 Neil Edelman, distributed under the terms of the
 [MIT License](https://opensource.org/licenses/MIT).
 @std C11 */
#include "../src/source.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>


/* `sourcelist` is an array of all the sources. */
static void sourcelist_to_string(const struct source *const s,
	char (*const z)[12]) {
	const char *a = s->name.a, *b;
	char *y = *z;
	b = s->name.b <= a + 11 ? s->name.b : a + 11;
	while(a < b) *(y++) = *(a++);
	*y = '\0';
}
#define ARRAY_NAME sourcelist
#define ARRAY_TYPE struct source
#define ARRAY_TO_STRING
#define ARRAY_BODY
#include "../src/array.h"


/* `source` is a tree mapping from date-line to indices in `sourcelist`. */
static void source_to_string(const union line64 line, const size_t *const u,
	char (*const a)[12]) { (void)u; date32_to_string(line.date, a); }
static int source_compare(const union line64 a, const union line64 b)
	{ return a.u64 > b.u64; }
#define TREE_NAME source
#define TREE_KEY union line64
#define TREE_VALUE size_t /* Index into source list. */
#define TREE_COMPARE
#define TREE_TO_STRING
#define TREE_DEFAULT 0
#define TREE_BODY
#include "../src/tree.h"


/*!conditions:re2c*/

static int scan(union date32 date, const char *const buffer,
	struct sources *const s) {
	const char *YYCURSOR, *YYMARKER, *yyt1, *yyt2, *s0, *s1;
	enum YYCONDTYPE condition = yycline;
	size_t line = 1;
	char datestr[12] = {0};
	const char *why = "unexpected";
	assert(buffer && s);
	YYCURSOR = YYMARKER = yyt1 = buffer;
	/*!re2c /**/
	re2c:define:YYCTYPE = char;
	re2c:yyfill:enable = 0;
	re2c:define:YYGETCONDITION = "condition";
	re2c:define:YYSETCONDITION = "condition = @@;";
	re2c:define:YYGETCONDITION:naked = 1;
	re2c:define:YYSETCONDITION:naked = 1;

	unix_control = [\x01-\x08\x0b-\x1f\x7f];
	ws = [ \t];
	glyph = [^] \ ("\x00" | "\n" | unix_control | ws);
	keyword = [A-Za-z0-9][A-Za-z0-9_-]*;
	*/
	for( ; ; ) {
		/*!re2c /**/
		/* Default ignore. */
		<skip> [^\n\x00] { continue; }
		<skip> "\x00" { why = "no newline at end of file"; goto catch; }
		<line> "\x00" { return 1; }
		<line, skip> "\n" => line { line++; continue; }
		<line> * :=> skip
		<line> "--" / [^-] :=> source

		<source> * { why = "default source unrecognized"; goto catch; }
		<source> @s0 keyword @s1 / "\n" => skip { also_add_to_tree: {
			const struct pair keyword = pair(s0, s1);
			const union line64 key = { { (uint32_t)line, date } };
			size_t i, *pi;
			if(line > UINT32_MAX)
				{ errno = ERANGE; why = "too many lines of text"; goto catch; }
			if(!(i = pair_map_table_get(&s->map, keyword)))
				{ why = "keyword not introduced"; goto catch; }
			switch(source_tree_try(&s->dates, key, &pi)) {
			case TREE_PRESENT: why = "duplicate key"; /* _Sic_. */
			case TREE_ERROR: goto catch;
			case TREE_ABSENT: *pi = i; break;
			}
			/*date32_to_string(date, &datestr);
			printf("%s: <%.*s>\n", datestr, (int)(s1 - s0), s0);*/
			continue;
		} }
		/* New source. This is lazy and will pickup trailing spaces. */
		<source> @s0 keyword @s1 ":" [^\x00\n]+ / "\n" => skip {
			struct pair keyword = pair(s0, s1);
			size_t *idx;
			struct source *source;
			switch(pair_map_table_assign(&s->map, keyword, &idx)) {
			case TABLE_PRESENT: errno = EDOM; why = "new keyword already used";
			case TABLE_ERROR: goto catch; /* /\ _Sic_. */
			case TABLE_ABSENT: *idx = 0; break; /* Good. */
			}
			if(!(source = sourcelist_array_new(&s->list))) goto catch;
			*idx = (size_t)(source - s->list.data);
			source->name.a = s0, source->name.b = s1;
			source->desc.a = 0, source->desc.b = 0;
			date32_to_string(date, &datestr);
			fprintf(stderr, "%s: new source <%.*s> stored in list at %zu.\n",
				datestr, (int)(s1 - s0), s0, *idx);
			goto also_add_to_tree;
		}
	*/ }
	assert(0); /* Never gets here. */
catch:
	if(!errno) errno = EILSEQ;
	date32_to_string(date, &datestr);
	fprintf(stderr, "%s line %zu: %s.\n", datestr, line, why);
	return 0;
}

void sources_(struct sources *const s) {
	if(!s) return;
	source_tree_(&s->dates);
	pair_map_table_(&s->map);
	sourcelist_array_(&s->list);
}

struct sources sources(struct journal *const j) {
	struct sources s
		= { sourcelist_array(), pair_map_table(), source_tree() };
	struct journal_iterator it;
	union date32 k;
	const char *v;
	assert(j);
	{ /* Null is the first item for convenience, (TABLE_DEFAULT). */
		struct source *nul;
		if(!(nul = sourcelist_array_new(&s.list))) goto catch;
		nul->name.a = nul->name.b = nul->desc.a = nul->desc.b = 0;
	}
	it = journal_iterator(j);
	while(journal_next(&it, &k, &v)) if(!scan(k, v, &s)) goto catch;
	fprintf(stderr, "List of sources: %s.\n"
		"Mapped to indices: %s.\n"
		"Date-line tree: %s.\n", sourcelist_array_to_string(&s.list),
		pair_map_table_to_string(&s.map), source_tree_to_string(&s.dates));
	goto finally;
catch:
	sources_(&s);
finally:
	return s;
}

int sources_is_empty(const struct sources *const s)
	{ return !s || !s->dates.root.node; }

/** Lookup the last source in `range` in sources `s`. They are invalidated on
 adding a source, (currently fine because we get all at once.) */
const struct source *source_lookup(struct sources *const s,
	const union line64 x) {
	struct source_tree_iterator it;
	assert(s);
	it = source_tree_less(&s->dates, x);
	/* If it's before all elements of the journal or is not on the same date as
	 the source, this has no source, which is `list[0]` by <fn:sources>. */
	return s->list.data + (source_tree_has_element(&it)
		&& x.date.u32 == source_tree_key(&it).date.u32
		? *source_tree_value(&it) : 0);
}
