/*
 * PVVMUD a 3D MUD
 * Copyright (C) 1998-1999  Programvareverkstedet (pvv@pvv.org)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iostream.h>
#include "client.H"
#include "inputfunctionmap.H"

CInputFunctionMapItem::CInputFunctionMapItem(CInputFunctionMap * inputFunctionMap, char * functionIdString, InputFunction_t inputFunction){
  this->inputIdStr = NULL;
  this->functionIdStr = functionIdString;
  this->inputFunction = inputFunction;
  argument = -1;
  m_inputFunctionMap = inputFunctionMap;
}

CInputFunctionMapItem::CInputFunctionMapItem(CInputFunctionMap * inputFunctionMap, char * inputIdString, char * functionIdString, InputFunction_t inputFunction,int argument){
  this->inputIdStr = inputIdString;
  this->functionIdStr = functionIdString;
  this->inputFunction = inputFunction;
  this->argument = argument;
  m_inputFunctionMap = inputFunctionMap;
}

void CInputFunctionMapItem::setInputIdStr(char * inputIdString){
  this->inputIdStr = (char*)malloc(strlen(inputIdString)+1);
  strcpy(this->inputIdStr,inputIdString);
}

int CInputFunctionMapItem::executeFunction(int argument){
  return (m_inputFunctionMap->*inputFunction)(argument); 
}

int CInputFunctionMapItem::executeFunction(){
  return executeFunction(argument); 
}


CInputFunctionMap::CInputFunctionMap(){
  inputFunctionMap = new CObjectList();
}

void CInputFunctionMap::addInputFunction(char * functionIdString, InputFunction_t inputFunction){
  inputFunctionMap->addLast(new CInputFunctionMapItem(this,functionIdString,inputFunction));
}

void CInputFunctionMap::addInputFunction(char * inputIdString, char * functionIdString, InputFunction_t inputFunction, int argument){
  inputFunctionMap->addLast(new CInputFunctionMapItem(this,inputIdString,functionIdString,inputFunction,argument));
}

void CInputFunctionMap::loadMap(char * fileName){
  char inputIdString[256];
  char functionIdString[256];
  FILE * mapFile = fopen(fileName,"r");
  if (mapFile == NULL){
    cdebug << "Failed to open input function map file " << fileName << "\n";
    return;
  }
  while (fscanf(mapFile,"%s %s\n",functionIdString,inputIdString)==2){
    setFunctionInputId(functionIdString,inputIdString);  
  }
  fclose(mapFile);
}

void CInputFunctionMap::setFunctionInputId(char * functionIdString,char * inputIdString){
  CObjectListItem * item = inputFunctionMap->getFirst();
  while (item != NULL){
    CInputFunctionMapItem * IFMItem = (CInputFunctionMapItem*)item->getObject();
    if (strcmp(functionIdString,IFMItem->getFunctionIdStr())==0){
      return IFMItem->setInputIdStr(inputIdString);
    }
    item = item->getNext();
  }
}

int CInputFunctionMap::executeFunction(char * inputIdStr, int argument ){
  CObjectListItem * item = inputFunctionMap->getFirst();
  while (item != NULL){
    CInputFunctionMapItem * IFMItem = (CInputFunctionMapItem*)item->getObject();
    if (strcmp(inputIdStr,IFMItem->getInputIdStr())==0){
      return IFMItem->executeFunction(argument);
    }
    item = item->getNext();
  }
  return -1;
}

int CInputFunctionMap::executeFunction(char * inputIdStr ){
  CObjectListItem * item = inputFunctionMap->getFirst();
  while (item != NULL){
    CInputFunctionMapItem * IFMItem = (CInputFunctionMapItem*)item->getObject();
    if (strcmp(inputIdStr,IFMItem->getInputIdStr())==0){
      return IFMItem->executeFunction();
    }
    item = item->getNext();
  }
  return -1;
}