#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#include <unistd.h> // for close()

// load environment variables from .env file
#include <fstream>
#include <cstdlib>

void load_env(const std::string& path) {
    std::ifstream f(path);
    std::string line;
    while (std::getline(f, line)) {
        if (line.empty() || line[0] == '#') continue;
        auto pos = line.find('=');
        if (pos == std::string::npos) continue;

        std::string key = line.substr(0, pos);
        std::string val = line.substr(pos + 1);

        #ifdef _WIN32
                _putenv_s(key.c_str(), val.c_str());
        #else
                setenv(key.c_str(), val.c_str(), 1);
        #endif
    }
}

int main(void){
    load_env(".env");
    // Declare variables
    const char *server_ip = std::getenv("SERVER_IP");
    const int server_port = std::atoi(std::getenv("SERVER_PORT"));
    printf("Connecting to server %s:%d\n", server_ip, server_port);
    
    char client_message[1024];
    char server_message[1024];
    const char * custom_message="Zheyuan Wu: ";
    
    // Create socket:
    int client_socket = socket(AF_INET, SOCK_STREAM, 0);
    if (client_socket == -1) {
        perror("Failed to create socket");
        return 1;
    }else{
        printf("Socket created successfully\n");
    }

    // Send connection request to server, be sure to set por tand IP the same as server-side
    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(server_port);
    inet_pton(AF_INET, server_ip, &server_addr.sin_addr);

    if (connect(client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
        perror("Failed to connect to server");
        close(client_socket);
        return 1;
    }else{
        printf("Connected to server successfully\n");
    }

    // Get input from the user:
    printf("Enter message sent to the server (type \\quit to exit): ");
    // clean the buffer
    memset(client_message, 0, sizeof(client_message));
    if (fgets(client_message, sizeof(client_message), stdin) == NULL) {
        // EOF or error reading from stdin, exit the loop
        perror("Error reading from stdin");
        return 1;
    }
    
    while (strcmp(client_message, "\\quit\n") != 0) {
        
        // Send the message to server:
        // add my name in the front
        char buffer[2048];
        std::snprintf(buffer, sizeof(buffer), "%s%s", custom_message, client_message);
        send(client_socket, buffer, sizeof(buffer), 0);

        // Receive the server's response:
        recv(client_socket, server_message, sizeof(server_message), 0);


        printf("Server's response: %s\n", server_message);

        printf("Enter message sent to the server (type \\quit to exit): ");

        // clean the buffer
        memset(client_message, 0, sizeof(client_message));
        memset(server_message, 0, sizeof(server_message));
        
        if (fgets(client_message, sizeof(client_message), stdin) == NULL) {
            // EOF or error reading from stdin, exit the loop
            perror("Error reading from stdin");
            break;
        }
    }

    // Close the socket
    close(client_socket);

    return 0;
}