#include <stdio.h>
#include <Windows.h>
#include "PTRobot.h"

int main() {
    printf("Hello, world!\n");

    printf("Initializing\n");
    int res = PTRobot_Initialize();
    if (res) {
        printf("Failed with error: %i\n", res);
    }
    else {
        printf("Init OK!\n");
    }

    // Enumerate robots
    printf("Enumerating robots\n");
    HANDLE phRobots[256];
    DWORD handles = 256; // Set to the size of the array

    DWORD result = PTRobot_EnumRobots(phRobots, &handles); // Pass the address of handles

    bool ded = true;
    if (result == PTROBOT_OK) {
        printf("Successfully enumerated %lu robots.\n", handles);
        // You can now use phRobots array which contains the handles to the robots
        ded = false;
    }
    else if (result == PTROBOT_INVALID_ROBOT) {
        printf("No robots found.\n");
    }
    else if (result == PTROBOT_SEQUENCE) {
        printf("Command called out of sequence.\n");
    }
    else if (result == PTROBOT_INTERNAL) {
        printf("An internal error occurred.\n");
    }
    else if (result == PTROBOT_OVERFLOW) {
        printf("Number of robots found exceeds the provided array size.\n");
    }
    else {
        printf("Unknown error occurred.\n");
    }
    if (ded) { return 1; }

    // Get status
    PTRobotStatus status;
    result = PTRobot_GetRobotStatus(phRobots[0], &status);
    if (result == PTROBOT_OK) {
        printf("Successfully enumerated %lu robots.\n", handles);
        // You can now use phRobots array which contains the handles to the robots
    }
    else {
        printf("ERROR: %u", result);
        return 1;
    }
    printf("State: %u", status.dwSystemState);
    printf("Error: %u", status.dwSystemError);

    // Enumerate drives
    HANDLE phDrives[256];
    DWORD numDrives = 256;
    result = PTRobot_EnumDrives(phRobots[0], phDrives, &numDrives);

    if (result) {
        printf("Failed to enumerate drives with error %u", result);
        return 1;
    }
    else {
        printf("Found %u drives", numDrives);
    }

    // Load drive
    result = PTRobot_LoadDrive(phRobots[0], phDrives[0], 1, CLEARDRIVE_NO);
    printf("Load drive: %u", result);

    return 0;
}