#ifndef _INTERPRET_CPP_
#define _INTERPRET_CPP_

#include "interpret.h"
#include "defines.h"

// Makes it easier to define constructors (since they are pretty much all the same)
// A is the name of the class, and B is the command to check for to execute action.
#define INTERPRET_CONSTRUCTOR(A, B, C) A::A() { command = B; levelrequired = C; }

//
// Base Interpret class
//

BaseInterpret::~BaseInterpret()
{
}

void BaseInterpret::action(Character & ch, string parameters)
{
}

string BaseInterpret::getCommand()
{
    return command;
}

int BaseInterpret::getLevelRequired()
{
    return levelrequired;
}

//
// InterpretHelp
//
INTERPRET_CONSTRUCTOR(InterpretHelp, "help", 0);
void InterpretHelp::action(Character & ch, string parameters)
{
    writeChar(ch, "Commands:\n\r");
    writeChar(ch, "help - This help menu\n\r");
    writeChar(ch, "password - Change your password.  Usage: password <old password> <new password>\n\r");
    writeChar(ch, "save - Save your character\n\r");
    writeChar(ch, "say - Say something, example: \'say COWMUD rocks\'.\n\r");
    writeChar(ch, "shutdown - Shut down COWMUD\n\r");
    writeChar(ch, "quit - Quit COWMUD\n\r");
}

//
// InterpretPassword
//
INTERPRET_CONSTRUCTOR(InterpretPassword, "password", 0);
void InterpretPassword::action(Character &ch, string parameters)
{
    string oldPword = argument(parameters, 1);
    string newPword = argument(parameters, 2);

    if (oldPword == ch.getPword()) {
	writeChar(ch, "Password Changed.\n\r");
	ch.setPword(newPword);
    } else {
	writeChar(ch, "Invalid Password.  Usage: password <old password> <new password>\n\r");
	ch.save();
    }
}	

//
// InterpretQuit
//
INTERPRET_CONSTRUCTOR(InterpretQuit, "quit", 0);
void InterpretQuit::action(Character & ch, string parameters)
{
    Character ch2;

    writeChar(ch, "Thank you for playing COWMUD.\n\r");
    ch.setStatus(STATUS_QUIT);

    for (int count = 1; count <= c_list.listLength(); count++) {
	c_list.listRetrieve(count, ch2);
	if (ch.getName() != ch2.getName()) {
	    writeChar(ch2, ch.getName() + " has left COWMUD.\n\r");
	}
	c_list.listReplace(count, ch2);
    }
};

//
// InterpretSave
//
INTERPRET_CONSTRUCTOR(InterpretSave, "save", 0);
void InterpretSave::action(Character & ch, string parameters)
{
    writeChar(ch, "Saving...\n\r");
    ch.save();
    writeChar(ch, "Saving complete\n\r");
}

//
// InterpretSay
//
INTERPRET_CONSTRUCTOR(InterpretSay, "say", 0);
void InterpretSay::action(Character & ch, string parameters)
{
    Character ch2;

    writeChar(ch, "You say \'" + parameters + "\'\n\r");

    string buf = ch.getName() + " says \'" + parameters + "\'\n\r";

    for (int count = 1; count <= c_list.listLength(); count++) {
	c_list.listRetrieve(count, ch2);
	if (ch.getSocket() != ch2.getSocket()) {
	    writeChar(ch2, buf);
	}
	c_list.listReplace(count, ch2);
    }
};

//
// InterpretShutdown
//
INTERPRET_CONSTRUCTOR(InterpretShutdown, "shutdown", 100);
void InterpretShutdown::action(Character & ch, string parameters)
{
    Character ch2;
    
    writeChar(ch, "COWMUD is shutting down...\n\r");
    for (int count = 1; count <= c_list.listLength(); count++) {
    	c_list.listRetrieve(count, ch2);
	writeChar(ch2, "COWMUD is shutting down...\n\r");
	c_list.listReplace(count, ch2);
    }
    running = false;
}

//
// InterpretWho
//
INTERPRET_CONSTRUCTOR(InterpretWho, "who", 0);
void InterpretWho::action(Character & ch, string parameters)
{
    Character ch2;

    writeChar(ch, "Users in COWMUD: \n\r");

    for (int count = 1; count <= c_list.listLength(); count++) {
	c_list.listRetrieve(count, ch2);
	if(ch2.getStatus() == STATUS_INGAME){
	    writeChar(ch, "-" + ch2.getName() + "\n\r");
	}
	c_list.listReplace(count, ch2);
    }
}

#endif