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

#include "f_pc/f_pc_create_req.h"
#include "SSystem/SComponent/c_malloc.h"
#include "f_pc/f_pc_base.h"
#include "f_pc/f_pc_create_iter.h"
#include "f_pc/f_pc_deletor.h"
#include "f_pc/f_pc_executor.h"
#include "f_pc/f_pc_layer.h"
#include "f_pc/f_pc_layer_iter.h"

/* 8003CD0C-8003CD28       .text fpcCtRq_isCreatingByID__FP10create_tagPUi */
BOOL fpcCtRq_isCreatingByID(create_tag* i_createTag, fpc_ProcID* i_procID) {
    create_request* pReq = static_cast<create_request*>(i_createTag->base.mpTagData);
    if (pReq->mBsPcId == *i_procID) {
        return TRUE;
    } else {
        return FALSE;
    }
}

/* 8003CD28-8003CD60       .text fpcCtRq_IsCreatingByID__FUi */
BOOL fpcCtRq_IsCreatingByID(fpc_ProcID i_id) {
    return fpcCtIt_Judge((fpcLyIt_JudgeFunc)fpcCtRq_isCreatingByID, &i_id) != NULL ? TRUE : FALSE;
}

/* 8003CD60-8003CD9C       .text fpcCtRq_CreateQTo__FP14create_request */
void fpcCtRq_CreateQTo(create_request* i_request) {
    fpcCtTg_CreateQTo(&i_request->base);
    fpcLy_CreatedMesg(i_request->mpLayer);
    fpcLy_CancelQTo(&i_request->mMtdTg);
}

/* 8003CD9C-8003CDE0       .text fpcCtRq_ToCreateQ__FP14create_request */
void fpcCtRq_ToCreateQ(create_request* i_request) {
    fpcLy_CreatingMesg(i_request->mpLayer);
    fpcLy_ToCancelQ(i_request->mpLayer, &i_request->mMtdTg);
    fpcCtTg_ToCreateQ(&i_request->base);
}

/* 8003CDE0-8003CE54       .text fpcCtRq_Delete__FP14create_request */
BOOL fpcCtRq_Delete(create_request* i_request) {
    fpcCtRq_CreateQTo(i_request);
    if (i_request->mpCtRqMtd != NULL && fpcMtd_Method(i_request->mpCtRqMtd->mpDelete, i_request) == FALSE) {
        return FALSE;
    } else {
        if (i_request->mpRes) {
            i_request->mpRes->mpCtRq = NULL;
        }
        cMl::free(i_request);
        return TRUE;
    }
}

/* 8003CE54-8003CEEC       .text fpcCtRq_Cancel__FP14create_request */
BOOL fpcCtRq_Cancel(create_request* i_request) {
    base_process_class* pProc;
    if (i_request != NULL && !i_request->mbIsCancelling) {
        i_request->mbIsCancelling = TRUE;
        pProc = i_request->mpRes;

        if (pProc != NULL && !fpcDt_Delete(pProc))
            return FALSE;

        if (i_request->mpCtRqMtd != NULL && !fpcMtd_Method(i_request->mpCtRqMtd->mpCancel, i_request))
            return FALSE;

        return fpcCtRq_Delete(i_request);
    } else {
        return TRUE;
    }
}

/* 8003CEEC-8003CF08       .text fpcCtRq_IsDoing__FP14create_request */
BOOL fpcCtRq_IsDoing(create_request* i_request) {
    if (i_request != NULL)
        return i_request->mbIsCreating;
    else
        return FALSE;
}

/* 8003CF08-8003CFC4       .text fpcCtRq_Do__FP14create_request */
BOOL fpcCtRq_Do(create_request* i_request) {
    cPhs_State phase = cPhs_COMPLEATE_e;

    if (i_request->mpCtRqMtd != NULL) {
        cPhs__Handler pHandler = i_request->mpCtRqMtd->mpHandler;
        if (pHandler != NULL) {
            i_request->mbIsCreating = TRUE;
            phase = pHandler(i_request);
            i_request->mbIsCreating = FALSE;
        }
    }

    switch (phase) {
    case cPhs_COMPLEATE_e: {
        BOOL success = fpcEx_ToExecuteQ(i_request->mpRes);
        if (success == FALSE)
            return fpcCtRq_Cancel(i_request);
        else
            return fpcCtRq_Delete(i_request);
    }
    case cPhs_STOP_e:
    case cPhs_ERROR_e:
        return fpcCtRq_Cancel(i_request);
    }

    return TRUE;
}

/* 8003CFC4-8003CFF0       .text fpcCtRq_Handler__Fv */
BOOL fpcCtRq_Handler() {
    return fpcCtIt_Method((fpcCtIt_MethodFunc)fpcCtRq_Do, NULL);
}

/* 8003CFF0-8003D078       .text fpcCtRq_Create__FP11layer_classUlP27create_request_method_class */
create_request* fpcCtRq_Create(layer_class* i_layer, u32 i_size, create_request_method_class* i_CtRqMethods) {
    create_request* pReq = (create_request*)cMl::memalignB(-4, i_size);

    if (pReq != NULL) {
        fpcCtTg_Init(&pReq->base, pReq);
        fpcMtdTg_Init(&pReq->mMtdTg, (process_method_tag_func)fpcCtRq_Cancel, pReq);
        pReq->mpLayer = i_layer;
        pReq->mpCtRqMtd = i_CtRqMethods;
        pReq->mBsPcId = fpcBs_MakeOfId();
        pReq->mpRes = NULL;
        pReq->mbIsCancelling = FALSE;
        fpcCtRq_ToCreateQ(pReq);
    }

    return pReq;
}
