/***************************************************************************
                          io_internet.h  -  description
                             -------------------
    begin                : Thu Aug 16 2001
    copyright            : (C) 2003 by Troy Corbin Jr.
    email                : tcorbin@users.sourceforge.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef IO_INTERNET_H
#define IO_INTERNET_H

/* Local */
#include "definitions.h"
#include "io_base.h"
#include "match_param.h"
#include "command.h"

/* Tabs */
#include "tabmanager.h"
#include "console.h"
#include "challenge_graph.h"
#include "tab_seeklist.h"

class TDESocket;
class TDEProcess;
class resource;
class core;
class dlg_login;
class dlg_challenge;

#include <tqfile.h>

/**
  *@author Troy Corbin Jr / Alexander Wels.
  */

struct MatchRecord
{
  int         knightsID;
  int         serverID;
  bool        observing;
  bool        examining;
  bool        finished;
};

const int READ_BUFFER_SIZE = 64 * 1024; /* 64k is max ip packet size */

const int LOGIN_STAGE_NAME = 1;
const int LOGIN_STAGE_PASSWORD = 2;
const int LOGIN_STAGE_LOGGED_IN = 3;

const unsigned int NORMAL_MODE = 1;
const unsigned int UPDATE_SOUGHT_MODE = 2;
const unsigned int NEW_GAME_MODE = 3;
const unsigned int OBSERVE_GAME_MODE = 4;
const unsigned int CHALLENGE_MODE = 5;
const unsigned int PARSE_MOVE_LIST_MODE = 6;

const unsigned int PARSE12_MODE_NEW = 1;
const unsigned int PARSE12_MODE_MOVE = 2;

typedef TQMap<TQWidget *, TQWidget *> TabMap;

class io_internet : public io_base
{
  Q_OBJECT
  
  public:
                              io_internet(TQWidget *parent = NULL, resource *Rsrc = NULL);
                              ~io_internet();

  public slots:
    virtual void              recvCMD(const Command& command);
    void                      readCommand(TDESocket *);
    void                      sendUserName(void);
    void                      sendPassword(void);
    void                      send(const TQString&);
    void                      nullifyChallenge( void );
    void                      acceptChallenge( void );
    void                      declineChallenge( void );
    void                      processSeekTimer( void );
    void                      connectToServer( void );
    void                      selfDestruct( void );
    void                      setUserInfo(TQString userName, TQString passWord);
  signals:
    virtual void              sendCMD(const Command& command);
    void                      setLastPrivateSource( const TQString& );
    void                      setLastChannelSource( const TQString& );

  protected:
    void                      parseLine(TQString command);
    void                      parseLoginData(TQString command);
    void                      writeToConsole(TQString text, TQString tag);
    void                      updateSoughtList(TQString text);
    void                      parseStyle12(TQString line, const unsigned int mode);
    void                      sendUserSettings(void);
    void                      parseMoveList(TQString data);
    void                      sendEndOfGameCommand(TQString line);
  private:

    dlg_login                 *loginDlg;
    dlg_challenge             *challenge;

    /* These are used to handle the automagic seek command */
    TQTimer                    *seekTimer;
    
    /* These are used for the connection & logon */
    TQFile                     *Log;
    TDESocket                   *socket;
    TDEProcess                  *myTimeseal;
    struct serverResource     *myServer;
    int                       loginStage;
    unsigned int              parseMode;
    TQString                   lastTag;
    int                       ficsMoveListNumber;
    TQString                   lineBuffer;
    bool                      connected;
    TQString                   userName;
    TQString                   passWord;

    /* communication with the server */
    TQStringList               command_list; /* this is a temp buffer while waiting for the movelist */
    bool                      waiting_for_move_list;
    TQMap<match_param *, int>  fics_ids;
    TQMap<int, int>            fics_to_knights; /* maps fics id's to knights id's */
    TabMap                    myTabList; /* maintains the list of the tabs currently owned by this io_internet */
    TQValueList<Command>       myCommandBuffer; /* this is a buffer for all the commands io_internet received while not connected to a server */
};

#endif
