#include "MrBotApiType.h"

unique_ptr<MrBotApiType> MrBotApiType::pMrBotApi = make_unique<MrBotApiType>();

bool MrBotApiType::GetMember(LSOBJECTDATA ObjectData, PLSTYPEMEMBER pMember, int argc, char *argv[], LSOBJECT &Dest)
{
    if (ObjectData.Ptr == nullptr)
    {
        return false;
    }

    const auto theMember = memberMap.find(static_cast<MrBotApiTypeMembers>(pMember->ID));
    if (theMember == memberMap.end())
    {
        return false;
    }

    return theMember->second(argc, argv, Dest);
}

bool MrBotApiType::GetMethod(LSOBJECTDATA &ObjectData, PLSTYPEMEMBER pMethod, int argc, char *argv[])
{
    if (ObjectData.Ptr == nullptr)
    {
        return false;
    }

    const auto theMethod = methodMap.find(static_cast<MrBotApiTypeMethods>(pMethod->ID));
    if (theMethod == methodMap.end())
    {
        return false;
    }

    return theMethod->second(argc, argv);
}

bool MrBotApiType::ToText(LSOBJECTDATA ObjectData, char *buf, unsigned int buflen)
{
    return false;
}

// Members
bool MrBotApiType::Version(const std::vector<std::string> &args, LSOBJECT &dest)
{
    dest.ConstCharPtr = "0.1.0";
    dest.Type = pStringType;
    return true;
}

// Methods
bool MrBotApiType::ClickActorByName(const std::vector<std::string> &args)
{
    if (args.size() != 1)
    {
        logw << "MrBotApiType::ClickActorByName: Expected 1 argument, got " << args.size() << std::endl;
        return false;
    }

    const std::string &name = args[0];
    log << "MrBotApiType::ClickActorByName " << name << std::endl;
    const auto actor = ExtensionTLOs::Actor().GetActorByName(name);
    if (!actor.has_value())
    {
        logw << "MrBotApiType::ClickActorByName: Actor not found: " << name << std::endl;
        return false;
    }
    // actor->DoubleClick();
    return true;
}

bool MrBotApiType::ClickActorById(const std::vector<std::string> &args)
{
    if (args.size() != 1)
    {
        logw << "MrBotApiType::ClickActorById: Expected 1 argument, got " << args.size() << std::endl;
        return false;
    }

    log << "MrBotApiType::ClickActorById " << args[0] << std::endl;
    const auto actor = ExtensionTLOs::Actor().GetActorById(std::stoul(args[0]));

    if (!actor.has_value())
    {
        logw << "MrBotApiType::ClickActorById: Actor not found: " << args[0] << std::endl;
        return false;
    }

    log << "MrBotApiType::ClickActorById: Actor found: " << actor->Name() << std::endl;

    actor->DoubleClick();
}
