/*
 * 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 "pvvmudclient.H"

#include "glrenderer.H"
#include "gltexture.H"
#include "glsrvcliconsole.H"

CPvvmudClient::CPvvmudClient(int argc,char * argv[]) {
  m_option = new COption;
  //m_stdInput = new CStdInput();
  m_option->setValue("server",'s',"localhost");
  m_option->setValue("username",'u',"Guest");
  m_option->setValue("passwd",'p',"pvv");

  m_option->loadOption("pvvmud.conf");
  m_option->parseArguments(argc,argv);
  
  initGlobalData(m_option);
  m_gui = new CGlutGUI(argc,argv,m_renderer, this, 400, 300);
  m_renderer = new COGLRenderer(this);
  m_console = new CGLSrvCliConsole(this, 40, 20);
  m_console->setColor(0.8, 0.8, 0.8);
  m_commandParser = new CCliCmdParser(this);
  m_gui->setRenderer(m_renderer);
  m_inputFunction->setRenderer(m_renderer);
  m_inputFunction->setConsole(m_console);
  m_renderer->setConsole(m_console);
  GLTexture_Init();
  m_geometryCache = new CCliGeometryCache(getManager());
  m_materialCache = new CCliMaterialCache(getManager());
  m_textureCache = new CCliTextureCache(getManager());
  m_lastTime.getTime();

  cdebug << "GeoCache at addr: " << m_geometryCache << "\n";

  m_manager->init();


}

void CPvvmudClient::run() {
  m_gui->run();
}

void CPvvmudClient::initGlobalData(COption * option) {
  CClientSocket * socket;

  if ((m_option->getString("server"))==NULL) {
    cdebug << "Error : Argument error in -server\n";
    exit(1);
  }
  try {
    CInetAddress addr(m_option->getString("server"), SERVER_CLIENT_PORT);
    socket = new CClientSocket(addr.getAddress(), addr.getPort() );
  } catch (CSocketException * e){
    delete e;
    cdebug << "Failed to connect to server!\n";
    exit(1);
  }
  socket->setNonBlocking();
  m_timeKeeper = new CTimeKeeper();
  m_manager = new CCliSrvManager( new CCliSrvProtocol( socket,m_timeKeeper ), m_timeKeeper);
  m_manager->setClient(this);

  m_inputFunction = new CInputFunction(m_manager, m_renderer);
  if (m_inputFunction == NULL){
    cdebug << "Out of memory!!!\n";
    exit(23);
  }

  m_timeKeeper->addHeartBeat(512,this);

}


CGeometryCache * CPvvmudClient::getGeometryCache() {
  return m_geometryCache;
}

CTextureCache * CPvvmudClient::getTextureCache() {
  return m_textureCache;
}

CMaterialCache * CPvvmudClient::getMaterialCache() {
  return m_materialCache;
}

CConsole * CPvvmudClient::getConsole() {
  return m_console;
}

CCliSrvManager * CPvvmudClient::getManager() {
  return m_manager;
}

CTimeKeeper * CPvvmudClient::getTimeKeeper() {
  return m_timeKeeper;
}

CGUI * CPvvmudClient::getGUI() {
  return m_gui;
}

CInputFunction * CPvvmudClient::getInputFunction() {
  return m_inputFunction;
}

CStdInput * CPvvmudClient::getStdInput() {
  return m_stdInput;
}

COption * CPvvmudClient::getOption() {
  return m_option;
}

int CPvvmudClient::timeKeeperHB(){
  m_manager->sendPing();
  return TRUE;
}

void CPvvmudClient::pollConsole(){
  const char * msg = getConsole()->getLine();
  if (msg != NULL) {
    m_commandParser->parseCommand(msg);
  }
}