//
// Generated by dtk
// Translation Unit: control.cpp
//

#include "JSystem/JSystem.h" // IWYU pragma: keep

#include "JSystem/JMessage/control.h"
#include "JSystem/JMessage/resource.h"

/* 8029E930-8029E970       .text __ct__Q28JMessage8TControlFv */
JMessage::TControl::TControl()
    : mResourceContainer(NULL)
    , mResource(NULL)
    , mBaseProcSeq(NULL)
    , mBaseProcRender(NULL)
    , mGroupID(0)
    , mMessageIndex(0)
    , mMessageEntry(NULL)
    , mMessageDataStart(NULL)
    , mMessageDataCurrent(NULL)
    , mCurrentText(NULL)
{
}

/* 8029E970-8029E9B8       .text __dt__Q28JMessage8TControlFv */
JMessage::TControl::~TControl() {}

/* 8029E9B8-8029EA34       .text getResource_groupID__Q28JMessage8TControlCFUs */
JMessage::TResource* JMessage::TControl::getResource_groupID(u16 groupID) const {
    if (isResourceCached_groupID(groupID))
        return mResource;

    if (mResourceContainer == NULL)
        return NULL;

    mResource = mResourceContainer->Get_groupID(groupID);
    return mResource;
}

/* 8029EA34-8029EAC8       .text getMessageData__Q28JMessage8TControlCFUsUs */
const char* JMessage::TControl::getMessageData(u16 groupID, u16 messageIndex) const {
    void* messageEntry = getMessageEntry(groupID, messageIndex);
    if (messageEntry == NULL)
        return NULL;
    u32 offs = *(u32*)messageEntry;
    return mResource->mMessageData + offs;
}

/* 8029EAC8-8029EB1C       .text reset__Q28JMessage8TControlFv */
void JMessage::TControl::reset() {
    reset_();

    if (mBaseProcSeq) {
        mBaseProcSeq->reset();
    }

    if (mBaseProcRender) {
        mBaseProcRender->reset();
    }
}

/* 8029EB1C-8029EC00       .text update__Q28JMessage8TControlFv */
bool JMessage::TControl::update() {
    if (!isReady_update_()) {
        return false;
    }

    if (mCurrentText == NULL) {
        mCurrentText = mMessageDataStart;
        mBaseProcSeq->setBegin(mMessageEntry, mMessageDataStart);
    }

    mCurrentText = mBaseProcSeq->process(NULL);

    if (!mCurrentText) {
        mMessageDataStart = NULL;
        return false;
    }

    return true;
}

/* 8029EC00-8029ECCC       .text render__Q28JMessage8TControlFv */
void JMessage::TControl::render() {
    if (!isReady_render_()) {
        return;
    }

    mBaseProcRender->setBegin(mMessageEntry, mMessageDataCurrent);
    mBaseProcRender->mStack = mRenderStack;
    mBaseProcRender->process(mCurrentText);
}

/* 8029ECCC-8029ECD4       .text do_word__Q28JMessage8TControlFUl */
const char* JMessage::TControl::do_word(u32) {
    return NULL;
}

/* 8029ECD4-8029ED88       .text setMessageCode_flush___Q28JMessage8TControlFv */
bool JMessage::TControl::setMessageCode_flush_() {
    reset_();
    mMessageEntry = getMessageEntry(mGroupID, mMessageIndex);
    if (mMessageEntry == NULL)
        return false;

    u32 offs = *(u32*)mMessageEntry;
    mMessageDataStart = mResource->mMessageData + offs;
    mMessageDataCurrent = mMessageDataStart;
    return true;
}

/* 8029ED88-8029EDA4       .text reset___Q28JMessage8TControlFv */
void JMessage::TControl::reset_() {
    mMessageEntry = NULL;
    mMessageDataStart = NULL;
    mMessageDataCurrent = NULL;
    mCurrentText = NULL;
    mRenderStack.clear();
}
