#include <ctype.h>
#include <stdio.h>
#include "com_globals.h"
#include "stringops.h"
#include "socket.h"
#include "room.h"

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

void command_global_load (globals *g, player *p, char *l)
{
	char *name, *file;
	int flag;

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

	copystring (name, p->name);
	copystring (file, g->globals_file);
	flag = p->verbose;

	l = tokenize (word, l);
	globals_burn (g);
	g = globals_load (strlen (word) ? word : file);

	if (flag)
	{
		sprintf (output, "w %s = Global file %s loaded.", name,
			strlen (word) ? word : file);
		socket_write (g->socket, output);
	}

	free (name);
	free (file);
}

void command_global_save (globals *g, player *p, char *l)
{
	char *globs;

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

	l = tokenize (word, l);

	if (strlen (word))
	{
		copystring (globs, g->globals_file);
		g->globals_file = word;
		globals_save (g);
		g->globals_file = globs;
	}
	else
	{
		globals_save (g);
	}

	if (p->verbose)
	{
		sprintf (output, "w %s = Globals saved to file %s.", p->name,
			strlen (word) ? word : g->globals_file);
		socket_write (g->socket, output);
	}
}

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

	l = tokenize (word, l);

	if (strlen (word))
	{
		free (g->globals_file);
		copystring (g->globals_file, word);
	}
	if (p->verbose)
	{
		sprintf (output, "w %s = Default globals file is %s.",
			p->name, g->globals_file);
		socket_write (g->socket, output);
	}
}

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

	if (l && p->owner)
	{
		name_burn (g->name);
		g->name = name_parse (l);
	}

	if (p->verbose)
	{
		name_list *scan;
		sprintf (output, "w %s = Name of robot is ", p->name);

		for (scan = g->name; scan; scan = scan->next)
		{
			strcat (output, scan->name);
			if (scan->next) strcat (output, "|");
		}
		socket_write (g->socket, output);
	}
}

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

	l = tokenize (word, l);

	if (p->owner)
	{
		if (strlen (word))
		{
			free (g->password);
			copystring (g->password, word);
		}

		if (p->verbose)
		{
			sprintf (output, "w %s = Password of robot is %s.",
				p->name, g->password);
			socket_write (g->socket, output);
		}
	}
}

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

	l = tokenize (word, l);

	if ((strlen (word)) && p->owner)
	{
		free (g->site);
		copystring (g->site, word);
	}

	if (p->verbose)
	{
		sprintf (output, "w %s = Site of robot is %s.", p->name,
			g->site);
		socket_write (g->socket, output);
	}
}

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

	l = tokenize (word, l);

	if ((strlen (word)) && p->owner)
	{
		g->port = atoi(word);
	}

	if (p->verbose)
	{
		sprintf (output, "w %s = Port of robot is %4d.", p->name,
			g->port);
		socket_write (g->socket, output);
	}
}

void command_global_command (globals *g, player *p, char *l)
{
	l = tokenize (word, l);

	if (strlen (word) > 0)
	{
		free (g->command_str);
		copystring (g->command_str, word);
	}

	if (p->verbose)
	{
		sprintf (output, "w %s = Command string set to:%s",
			p->name, g->command_str);
		socket_write (g->socket, output);
	}
}

void command_global_home (globals *g, player *p, char *l)
{
	l = tokenize (word, l);

	if (strlen (word) > 0)
	{
		free (g->home_str);
		copystring (g->home_str, word);
	}

	if (p->verbose)
	{
		sprintf (output, "w %s = Home string set to:%s",
			p->name, g->home_str);
		socket_write (g->socket, output);
	}
}

void command_global_room (globals *g, player *p, char *l)
{
	room *r;
	l = tokenize (word, l);

	if (strlen (word) > 0)
	{
		if (*word == '*')
			g->room_home = g->room_current;
		else
		{
			if (isalpha (*word))
				g->room_home = room_find (g->room_list, word);
			else
				g->room_home = room_find_num
					(g->room_list, atoi(word));
		}
	}

	if (p->verbose)
	{
		if (g->room_home != NULL)
		{
			sprintf (output, "w %s = Home room set to:%d %s",
				p->name, room_num (g->room_list, g->room_home),
				g->room_home->name);
		}
		else
		{
			sprintf (output, "w %s = Home room set to:nowhere",
				p->name);
		}
		socket_write (g->socket, output);
	}
}

void command_global_moveodds (globals *g, player *p, char *l)
{
	l = tokenize (word, l);

	if (strlen (word) > 0)
	{
		g->moveodds = atoi(word);
	}

	if (p->verbose)
	{
		sprintf (output, "w %s = Move odds set to:1 in %d",
			p->name, g->moveodds);
		socket_write (g->socket, output);
	}
}

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

	l = tokenize (word, l);

	if (strlen (word) > 0)
	{
		g->movequeuesize = atoi(word);
	}

	if (p->verbose)
	{
		sprintf (output, "w %s = Move queue size set to:%d",
			p->name, g->movequeuesize);
		socket_write (g->socket, output);
	}
}

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

	for (scan = g->rooms_visited->head; scan != NULL; scan = scan->next)
	{
		sprintf (output, "w %s = %d %s", p->name,
			room_num (g->room_list, scan->room), scan->room->name);
		socket_write (g->socket, output);
	}
}

void command_global_actodds (globals *g, player *p, char *l)
{
	l = tokenize (word, l);

	if (strlen (word) > 0)
	{
		g->actodds = atoi(word);
	}

	if (p->verbose)
	{
		sprintf (output, "w %s = Action odds set to:1 in %d",
			p->name, g->actodds);
		socket_write (g->socket, output);
	}
}

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

	l = tokenize (word, l);

	if (strlen (word) == 0)
	{
		if (p->verbose)
		{
			sprintf (output, "w %s = global commands:load, save, \
				default, name, password, site, port, command, \
				home, room, moveodds, queue, queuesize, \
				actodds",
				p->name);
			socket_write (g->socket, output);
		}
		return;
	}

	if (!strcasecmp (word, "load"))      command_global_load (g, p, l);
	if (!strcasecmp (word, "save"))      command_global_save (g, p, l);
	if (!strcasecmp (word, "default"))   command_global_default (g, p, l);
	if (!strcasecmp (word, "name"))      command_global_name (g, p, l);
	if (!strcasecmp (word, "password"))  command_global_password (g, p, l);
	if (!strcasecmp (word, "site"))      command_global_site (g, p, l);
	if (!strcasecmp (word, "port"))      command_global_port (g, p, l);
	if (!strcasecmp (word, "command"))   command_global_command (g, p, l);
	if (!strcasecmp (word, "home"))      command_global_home (g, p, l);
	if (!strcasecmp (word, "room"))      command_global_room (g, p, l);
	if (!strcasecmp (word, "moveodds"))  command_global_moveodds (g, p, l);
	if (!strcasecmp (word, "queuesize")) command_global_queuesize (g, p, l);
	if (!strcasecmp (word, "queue"))     command_global_queue (g, p, l);
	if (!strcasecmp (word, "actodds"))   command_global_actodds (g, p, l);
}