#include <ctype.h>
#include <stdio.h>
#include "com_room.h"
#include "socket.h"
#include "stringops.h"

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

static room *command_exit_roomfind (globals *g, player *p, char *l)
{
	room *r;

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

	if (!strlen (l))
		return NULL;

	if (*l == '*')
	{
		r = g->room_current;
		if (r == NULL)
		{
			sprintf (output, "w %s = No current room.",
				p->name);
			socket_write (g->socket, output);
		}
	}
	else
	{
		if (isalpha (*l))
			r = room_find (g->room_list, l);
		else
			r = room_find_num (g->room_list, atoi(l));

		if (r == NULL)
		{
			sprintf (output, "w %s = Room %s not found.",
				p->name, l);
			socket_write (g->socket, output);
		}
	}
	return r;
}

void command_exit_add (globals *g, player *p, char *l)
{
	room *r, *dest;
	link *list;

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

	l = tokenize (word, l);

	r = command_exit_roomfind (g, p, word);

	l = tokenize (word, l);

	dest = command_exit_roomfind (g, p, word);

	if ((r != NULL) && (dest != NULL))
	{
		l = tokenize (word, l);

		if (strlen (word))
		{
			link_add (r->links, dest, word);

			if (p->verbose)
			{
				sprintf (output, "w %s = Exit %s added \
					to room %s from room %s.",
					p->name, word, dest->name,
					r->name);
				socket_write (g->socket, output);
			}
		}
		else
		{
			if (p->verbose)
			{
				sprintf (output, "w %s = You have to \
					specify a name for the exit",
					p->name);
				socket_write (g->socket, output);
			}
		}
	}
}

void command_exit_delete (globals *g, player *p, char *l)
{
	room *r;
	link *list;

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

	l = tokenize (word, l);

	r = command_exit_roomfind (g, p, word);

	if (r != NULL)
	{
		l = tokenize (word, l);

		if (strlen (word))
		{
			link *li;
			if (isalpha (*word))
				li = link_find (r->links, word);
			else
				li = link_find_num (r->links, atoi(word));

			if (li != NULL)
			{
				link_delete (r->links, li);

				if (p->verbose)
				{
					sprintf (output, "w %s = Exit %s \
						deleted from room %s.",
						p->name, word, r->name);
					socket_write (g->socket, output);
				}
			}
			else
			{
				if (p->verbose)
				{
					sprintf (output, "w %s = Exit %s \
						not found in room %s.",
						p->name, word, r->name);
					socket_write (g->socket, output);
				}
			}
		}
		else
		{
			if (p->verbose)
			{
				sprintf (output, "w %s = You have to \
					specify a name for the exit",
					p->name);
				socket_write (g->socket, output);
			}
		}
	}
}

void command_exit_list (globals *g, player *p, char *l)
{
	room *r;
	link *list;

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

	l = tokenize (word, l);

	r = command_exit_roomfind (g, p, word);

	if (r != NULL)
	{
		if (r->links->head != NULL)
		{
			link *li;
			int num = 1;

			sprintf (output, "w %s = Exits for room %s:",
				p->name, r->name);
			socket_write (g->socket, output);

			for (li = r->links->head;
				li != NULL;
				li = li->next, num++)
			{
				sprintf (output, "w %s = %d %s -> %d %s",
					p->name, num, li->name,
					room_num (g->room_list, li->dest),
					(li->dest != NULL)? li->dest->name :
					"Nowhere");
				socket_write (g->socket, output);
			}
		}
		else
		{
			if (p->verbose)
			{
				sprintf (output, "w %s = No exits in \
					room %s.", p->name, r->name);
				socket_write (g->socket, output);
			}
		}
	}
}

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

	l = tokenize (word, l);

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

	if (!strcasecmp (word, "add"))		command_exit_add (g, p, l);
	if (!strcasecmp (word, "delete"))	command_exit_delete (g, p, l);
	if (!strcasecmp (word, "list"))		command_exit_list (g, p, l);
}