#ifndef TELNET_H
#define TELNET_H

#include <string>
#include <iosfwd>
#include <sys/socket.h>
#include <pthread.h>
#include <string>
#include <cstring>
#include <netdb.h>
#include <netinet/in.h>
#include <deque>
using std::string;
using std::string;
using namespace std;


enum BOOLEAN
{
	FALSE = 0,
	TRUE = 1,

};

enum Verbs
{
	IS = 0,
	SEND = 1,
	SE = 240,
	SB	= 250,
	WILL = 251,
	WONT = 252,
	DO = 253,
	DONT = 254,
	IAC = 255,
				   /* interpret as subnegotiation */

};

enum Options
{
	ECHO = 1,
	RCP = 2,
	SGA = 3,
	NAMS = 4,
	NAWS = 31,
	TERMINAL_TYPE = 24,

};

enum {
    OPTINDEX_NAWS,
    OPTINDEX_TSPEED,
    OPTINDEX_TTYPE,
    OPTINDEX_OENV,
    OPTINDEX_NENV,
    OPTINDEX_ECHO,
    OPTINDEX_WE_SGA,
    OPTINDEX_THEY_SGA,
    OPTINDEX_WE_BIN,
    OPTINDEX_THEY_BIN,
    NUM_OPTS
};






void *ParseTelnet(void* params);

class Telnet
{

private:

	hostent *host;                                                  // Vzdálený počítač;
	sockaddr_in serverSock;                                         //konfigurace socketu

	char init;                                                      //Informace o kompletnosti spojeni

	pthread_mutex_t dataMutex;                                      //Datovy zamek
	pthread_cond_t dataCond;                                        //Datova podminena promenna
	pthread_t listenerThread;                                       //handler vlakna pro data z telnetu
	string data;                                                    //Prijata data
	int  sendOption(unsigned char cmd, unsigned char option);       //Odeslani konfiguracniho paketu
        char sendSubnegotiation(unsigned char cmd, int paramsLen,unsigned char* params);
public:
	BOOLEAN isConnected(void);                                      //Informace o spojeni
	pthread_mutex_t * GetReaderLock(void);                          //Vraci zamek datove fronty
	pthread_cond_t * GetReaderCond(void);                           //Vraci podminenou promennou dat

	int mySocket;                                                   //Socket spojeni
	int AvailableData(void);                                        //Informace o mnozstvi prijatych dat
	Telnet(char * host, int port);                                  //KOnstruktor tridy
	string ReadData(void);                                          //Vraci prijata data
	int WriteData(string input);                                    //Data k odeslani

	friend void *ParseTelnet(void* params);                         //Zpratelena parsovaci funkce
};





#endif
