
#include "iobox_api.h"
#include <thread>

iobox_api ioBoxApp((char*)"239.255.255.250", 12345);
void userInputThread(iobox_api* api) {
    std::string current_ip;

    while (true) {
        std::string input;
        std::cout << std::endl;
        std::cout << "Enter command followed by Enter: exit, scan, setip, connect, channels, disconnect, show, get, set, restart, setauthen, resetauthen" << std::endl;
        // std::cin >> input;       // This will only get the first word
        std::getline(std::cin, input);  
        if (input == "exit") {
            break;
        }
        else if(input == "scan") {
            std::vector<std::string> devices = api->scanNetworkDevicesMulticast(5);
            std::cout << "Found devices: " << devices.size() << std::endl;
            for (const std::string& device : devices) {
                std::cout << device << std::endl;
            }
            continue;
        }
        else if(input == "setip") {
            std::cout << "Enter IP: ";
            std::getline(std::cin, current_ip);  
            continue;
        }
        else if(input == "connect") {
            if(current_ip == "") {
                std::cout << "Please setip address first" << std::endl;
                continue;
            }
            std::string username, password;
            std::cout << "Enter username: ";
            std::cin >> username;
            std::cout << "Enter password: ";
            std::cin >> password;
            bool connect = api->connectToIobox(current_ip, DEVICE_TCP_PORT, username, password);
            std::cout << "Connection to " << current_ip << (connect ? " succeeded." : " failed.") << std::endl;
            continue;
        }
        else if(input == "disconnect") {
            bool disconnect = api->disconnectToIobox(current_ip);
            std::cout << "Disconnect to " << current_ip << (disconnect ? " succeeded." : " failed.") << std::endl;
            // current_ip = "";
            continue;
        }
        else if(input == "show") {
            api->show_profile_ioboxs();
            continue;
        }
        else if(input == "get") {
            std::string channel;
            std::cout << "Enter channel: ";
            std::cin >> channel;
            std::string value = api->getValueDataStringIoboxFromChannelName(current_ip, channel);
            std::cout << "Value of " << channel << ": " << value << std::endl;
            continue;
        }
        else if(input == "set") {
            std::string  value;
            std::string channel;
            std::cout << "Enter channel: ";
            std::cin >> channel;
            std::cout << "Enter value: ";
            std::cin >> value;
            bool set = api->setValueDataStringIoboxFromChannelName(current_ip, channel, value);
            std::cout << "Set value to " << current_ip << (set ? " succeeded." : " failed.") << std::endl;
        }
        else if(input == "restart") {
            std::string token = api->generateToken(current_ip);
            bool reset = api->resetIobox(current_ip, token);
            std::cout << "Restart " << current_ip << (reset ? " succeeded." : " failed.") << std::endl;
        }
        else if(input == "setauthen") {
            std::string username, password;
            std::cout << "Enter username: ";
            std::cin >> username;
            std::cout << "Enter password: ";
            std::cin >> password;
            bool set = api->setAuthenticationIobox(current_ip, username, password);
            std::cout << "Set authentication to " << current_ip << (set ? " succeeded." : " failed.") << std::endl;
        }
        else if(input == "resetauthen") {
            std::string token = api->generateToken(current_ip);
            bool reset = api->resetAuthenticationIobox(current_ip, token);
            std::cout << "Reset authentication to " << current_ip << (reset ? " succeeded." : " failed.") << std::endl;
        }
        else if(input == "channels") {
            std::vector<std::string> channels = api->getDeviceChannelNames(current_ip);
            std::cout << "Channels of " << current_ip << std::endl;
            for(const std::string& channel : channels) {
                std::cout << channel << std::endl;
            }
        }
        else {
            // std::cout << "Invalid command" << std::endl;
        }
    }
}
int main() {

    std::thread userThread(userInputThread, &ioBoxApp);
    userThread.join();

    std::cout << "Main thread is done" << std::endl;
    return 0;
}
