#ifdef WIN32
#define STL_USING_ALL
#include "stl.h"
#endif

#include <GL/glut.h>
#include <iostream>
#include <fstream>
#include <strstream>
#include <string>
#include <sstream>
#include <strstream>
#include <vector>
using namespace std;

#include "Network.h"
#include "Multithread.h"

#include "CWinStream.h"
#include "CMove.h"
#include "CSafeFile.h"
#include "Pieces.h"
#include "CBoard.h"
#include "CException.h"
#include "glpng/glpng.h"
#include "CTexture.h"
#include "CWindow.h"
#include "CActiveWin.h"
#include "CWindowSection.h"
#include "CSectionBoard.h"
#include "CMainWin.h"
#include "CStartUpWin.h"

#include "CNetMessage.h"
#include "CNetStream.h"

#include "CGameInfo.h"

CWinStream wout;
CGameInfo g_gameinfo;



void VideBoard(CBoard& board)
{
  int i, j;

  for (i = 0; i < 8; i++)
    for (j = 0; j < 8; j++)
      {
        if (board(i, j) == 0)
          delete board(i, j);
        board(i, j) = new CEmpty(&board, i, j);
      }
}



#define NEWPI(piece, x, y, color) {                 \
  delete Board(x,y);                                \
  Board(x,y) = new C##piece##(&Board, x, y, color); \
}

string Text;

int main(int argc, char **argv)
{
  try
    {
      NetInit();
      int port;

#ifdef SERVER_VERSION
      cout << "Entrez le port a ecouter: ";
      cin >> port;

      g_gameinfo.PlayerColor = WHITE;
      Text = "SERVEUR";

      n_in_out = new CServerStream(port);

      cout << "Je suis \"" << n_in_out->GetMachineName() << "\" a "
           << n_in_out->GetDottedIP() << endl << endl;

      n_in_out->Connect();

      cout << "Connection recu de " << n_in_out->GetDottedIPother()
           << endl << endl;

#elif defined CLIENT_VERSION
      string IP;
      cout << "Entrez le IP auquel se connecter:" << endl;
      cin >> IP;

      cout << "Entrez le port auquel se connecter: ";
      cin >> port;

      g_gameinfo.PlayerColor = BLACK;
      Text = "CLIENT";

      n_in_out = new CClientStream(port, "");

      cout << "Je suis \"" << n_in_out->GetMachineName() << "\" a "
           << n_in_out->GetDottedIP() << endl << endl;

      n_in_out->SetDottedIPother(IP);

      n_in_out->Connect();

      cout << "Connection a " << n_in_out->GetDottedIPother() << endl << endl;

#else

#endif

      glutInit(&argc, argv);
      glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);

      g_Window = new CActiveWin;

      p_MainWin = new CMainWin(string("Main Window"));
      p_StartUpWin = new CStartUpWin;

      *g_Window = p_MainWin;

      CreateNewThread(ListenNetworkMessage, 0);

      glutMainLoop();
    }
  catch (CNetworkException e)
    {
      cout << e.GetErrorString() << endl;
      exit(1);
    }

  return 0;
}
