/*
 * 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 "pvvmud.H"
#include <stdio.h>
#include <stdlib.h>
#include "client.H"
#include "clisrvmessage.H"
#include "clisrvmanager.H"

///////////////////////////////////////////////////////////////////////////////
//
// CMsgHelloCli
// 
///////////////////////////////////////////////////////////////////////////////

CMsgHelloCli::CMsgHelloCli():CMsgHello(){
}

CMsgHelloCli * CMsgHelloCli::createMsg(){
  return new CMsgHelloCli();
}

int CMsgHelloCli::executeMsg(){
  cdebug << "Hello from server!\n";
  CCliSrvManager * manager = (CCliSrvManager*)getManager();
  if (getVersion() != PROTOCOL_VERSION){
    cdebug << "ERROR: Wrong protocol version!\n";
    exit(2);
  }
  manager->hello();
  return TRUE;
}


///////////////////////////////////////////////////////////////////////////////
//
// CMsgServerInfoCli
//
///////////////////////////////////////////////////////////////////////////////

CMsgServerInfoCli::CMsgServerInfoCli():CMsgServerInfo(){
}

CMsgServerInfoCli * CMsgServerInfoCli::createMsg(){
  return new CMsgServerInfoCli();
}

int CMsgServerInfoCli::executeMsg(){
  CInetAddress * worldSrvAddress, * gosAddress;
  worldSrvAddress = getWorldSrvAddress();
  gosAddress = getGOSAddress();
  cdebug << "Server info: WorldSrv" << worldSrvAddress->getAddressString() << "GOS"  << worldSrvAddress->getAddressString() << " world " << gosAddress->getAddressString() << "\n";
  CCliSrvManager * manager = (CCliSrvManager*)getManager();
  manager->serverInfo(worldSrvAddress,gosAddress);
  return TRUE;
}


///////////////////////////////////////////////////////////////////////////////
//
// CMsgByeCli
//
///////////////////////////////////////////////////////////////////////////////

CMsgByeCli::CMsgByeCli():CMsgBye(){
}

CMsgByeCli * CMsgByeCli::createMsg(){
  return new CMsgByeCli();
}

int CMsgByeCli::executeMsg(){
  cout << "Bye from server: " << getReasonString(getReason()) << "\n";
  CCliSrvManager * manager = (CCliSrvManager*)getManager();
  manager->exit();
  return TRUE;
}

/*

///////////////////////////////////////////////////////////////////////////////
//
// CMsgGeometryObjectCli
//
///////////////////////////////////////////////////////////////////////////////

CMsgGeometryObjectCli::CMsgGeometryObjectCli():CMsgGeometryObject(){
}

CMsgGeometryObjectCli * CMsgGeometryObjectCli::createMsg(){
  return new CMsgGeometryObjectCli();
}

int CMsgGeometryObjectCli::executeMsg(){
  double pos[3],dir[3];
  cdebug << "New geometryobject(" << getWorldObjectId() << ") from server with geometry(" << getGeometryId() << ") and parent(" << getParentId() << ")!\n",
  getPosition(pos);
  getDirection(dir);
  cdebug ("Pos: %f %f %f Dir: %f %f %f\n",pos[0],pos[1],pos[2],dir[0],dir[1],dir[2]);

  CCliSrvManager * manager = (CCliSrvManager*)getManager();
  manager->addWorldObject(getParentId(),getWorldObjectId(),
                          getGeometryId(),pos,dir);
  return TRUE;
}


///////////////////////////////////////////////////////////////////////////////
//
// CMsgGeoObjRemoveCli
//
///////////////////////////////////////////////////////////////////////////////

CMsgGeoObjRemoveCli::CMsgGeoObjRemoveCli():CMsgGeoObjRemove(){
}

CMsgGeoObjRemoveCli * CMsgGeoObjRemoveCli::createMsg(){
  return new CMsgGeoObjRemoveCli();
}

int CMsgGeoObjRemoveCli::executeMsg(){
  cdebug("Remove geometryobject(%i) from parent(%i)\n",getWorldObjectId(),
         getParentId());
  CCliSrvManager * manager = (CCliSrvManager*)getManager();
  manager->removeWorldObject(getWorldObjectId(),getParentId());
  return TRUE;
}


///////////////////////////////////////////////////////////////////////////////
//
// CMsgUpdatePosition
//
///////////////////////////////////////////////////////////////////////////////

CMsgUpdatePositionCli::CMsgUpdatePositionCli():CMsgUpdatePosition(){
}

CMsgUpdatePositionCli * CMsgUpdatePositionCli::createMsg(){
  return new CMsgUpdatePositionCli();
}

int CMsgUpdatePositionCli::executeMsg(){
  double pos[3];
  CCliSrvManager * manager = (CCliSrvManager*)getManager();
  getPosition(pos);
  manager->updatePosition(getWorldObjectId(),pos);
  return TRUE;
}

///////////////////////////////////////////////////////////////////////////////
//
// CMsgUpdateDirection
//
///////////////////////////////////////////////////////////////////////////////

CMsgUpdateDirectionCli::CMsgUpdateDirectionCli():CMsgUpdateDirection(){
}

CMsgUpdateDirectionCli * CMsgUpdateDirectionCli::createMsg(){
  return new CMsgUpdateDirectionCli();
}

int CMsgUpdateDirectionCli::executeMsg(){
  double dir[3];
  CCliSrvManager * manager = (CCliSrvManager*)getManager();
  getDirection(dir);
  manager->updateDirection(getWorldObjectId(),dir);
  return TRUE;
}
*/

