
#pragma once

#include <stream.h>

#include "other.h"
#include "history.h"

class Telnet;

class Library
{
  friend class Barton;
  friend class CARL;
  friend class BU_Library;
  friend class UC_MELVYL;
  friend class Harvard;

  enum { _Closed,
	 _SetUp,
	 _Logon,
	 _Operate } state;
  enum { _none,
	 _Dropped,
         _Busy,
	 _Down,
	 _Bug } result;

  Telnet *tn =NULL;

  History*          ohist;

  virtual void      state_update();

  bool   use_vt100_if_available;

  String nl;
  String cr;
  String bs;
  String del;
  String strRedraw;

  int    number_of_logon_steps;
  int    logon_step;

  struct { String Trigger  ="";
	   int    Delay    = 0;
	   String Action   ="";
	   String vtAction ="";
	   String hdAction ="";
	 }                 LogonSequence[5];

  String strLogoff;

  int    response_timeout;

  virtual int       LoggingOn (bool just_now_connected);
  virtual void      Send_Logoff ();

  String strDropped;
  String strBusy;
  String strDown;
  String strBug;
  String strElaboration;

 public:

  String HumanName;
  String HostName;
  String HostNumber;

  bool supports_vt100;
  bool supports_hardcopy;
  bool supports_redraw;

  Doc    Help;
  String strBanner;

  Library();
  ~Library();
  virtual void Open(bool vt100p =0);
  virtual void Close();

  virtual void redraw();

  int theReadFileDescriptor ();
  virtual int read  (char* buf, int nchars);

  int IncrementalWrite(char*buf,int nchars);

  virtual int write (char* buf, int nchars);
  virtual int write (char* buf) { return write(buf,strlen(buf)); };
  virtual int write (String& s) { return write(s,s.length()); };

  virtual bool isStarting();
  virtual bool isOperational();
          bool isNotOpen() { return !isStarting() && !isOperational(); };
  virtual bool wasDropped();
  virtual bool wasBusy();
  virtual bool wasDown();
  virtual bool hadBug();

  virtual void Describe_Failure(ostream&);
};
