#include <ctype.h>
#include <stdio.h>
#include "socket.h"
#include "stim.h"
#include "com_stim.h"
#include "stringops.h"
#include "group.h"
#include "command.h"

static char word [128], output[1024];

void command_stim_add (globals *g, player *p, char *l)
{
	group *gr;

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

	l = tokenize (word, l);

	if (strlen (word) == 0)
	{
		if (p->verbose)
		{
			sprintf (output, "w %s = stim add {group} {stim}",
				p->name);
			socket_write (g->socket, output);
		}
		return;
	}

	gr = (*word == '*') ? (g->group_current) :
		group_find (g->group_list, word);

	if (gr)
	{
		stim_add (gr->s, l);

		if (p->verbose)
		{
			sprintf (output, "w %s = Stimulus %s added to \
				group %s.", p->name, l, gr->name); 
			socket_write (g->socket, output);
		}
	}
	else
	{
		if (p->verbose)
		{
			if (g->group_current)
				sprintf (output, "w %s = No group by the name \
					of %s.", p->name, word);
			else
				sprintf (output,
					"w %s = No groups in memory.",
					p->name);
			socket_write (g->socket, output);
		}
	}
}

void command_stim_list (globals *g, player *p, char *l)
{
	group *gr;

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

	l = tokenize (word, l);

	if (strlen (word) == 0)
	{
		if (p->verbose)
		{
			sprintf (output, "w %s = stim list {group}", p->name);
			socket_write (g->socket, output);
		}
		return;
	}

	gr = (*word == '*') ? g->group_current :
		group_find (g->group_list, word);

	if (gr != NULL)
	{
		stim *scan;
		int count = 1;

		scan = gr->s->head;

		sprintf (output, "w %s = Stimuli for group %s:", p->name,
			gr->name);
		socket_write (g->socket, output);

		while (scan != NULL)
		{
			sprintf (output, "w %s = %-4d - ", p->name, count);
			socket_write_noret (g->socket, output);
			stim_output (scan->tree, g);
			socket_write (g->socket, "");
			scan = scan->next;
			count++;
		}
	}
	else
	{
		if (p->verbose)
		{
			if (g->group_current)
				sprintf (output, "w %s = No group by the name \
					of %s.", p->name, word);
			else
				sprintf (output,
					"w %s = No groups in memory.",
					p->name);
			socket_write (g->socket, output);
		}
	}
}

void command_stim_delete (globals *g, player *p, char *l)
{
	group *gr;

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

	l = tokenize (word, l);

	if (strlen (word) == 0)
	{
		if (p->verbose)
		{
			sprintf (output,
				"w %s = stim delete {group} {stim num}",
				p->name);
			socket_write (g->socket, output);
		}
		return;
	}

	gr = (*word == '*') ? (g->group_current) :
		group_find (g->group_list, l);

	if (gr)
	{
		stim *stim_found;
		stim_found = stim_find_num (gr->s, atoi(word));

		if (stim_found != NULL)
		{
			stim_delete (gr->s, stim_found);

			if (p->verbose)
			{
				sprintf (output, "w %s = Stimulus %s deleted \
					from group %s.", p->name, l, gr->name); 
				socket_write (g->socket, output);
			}
		}
		else
		{
			if (p->verbose)
			{
				sprintf (output, "w %s = Stimulus %s doesn't \
					exist in group %s.", p->name, l,
					gr->name);
				socket_write (g->socket, output);
			}
		}
	}
	else
	{
		if (p->verbose)
		{
			if (g->group_current)
				sprintf (output, "w %s = No group by the name \
					of %s.", p->name, word);
			else
				sprintf (output,
					"w %s = No groups in memory.",
					p->name);
			socket_write (g->socket, output);
		}
	}
}

void command_stim (globals *g, player *p, char *l)
{
	#ifdef FUNCTIONS
	puts ("**command_stim");
	#endif

	l = tokenize (word, l);

	if (strlen (word) == 0)
	{
		if (p->verbose)
		{
			sprintf (output, "w %s = stim commands:add, delete, \
			list", p->name);
			socket_write (g->socket, output);
		}
		return;
	}

	if (!strcasecmp (word, "add"))		command_stim_add (g, p, l);
	if (!strcasecmp (word, "list"))		command_stim_list (g, p, l);
	if (!strcasecmp (word, "delete"))	command_stim_delete (g, p, l);
}