
#include "ANSANNHUB.h"
#include <iostream>
#include <chrono>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>

std::vector<double> readCSVTo1DArray(const std::string& filename) {
    std::vector<double> result;
    std::ifstream file(filename);

    if (!file.is_open()) {
        std::cerr << "Error opening file: " << filename << std::endl;
        return result;
    }

    std::string line;
    while (std::getline(file, line)) {
        std::stringstream ss(line);
        std::string value;
        while (std::getline(ss, value, ',')) {
            try {
                result.push_back(std::stod(value));
            }
            catch (const std::invalid_argument& e) {
                std::cerr << "Invalid number: " << value << std::endl;
            }
            catch (const std::out_of_range& e) {
                std::cerr << "Number out of range: " << value << std::endl;
            }
        }
    }

    file.close();
    return result;
}

int IRISTest() {
    ANSCENTER::ANNHUBAPI* annhubObject;
    std::string annhubModelPath = "C:\\ProgramData\\ANSCENTER\\Shared\\annhubmodels\\IRIS_model.ann";
    double originalInput[4];
    originalInput[0] = 5.8;
    originalInput[1] = 2.8;
    originalInput[2] = 5.1;
    originalInput[3] = 2.4;

    double* outputHandle = nullptr;
    for (int i = 0; i < 10; i++) {
        CreateANNHUBHandle(&annhubObject, "", annhubModelPath.c_str());

        auto start = std::chrono::high_resolution_clock::now(); // Start timing

        ANNHUB_Inference(&annhubObject, originalInput, 4, &outputHandle);

        auto end = std::chrono::high_resolution_clock::now(); // End timing
        auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); // Calculate elapsed time in milliseconds

        printf("Output =[%f, %f, %f]\n", outputHandle[0], outputHandle[1], outputHandle[2]);
        std::cout << "Time elapsed: " << elapsed.count() << " ms\n";

        ReleaseANNHUBHandle(&annhubObject);
    }

    std::cout << "End of the application!\n";
}
int XORTest() {
    ANSCENTER::ANNHUBAPI* annhubObject;
    std::string annhubModelPath = "C:\\ProgramData\\ANSCENTER\\Shared\\annhubmodels\\XOR_model.ann";
    double originalInput[2];
    double* outputHandle = nullptr;
    CreateANNHUBHandle(&annhubObject, "", annhubModelPath.c_str());

    originalInput[0] = 0;
    originalInput[1] = 0;
    ANNHUB_Inference(&annhubObject, originalInput, 2, &outputHandle);
    printf("Output =[%f]\n", outputHandle[0]);

    originalInput[0] = 0;
    originalInput[1] = 1;
    ANNHUB_Inference(&annhubObject, originalInput, 2, &outputHandle);
    printf("Output =[%f]\n", outputHandle[0]);


    originalInput[0] = 1;
    originalInput[1] = 0;
    ANNHUB_Inference(&annhubObject, originalInput, 2, &outputHandle);
    printf("Output =[%f]\n", outputHandle[0]);

    originalInput[0] = 1;
    originalInput[1] = 1;
    ANNHUB_Inference(&annhubObject, originalInput, 2, &outputHandle);
    printf("Output =[%f]\n", outputHandle[0]);

    ReleaseANNHUBHandle(&annhubObject);

    std::cout << "End of the application!\n";
}


int CustomerTest() {
    ANSCENTER::ANNHUBAPI* annhubObject;

    std::string filename = "C:\\Workstation\\CustomerSupport\\Michael\\ANNHUB\\test.txt";
    std::vector<double> data = readCSVTo1DArray(filename);


    std::string annhubModelPath = "C:\\Workstation\\CustomerSupport\\Michael\\ANNHUB\\CorrectTrainedModel_lv.ann";
    double originalInput[1024] = { 0 }; // Initialize the array with zeros
    double* outputHandle = nullptr;
    // Copy data to originalInput array
    std::copy(data.begin(), data.end(), originalInput);

    int createHandle= CreateANNHUBHandle(&annhubObject, "", annhubModelPath.c_str());
    printf("Created Handle =%d\n", createHandle);

    ANNHUB_Inference(&annhubObject, originalInput, data.size(), &outputHandle);
    printf("Output =[%f]\n", outputHandle[0]);
    printf("Output =[%f]\n", outputHandle[1]);

    
    ReleaseANNHUBHandle(&annhubObject);

    std::cout << "End of the application!\n";
}
int main()
{
    CustomerTest();
    //XORTest();
	//IRISTest();
	return 0;

}
