#ifndef SERVERCONNECTION_H
#define SERVERCONNECTION_H

#include <winsock.h>
#include <iostream>
#include <string>

#define BUFSIZE 4096
bool closeError = false;

WORD wVersionRequested = MAKEWORD(1,1); // Version number
WSADATA data;                           // Struct with info. about library
SOCKET mySocket;
sockaddr_in serverSock;
std::string gRobotName = "";
std::string gConnectionAddress = "";
int gConnectionPort = 0;
hostent * gConnectionHost;

void setConnection();

bool connectionIsInitialize()
{
    return false;
}

std::string sendStringToServer(std::string const & value)
{
    if (send(mySocket, value.c_str(), value.size()+1, 0) == -1)
    {
        std::cout << "Problem with sending data to server" << std::endl;
        WSACleanup();
        return "error";
    }
    int size = -1;
    char buf[BUFSIZE];
    std::string text = "";
    //todo
    //while ((size != 0) && (size != -1))
    //{
        size = recv(mySocket, buf, BUFSIZE, 0);
        text.append(buf, buf + size);
    //}
    if (size == -1)
    {
        std::cout << "Cannot receive data" << std::endl;
        return "error";
    }
    return text;
}

std::string sendMessageToServer(std::string const & type, std::string const & value)
{
    std::string text = type + "\n" + value;
    text = sendStringToServer(text);
    if(text == "error")
    {
        closeError = true;
    }
    return text;
}

bool startClient()
{
    if(gRobotName == "")
    {
        std::cout << "Write name of your robot:" << std::endl;
        getline( std::cin, gRobotName, '\n' );
    }
    if(gConnectionAddress == "")
    {
        std::cout << "Write address of server:" << std::endl;
        getline( std::cin, gConnectionAddress, '\n' );
    }
    if(gConnectionPort == 0)
    {
        std::cout << "Write communication port on server:" << std::endl;
        std::cin >> gConnectionPort;
    }

    if (WSAStartup(wVersionRequested, &data) != 0)
    {
        std::cout << "Cannot initialize sockets" << std::endl;
        return false;
    }
    if ((gConnectionHost = gethostbyname(gConnectionAddress.c_str())) == NULL)
    {
        std::cout << "Wrong IP address" << std::endl;
        WSACleanup();
        return false;
    }
    if ((mySocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == 0)
    {
        std::cout << "Cannot create socket" << std::endl;
        WSACleanup();
        return false;
    }
    serverSock.sin_family = AF_INET;
    serverSock.sin_port = htons(gConnectionPort);
    memcpy(&(serverSock.sin_addr), gConnectionHost->h_addr, gConnectionHost->h_length);
    if (connect(mySocket, (sockaddr *)&serverSock, sizeof(serverSock)) == -1)
    {
        std::cout << "Cannot create connection - server isn't available" << std::endl;
        WSACleanup();
        return false;
    }

    while( sendMessageToServer("connect",gRobotName) == "0" )
    {
        std::cout << "On server is already connected robot with same name" << std::endl
                  << "Write different robot name:" << std::endl;
        getline( std::cin, gRobotName, '\n' );
    }
    std::cout << "Connected to server..." << std::endl;
    return true;
}

bool endClient()
{
    sendMessageToServer("finish","");
    closesocket(mySocket);
    WSACleanup();
    std::cout << "Disconnected from server..." << std::endl;
    return true;
}

#endif // SERVERCONNECTION_H

