//
// Copyright (c) 2014 Secret Geometry, Inc.  All rights reserved.
//

#ifndef TERMKIT_SSHCONNECTION_HPP
#define TERMKIT_SSHCONNECTION_HPP

#include "Types.hpp"
#include <stdint.h>
#include <map>
#include <string>
#include <pthread.h>

namespace term {

struct SSHResources;

// 
// SSHConnection
// 

class SSHConnection
{
  public:
    
    // 
    SSHConnection(const char* server,
                  const char* user,
                  const char* password,
                  unsigned int port,
                  size_t width,
                  size_t height,
                  const char* locale,
                  const char* termIdentifier,
                  StringMap& otherEnvars);
    
    // 
    ~SSHConnection();
    
    // 
    const char* lookupProcessName();
    
    //
    ConnectionState state() const;
    
    //
    void start();
    
    //
    void update();
    
    //
    const std::string& message() const;
    
    //
    void clearErrorMessage();
    
    //
    void logout();
    
    //
    void suspend();
    
    //
    void resume();
    
    //
    void setPassword(const char* password);
    
    //
    int processCount() const;
    
    //
    void updateProcessInfo();
    
    //
    void resize(size_t width, size_t height);
    
    //
    ssize_t read(uint8_t* str, size_t count);
    
    //
    ssize_t write(const uint8_t* str, size_t count);
    
    //
    void setKeyFile(const char* filename);
    
    //
    const std::string& activeProcessName() const;
    
    //
    const std::string& workingPath() const;
    
    //
    const StringVector& allProcessNames() const;
    
  private:
    
    bool running() const;
    
    static void* connectThreadMain(void* args);
    static void cleanup(SSHResources* resources);
    static void handlePipeError(int sig);
    static std::string getLastSSHError(SSHResources* resources);
    
    std::string     _server;
    std::string     _user;
    std::string     _password;
    std::string     _privateKeyFile;
    std::string     _message;
    std::string     _locale;
    std::string     _termIdentifier;
    std::string     _appIdentifier;
    std::string     _processName;
    std::string     _workingPath;
    StringVector    _processNames;
    StringMap       _otherEnvars;
    int             _port;
    size_t          _width;
    size_t          _height;
    pthread_t       _thread;
    pthread_mutex_t _mutex;
    ConnectionState _state;
    SSHResources*   _resources;
};
    
}

#endif
