/*
....[@@@..[@@@..............[@.................. MUD++ is a written from
....[@..[@..[@..[@..[@..[@@@@@....[@......[@.... scratch multi-user swords and
....[@..[@..[@..[@..[@..[@..[@..[@@@@@..[@@@@@.. sorcery game written in C++.
....[@......[@..[@..[@..[@..[@....[@......[@.... This server is an ongoing
....[@......[@..[@@@@@..[@@@@@.................. development project.  All 
................................................ contributions are welcome. 
....Copyright(C).1995.Melvin.Smith.............. Enjoy. 
------------------------------------------------------------------------------
Melvin Smith (aka Fusion)         msmith@falcon.mercer.peachnet.edu 
MUD++ development mailing list    mudpp-list@spice.com
------------------------------------------------------------------------------
editroom.cc
*/

#include "string.h"
#include "llist.h"
#include "index.h"
#include "room.h"
#include "indexable.h"
#include "server.h"
#include "bit.h"
#include "edit.h"
#include "area.h"

#include "global.h"

void RoomEditor::command( String & arg )
{
	String str;
	Area *area;
	const Exit *exit;
	Exit *newExit;

	if( arg == "\n" )
	{
		// Display room and return

		str << "\n\r"
			<< "Name: " << room->getName() << "\n\r"
			<< "Area: " << room->getArea()->getName() << "\n\r"
			<< "Index: " << room->getKey() << "\n\r"
			<< "Room affects: " << "\n\r"
			<< "Room text:\n\r" << room->getDesc() << "\n\r"
			;

		for( int i = 0; i < MAX_DIR; i++ )
		{
			if( !( exit = room->getExit( i ) ) )
				continue;

			// Modify later to use -->, <-- and <--> for doors
			str	<< "Door: " << exit->getName()
				<< " --> " << exit->getIndex().asString()
				<< "  Key: " << "\n\r"; 
		}

		pc->out( str );
	} 
	else
	{
		Index index;
		String arg1;
		String arg2;
		String arg3;
		int dir;
		Room *toRoom;

		arg.startArgs();

		arg1	= arg.getArg();
		arg2	= arg.getArgRest();

		area = room->getArea();

		if( arg1 == "name" )
		{
			room->setName( arg2 );
			pc->out( "Name set.\n\r" );
			area->setDirtyBit();
			return;
		}
		else if( arg1 == "desc" )
		{
			if( arg2[0] == '+' )
			{
				arg2.shiftLeft();
				str << room->getDesc() << "\n\r" << arg2;
				room->setDesc( str );
			}
			else
			{
				pc->editText( room->getDesc() );
			}

			area->setDirtyBit();
			return;
		}
//		else if( arg1 == "index" )
//		{
//			room->setIndexKey( arg2 );
//			area->setDirtyBit();
//			return;
//		}
		else if( arg1 == "copy" )
		{
			if( !arg2 )
			{
				pc->out( "Copy which room?\n\r" );
				return;
			}

			if( !( toRoom = lookupRoom( arg2 ) ) )
			{
				pc->out( "No such room to copy.\n\r" );
				return;
			}

			room->setName( toRoom->getName() );
			room->setDesc( toRoom->getDesc() );
			area->setDirtyBit();
			return;
		}
		else if( arg1 == "save" )
		{
			if( !area->getDirtyBit() )
			{
				pc->out( "No modification since last save.\n\r" );
				return;
			}

			area->writeTo();
			area->rmDirtyBit();
			pc->out( "Zone saved.\n\r" );
			return;
		}
		if( ( dir = getDir( arg1[0] ) ) != DIR_UNDEFINED )
		{
			if( !arg2 )
			{
				if( !( toRoom = room->toRoom( dir ) ) )
				{
					pc->out( "No exit in that direction.\n\r" );
					return;
				}
	
				pc->out( "Moving to room " );
				pc->out( toRoom->getKey() );
				pc->out( ".\n\r" );

				room->rmCharInv( pc );
				toRoom->addCharInv( pc );
				room = toRoom;
				return;
			}
			else	
			{
				// Break into 3rd arg
				arg2.startArgs();
				arg3 = arg2.getArg();

				if( arg3 == "dig" )
				{
					if( room->getExit( dir ) )
					{
						pc->out( "Already an exit in that direction.\n\r" );
						return;
					} 

					if( !arg2.getArgRest() )
					{
						pc->out( "Dig to where?\n\r" );
						return;
					}

					index = arg2.getArgRest();
					if( index.getScope() )
					{
						pc->out( "Linking to external area not allowed yet.\n\r" );
						return;
					}

					index.setScope( area->getKey() );

					if( !( toRoom = area->lookupRoom( index ) ) )
					{
						toRoom = new Room( area );
						toRoom->setName( "No name" );
						toRoom->setDesc( "No description" );
						toRoom->setKey( index.getKey() );
						area->addRoom( toRoom );
						pc->out( "New room created.\n\r" );
					}

					// Convert what was 'e' to 'east' for constructor
					arg1 = getDirName( dir );
					newExit = new Exit( arg1, index, "", 0, "" );
					room->addExit( newExit, dir );
					room->hardLink();
					pc->out( "Exit linked.\n\r" );
					area->setDirtyBit();
					return;
				}
				else if( arg3 == "delete" )
				{
					if( !room->getExit( dir ) )
					{
						pc->out( "No exit in that direction.\n\r" );
						return;
					} 

					room->deleteExit( dir );
					pc->out( "Exit deleted.\n\r" );
					area->setDirtyBit();
					return;	
				}
			}
		}

		pc->out( "Invalid command.\n\r" );
	}
}