///////////////////////////////////////////////////////////////////////////////
//
// CMsgCommand
//
///////////////////////////////////////////////////////////////////////////////

CMsgCommandCli::CMsgCommandCli():CMsgCommand(){
}

CMsgCommandCli * CMsgCommandCli::createMsg(){
  return new CMsgCommandCli();
}

int CMsgCommandCli::executeMsg(){
  CCliSrvManager * manager = (CCliSrvManager*)getManager();
  switch(getCommand()){
  case MSGCOMMAND_CLIENTCLEARWORLD:
    cdebug << "MsgCommand: Clear world\n";
    manager->clearWorld();
    break;
  }
  return TRUE;
}

///////////////////////////////////////////////////////////////////////////////
//
// CMsgViewpointCli
//
///////////////////////////////////////////////////////////////////////////////

CMsgViewpointCli::CMsgViewpointCli():CMsgViewpoint(){
}

CMsgViewpointCli * CMsgViewpointCli::createMsg(){
  return new CMsgViewpointCli();
}

int CMsgViewpointCli::executeMsg(){
  CPosition pos; CDirection dir;
  CCliSrvManager * manager = (CCliSrvManager*)getManager();
  getPosition(pos); getDirection(dir);
  manager->updateViewpoint(pos,dir,getTargetId());
  return TRUE;
}

///////////////////////////////////////////////////////////////////////////////
//
// CMsgMsgCli
//
///////////////////////////////////////////////////////////////////////////////

CMsgMsgCli::CMsgMsgCli():CMsgMsg(){
}

CMsgMsgCli * CMsgMsgCli::createMsg(){
  return new CMsgMsgCli();
}

int CMsgMsgCli::executeMsg(){
  char * msg = (char*)malloc(getMsgLength());
  getMsg(msg);
  CCliSrvManager * manager = (CCliSrvManager*)getManager();
  manager->getClient()->getConsole()->addMsg(msg);
  cdebug << "MSG: " << msg << "\n";
  free(msg);
  return TRUE;
}





///////////////////////////////////////////////////////////////////////////////
//
// CMsgActionListCli
//
///////////////////////////////////////////////////////////////////////////////

CMsgActionListCli::CMsgActionListCli():CMsgActionList(){
}

CMsgActionListCli * CMsgActionListCli::createMsg(){
  return new CMsgActionListCli();
}

int CMsgActionListCli::executeMsg(){
  CCliSrvManager * manager = (CCliSrvManager*)getManager();
  cdebug << "Action list for object: " << getObjectId() << "\n";
  CObjectListItem * item = getActionList()->getFirst();
  while (item != NULL){
    CAction *  action = (CAction*)item->getObject();
    cdebug << "Action: " << action->getId() << " " << action->getName() << "\n";
    manager->addAction(getObjectId(),action);
    item = item->getNext();
  }
  return TRUE;
}

///////////////////////////////////////////////////////////////////////////////
//
// CMsgPlayerInfoCli
//
///////////////////////////////////////////////////////////////////////////////

CMsgPlayerInfoCli::CMsgPlayerInfoCli():CMsgPlayerInfo(){
}

CMsgPlayerInfoCli * CMsgPlayerInfoCli::createMsg(){
  return new CMsgPlayerInfoCli();
}

int CMsgPlayerInfoCli::executeMsg(){
  CCliSrvManager * manager = (CCliSrvManager*)getManager();
  cdebug << "Player info with mastercell : " << getMasterCellId() << "\n",
  manager->changeMasterCellId(getMasterCellId());
  return TRUE;
}