//
// Created by marob on 12/20/2023.
//

#ifndef LSOBJECT_H
#define LSOBJECT_H
#include <ISXDK.h>
#include <vector>
#include <sstream>

#include "ISXMr.h"

template<typename T>
std::string toString(const T &value) {
    std::ostringstream oss;
    oss << value;
    return oss.str();
}

class LSObject {
public:
    explicit LSObject(const LSOBJECT &obj) : lsObject(obj) {
    }

    template<typename... Args>
    LSOBJECT GetMember(const std::string &memberName, Args... args) {
        LSOBJECT response;

        // Use a fold expression to convert each argument to a string
        const std::vector<std::string> arguments = {toString(args)...};

        const int argc = arguments.size();
        std::vector<char *> argv;

        for (auto &arg: arguments) {
            argv.push_back(const_cast<char *>(arg.c_str()));
        }

        this->lsObject.Type->GetMemberEx(
            this->lsObject.GetObjectData(),
            const_cast<char *>(memberName.c_str()),
            argc,
            argv.data(),
            response
        );

        return response;
    }

    template<typename... Args>
    void CallMethod(const std::string &methodName, Args... args) {
        LSOBJECT response;

        // Use a fold expression to convert each argument to a string
        const std::vector<std::string> arguments = {toString(args)...};

        const int argc = arguments.size();
        std::vector<char *> argv;

        for (auto &arg: arguments) {
            argv.push_back(const_cast<char *>(arg.c_str()));
        }

        this->lsObject.Type->GetMethodEx(
            this->lsObject.GetObjectData(),
            const_cast<char *>(methodName.c_str()),
            argc,
            argv.data());
    }

private:
    LSOBJECT lsObject;
};


#endif //LSOBJECT_H
