#include <stdio.h>
#include "stringops.h"
#include "group.h"
#include "stim.h"
#include "resp.h"

static char io[1024];

void group_add (group_list *gl, char *name)
{
	group *new;

	#ifdef FUNCTIONS
	puts ("**group_add");
	#endif

	new = allocate (group);
	copystring (new->name, name);

	new->s = allocate (stim_list);
	new->s->head = NULL;
	new->s->tail = NULL;
	new->s->size = 0;

	new->r = allocate (resp_list);
	new->r->head = NULL;
	new->r->tail = NULL;
	new->r->size = new->r->priority_total = 0;

	new->next = NULL;

	if (gl->tail != NULL) gl->tail->next = new;
	gl->tail = new;
	if (gl->head == NULL) gl->head = new;
}

group *group_find (group_list *gl, char *name)
{
	group *scan;

	#ifdef FUNCTIONS
	puts ("**group_find");
	#endif

	for (scan = gl->head; (scan != NULL) && (strcasecmp (scan->name, name));
		scan = scan->next);

	return scan;
}

void group_delete (group_list *gl, group *dead)
{
	group *scan;

	#ifdef FUNCTIONS
	puts ("**group_delete");
	#endif

	if ((gl == NULL) || (gl->head == NULL))
		return;

	if (gl->head == dead)
	{
		gl->head = dead->next;
		if (gl->tail == dead) gl->tail = NULL;
		stim_burn (dead->s);
		resp_burn (dead->r);
		free (dead);

		(gl->size)--;
	}
	else
	{
		for (scan = gl->head;
			(scan->next != NULL) && (scan->next != dead);
			scan = scan->next);

		if (scan->next != NULL)
		{
			if (gl->tail == dead) gl->tail = scan;

			scan->next = scan->next->next;
			stim_burn (dead->s);
			resp_burn (dead->r);
			free (dead);

			(gl->size)--;
		}
	}
}

static void group_elements_burn (group *g)
{
	#ifdef FUNCTIONS
	puts ("**group_elements_burn");
	#endif

	if (g != NULL)
	{
		group_elements_burn (g->next);
		stim_burn (g->s);
		resp_burn (g->r);
		free (g);
	}
}

void group_burn (group_list *gl)
{
	#ifdef FUNCTIONS
	puts ("**group_burn");
	#endif

	if (gl)
	{
		group_elements_burn (gl->head);
		free (gl);
	}
}

group_list *group_load (char *filename)
{
	group_list *gl;
	FILE *f;

	#ifdef FUNCTIONS
	puts ("**group_load");
	#endif

	gl = allocate (group_list);
	gl->head = NULL;
	gl->tail = NULL;
	gl->size = NULL;

	f = fopen (filename, "r");

	if (!f) return gl;

	fgets (io, 200, f);
	clipret (io);

	do
	{
		if (!feof(f))
		{
			group_add (gl, io);

			fgets (io, 200, f);
			clipret (io);

			while ((*io == '<') & !feof (f))
			{
				if ((*io == '<') && !feof (f))
				{
					stim_add (gl->tail->s, io + 1);
				}
				fgets (io, 200, f);
				clipret (io);
			}

			while ((*io == '>') && !feof (f))
			{
				if ((*io == '>') && !feof (f))
				{
					if (!resp_add (gl->tail->r, io + 1))
						printf ("Response invalid:%s",
							io + 1);
				}

				fgets (io, 200, f);
				clipret (io);
			}
		}
	} 
	while (!feof (f));

	fclose (f);

	return gl;
}

void group_save (group_list *gl, char *filename)
{
	FILE *f;
	group *scan;
	stim *st;
	resp *re;

	#ifdef FUNCTIONS
	puts ("**group_save");
	#endif

	f = fopen (filename, "w");

	for (scan = gl->head; scan != NULL; scan = scan->next)
	{
		fputs (scan->name, f);
		putc ('\n', f);

		for (st = scan->s->head; st != NULL; st = st->next)
		{
			putc ('<', f);
			stim_file_output (st->tree, f);
			putc ('\n', f);
		}

		for (re = scan->r->head; re != NULL; re = re->next)
		{
			putc ('>', f);
			resp_file_output (re, f);
			putc ('\n', f);
		}
	}

	fclose (f);
}