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

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

#include "JSystem/JAudio/JASDvdThread.h"
#include "JSystem/JAudio/JASCalc.h"
#include "JSystem/JAudio/JASSystemHeap.h"
#include "JSystem/JKernel/JKRSolidHeap.h"
#include "JSystem/JUtility/JUTAssert.h"

/* 8027B500-8027B584       .text __ct__Q213JASTaskThread10TCallStackFUl */
JASTaskThread::TCallStack::TCallStack(u32 param_1) {
    field_0x0 = NULL;
    argspace = new (JKRHeap::sSystemHeap, -4) u8[param_1];
    JUT_ASSERT(39, argspace);
}

/* 8027B584-8027B5D8       .text __dt__Q213JASTaskThread10TCallStackFv */
JASTaskThread::TCallStack::~TCallStack() {
    delete[] argspace;
}

/* 8027B5D8-8027B66C       .text sendCmdMsg__13JASTaskThreadFPFPv_lPvUl */
BOOL JASTaskThread::sendCmdMsg(s32 (*param_1)(void*), void* param_2, u32 param_3) {
    TCallStack* stack = new(JKRGetSystemHeap(), -4) TCallStack(param_3);
    if (!stack) {
        return false;
    }
    JASystem::Calc::bcopy(param_2, stack->argspace, param_3);
    stack->field_0x0 = param_1;
    return sendMessageBlock(stack);
}

/* 8027B66C-8027B6D4       .text run__13JASTaskThreadFv */
void* JASTaskThread::run() {
    OSMessage msg;
    while (true) {
        msg = waitMessageBlock();
        TCallStack* callStack = (TCallStack*)msg;
        if (mPaused) {
            OSSleepThread(&mQ);
        }
        if (!callStack) {
            continue;
        }
        callStack->field_0x0(callStack->argspace);
        delete callStack;
    }
    return NULL;
}

/* 8027B6D4-8027B72C       .text pause__13JASTaskThreadFb */
void JASTaskThread::pause(bool param_1) {
    if (param_1) {
        mPaused = 1;
    } else {
        if (mPaused) {
            OSWakeupThread(&mQ);
        }
        mPaused = 0;
    }
}

JASTaskThread* JASystem::Dvd::sThread;

/* 8027B72C-8027B7E8       .text createThread__Q28JASystem3DvdFiiUl */
bool JASystem::Dvd::createThread(int param_1, int param_2, u32 param_3) {
    sThread = new (JASDram, 0) JASTaskThread(param_3, param_2, param_1);
    JUT_ASSERT(136, sThread);
    return true;
}

/* 8027B7E8-8027B84C       .text resumeThread__Q28JASystem3DvdFv */
void JASystem::Dvd::resumeThread() {
    JUT_ASSERT(143, sThread);
    OSResumeThread(sThread->getThreadRecord());
}

/* 8027B84C-8027B8D4       .text sendCmdMsg__Q28JASystem3DvdFPFPv_lPvUl */
BOOL JASystem::Dvd::sendCmdMsg(s32 (*param_1)(void*), void* param_2, u32 param_3) {
    JUT_ASSERT(152, sThread);
    return sThread->sendCmdMsg(param_1, param_2, param_3);
}

struct dvd_msg {
    u32 field_0x00;
    u32 * field_0x04;
    void(* field_0x08)(u32);
};

/* 8027B8D4-8027B914       .text checkPassDvdT__Q28JASystem3DvdFUlPUlPFUl_v */
int JASystem::Dvd::checkPassDvdT(u32 p1, u32* p2, void (*p3)(u32)) {
    dvd_msg msg;
    msg.field_0x00 = p1;
    msg.field_0x04 = p2;
    msg.field_0x08 = p3;
    sendCmdMsg(dvdThreadCheckBack, &msg, sizeof(msg));
    return 0;
}

/* 8027B914-8027B960       .text checkFile__Q28JASystem3DvdFPc */
u32 JASystem::Dvd::checkFile(char* name) {
    DVDFileInfo info;
    if (!DVDOpen(name, &info)) {
        return 0;
    }
    u32 size = info.length;
    DVDClose(&info);
    return size;
}

/* 8027B960-8027B9C4       .text unpauseDvdT__Q28JASystem3DvdFv */
void JASystem::Dvd::unpauseDvdT() {
    JUT_ASSERT(231, sThread);
    sThread->pause(false);
}

/* 8027B9C4-8027BA10       .text dvdThreadCheckBack__Q28JASystem3DvdFPv */
s32 JASystem::Dvd::dvdThreadCheckBack(void* unk) {
    const dvd_msg* msg = (const dvd_msg*)unk;
    if (msg->field_0x04 != 0x00)
        msg->field_0x04[0] = msg->field_0x00;
    if (msg->field_0x08 != NULL)
        msg->field_0x08(msg->field_0x00);
    return 0;
}

/* 8027BA10-8027BA70       .text __dt__13JASTaskThreadFv */
JASTaskThread::~JASTaskThread() {}
