//
// $Id: Client.h,v 1.1 2003/07/24 20:00:30 sarfata Exp $
//
// Copyright vNET Project <vnet@vnet-project.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; see the file COPYING.  If not, write to:
//   The Free Software Foundation, Inc.
//   59 Temple Place, Suite 330
//   Boston, MA 02111 USA
//


class Client;

#ifndef __CLIENT_H__
#define __CLIENT_H__

#include <stdio.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <math.h>
#include <SDL/SDL.h>
#include <vector>
#include <Entity.h>

class World;
class Shell;
namespace V3d {
  class Camera;
  class DrawEngine3D;
  class PositionEngine;
  class Texture;
  class Font;
}
namespace VGUI {
  class Widget;
  class GUI;
  class Mouse;
  class Input;
  class PanelCmdLine;
  class FloatingMenu;
  class PanelHistory;
  class PanelWrapText;
  class PanelHour;
  class PanelTarget;
}
class ProbeManager;

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_SOUND
#  include "SoundPlayer.h"
#endif
#include <ConfigObject.h>
#include <3d/vec.h>
#include <3d/Euler.h>

class ProbeConnection;

class Client
{
private:
  ConfigObject config;
 GLuint	ground_id; 
  float TimeElapsed;
  const Uint32& get_last_time(void)const {return LastTime;};
  float FPS;
  Uint32 LastTime;
  Uint32 LastFPS_Time;
  Uint32 LastContext_Time;
  long frame_nb;
  int Frames;
  SDL_Surface *SDL_surface;
  World *world;
  VGUI::GUI* gui;
  V3d::DrawEngine3D* engine;
  V3d::PositionEngine* position_engine;
  int ScreenWidth;
  int ScreenHeight;
  void gl_2d_Begin(void);
  void gl_2d_End(void);
  VGUI::PanelCmdLine *panelcmdline;
  VGUI::PanelHistory *panelhistory;
  VGUI::FloatingMenu *floating_menu;
  VGUI::PanelTarget *panel_targ;
  VGUI::PanelTarget *panel_select;
  VGUI::PanelWrapText *panel_help;
  VGUI::Mouse *mouse;
  bool mouse_move;
  Shell *shell;
  Entity *focusedentity;
  std::string focused_e_address;
  std::string focused_e_netmask;
  std::string focused_e_name;
  Entity::EntityType  focused_e_type;
  ProbeManager* probemanager;
#ifdef HAVE_SOUND
  SoundPlayer* sp;
#endif
    
  int alive;

  static void cmd_historyopen(void* client,int argc,char** argv);
  static void cmd_historyclose(void* client,int argc,char** argv);
  static void cmd_dispprop(void* client,int argc,char** argv);
  static void cmd_dumpxml(void* client,int argc,char** argv);
  static void cmd_importxml(void* client,int argc,char** argv);
  static void cmd_probeconnect(void* client, int argc, char** argv);
  static void cmd_probedisconnect(void* client, int argc, char** argv);
  static void cmd_probesend(void* client, int argc, char** argv);
  static void cmd_exit(void* client,int argc,char** argv);
  void make_ground();
  GLUquadricObj *quadratic;
  V3d::Vector3f select_dir;
  V3d::Vector3f select_center;
  float select_anim;
  float demo_anim;
  bool demo_mode;
  V3d::Texture* tube_tex;
  float tube_tex_offset;
  void draw_tube_link(const V3d::Vector3f& src,const V3d::Vector3f& dst,float radius,int sides,const V3d::Vector3f& color);
public:
  Client(const ConfigObject& config);
  ~Client();
  const std::string& getVersionString() const {static std::string ver ="vNET Project v"+getNumericVersionString();return ver;};
  const std::string& getNumericVersionString()const {static std::string numver = VERSION; return numver;};
  V3d::DrawEngine3D* get_draw_engine(){return engine;};
  void GUIattach(VGUI::Widget* widg);
  void GUIdetach(VGUI::Widget* widg);
  void displayHelp(std::string help);
  void displayHelp(std::vector<std::string> help);
  VGUI::Widget* KeyFocusedWidget;
  V3d::Font *font;
  VGUI::Input *input;
  Entity* getfocusedentity(void);
  void	set_cam_target(Entity* ent); 
  void resetkeyfocus(void);
  void setfocusedentity(Entity* entity);
  void ResetWorld(void);
  int GetScreenWidth();
  int GetScreenHeight();
  void draw_links();
  void draw_selection_balls();
  std::string get3DDir() const {return config.getOption("Data Directory")+"/models/";};
  std::string getGUIDir() const {return config.getOption("Data Directory")+"/GUI/";};
  std::string getSoundDir() const {return config.getOption("Data Directory")+"/sounds/";};
  std::string getAuthLogin() const {return config.getOption("AuthLogin");};
  std::string getAuthPass() const {return config.getOption("AuthPassword");};
  void historyadd(const char *text);
  void saveFile(std::string filename);
  void openFile(std::string filename);
  void importFile(std::string filename);
  void historytoggle(void);
  int run();
  char** getcmdlist();
  void parse(const char* cmdline);
  int isinside(int x,int y){return 1;};
  void draw(float TimeElapsed);
  void quit();
  float getfps();
  World* getworld() {return world;};
  void toggle_mouse();
  const bool get_mouse_move() const{return mouse_move;};
  int connecttoprobe(std::string url);
  ProbeConnection* get_probe();
#ifdef HAVE_SOUND
  int playsound(const std::string& file) {  
    if (sp && sp->isInitialized())
      return sp->play(file.c_str());
    else 
      return -1;
  };
#else 
  int playsound(const std::string& file) { return -1; };
#endif 
};

#endif