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

#include "d/dolzel_rel.h" // IWYU pragma: keep
#include "d/actor/d_a_ygcwp.h"
#include "d/actor/d_a_player.h"
#include "res/Object/Ygcwp.h"
#include "d/d_com_inf_game.h"

enum {
    EVENT_WARP_START,
    EVENT_WARP_APPEAR,
    EVENT_WARP_MAKE,
};

const u32 daYgcwp_c::M_brk_table[] = {
    dRes_INDEX_YGCWP_BRK_YGCWP00_COMMON_e,
    dRes_INDEX_YGCWP_BRK_YGCWP00_WARP_e,
};

const u32 daYgcwp_c::M_brk_mode_table[] = {
    J3DFrameCtrl::EMode_LOOP,
    J3DFrameCtrl::EMode_NONE,
};

const char daYgcwp_c::M_arcname[6] = "Ygcwp";

static void dummy() {
    OSReport("rtn_warp");
    OSReport("fg_warp0");
    OSReport("fg_warp1");
}

static char* M_act_table[] = {
    "warp_start",
    "warp_appear",
    "warp_make",
};

/* 00000078-00000098       .text solidHeapCB__9daYgcwp_cFP10fopAc_ac_c */
BOOL daYgcwp_c::solidHeapCB(fopAc_ac_c* i_this) {
    return ((daYgcwp_c*)i_this)->create_heap();
}

/* 00000098-0000023C       .text create_heap__9daYgcwp_cFv */
BOOL daYgcwp_c::create_heap() {
    J3DModelData* mdl_data;
    J3DAnmTevRegKey* brk_p;
    s32 i;
    BOOL ret = FALSE;
    mdl_data = static_cast<J3DModelData*>(dComIfG_getObjectRes(M_arcname, dRes_INDEX_YGCWP_BDL_YGCWP00_e));
    JUT_ASSERT(0xBE, mdl_data != NULL);

    if (mdl_data != NULL) {
        mpModel = mDoExt_J3DModel__create(mdl_data, 0x80000, 0x11000222);
        if (mpModel != NULL) {
            ret = TRUE;

            for (i = 0; i < (s32)ARRAY_SIZE(mBrkAnm); i++) {
                brk_p = static_cast<J3DAnmTevRegKey*>(dComIfG_getObjectRes(M_arcname, M_brk_table[i]));
                JUT_ASSERT(0xC9, brk_p != NULL);
                if (brk_p != NULL) {
                    if (!mBrkAnm[i].init(mdl_data, brk_p, TRUE, M_brk_mode_table[i])) {
                        ret = FALSE;
                        break;
                    }
                } else {
                    ret = FALSE;
                    break;
                }
            }
        }
    }

    return ret;
}

/* 0000023C-000003A0       .text _create__9daYgcwp_cFv */
cPhs_State daYgcwp_c::_create() {
    fopAcM_ct(this, daYgcwp_c);
    cPhs_State rt = dComIfG_resLoad(&mPhs, M_arcname);
    if (rt == cPhs_COMPLEATE_e) {
        rt = cPhs_ERROR_e;
        if (fopAcM_entrySolidHeap(this, solidHeapCB, 0)) {
            fopAcM_SetMtx(this, mpModel->getBaseTRMtx());
            init_mtx();
            m2D8 = fopAcM_GetParam(this) & 0x0F;
            if (m2D8 < 0 || m2D8 > 2)
                m2D8 = 0;
            mBrkAnm[1].setPlaySpeed(0.0f);
            if (check_ev()) {
                mBrkAnm[0].setPlaySpeed(0.0f);
            } else {
                mBrkAnm[0].setPlaySpeed(1.0f);
                mCurBrk = 0;
            }
            off_ev();
            fopAcM_setCullSizeBox(this, -160.0f, -10.0f, -160.0f, 160.0f, 50.0f, 160.0f);
            rt = cPhs_COMPLEATE_e;
        }
    }
    return rt;
}

/* 00000470-000004A0       .text _delete__9daYgcwp_cFv */
bool daYgcwp_c::_delete() {
    dComIfG_resDeleteDemo(&mPhs, M_arcname);
    return true;
}

/* 000004A0-000004D0       .text check_ev__9daYgcwp_cCFv */
BOOL daYgcwp_c::check_ev() const {
    return dComIfGs_isTmpBit(dSv_event_tmp_flag_c::UNK_0480);
}

/* 000004D0-00000500       .text off_ev__9daYgcwp_cCFv */
void daYgcwp_c::off_ev() const {
    dComIfGs_offTmpBit(dSv_event_tmp_flag_c::UNK_0480);
}

/* 00000500-00000588       .text init_mtx__9daYgcwp_cFv */
void daYgcwp_c::init_mtx() {
    mpModel->setBaseScale(scale);
    mDoMtx_stack_c::transS(current.pos);
    mDoMtx_stack_c::ZXYrotM(shape_angle);
    mpModel->setBaseTRMtx(mDoMtx_stack_c::get());
}

/* 00000588-000005F0       .text make_shine__9daYgcwp_cFv */
void daYgcwp_c::make_shine() {
    dComIfGp_particle_set(dPa_name::ID_AK_SN_GANONCASTLEWARP00, &current.pos, NULL, &scale);
}

/* 000005F0-00000654       .text set_timer__9daYgcwp_cFv */
void daYgcwp_c::set_timer() {
    int* a_intP = dComIfGp_evmng_getMyIntegerP(mStaffId, "Timer");
    mTimer = 0;
    if (a_intP != NULL)
        mTimer = *a_intP;
}

/* 00000654-00000834       .text _execute__9daYgcwp_cFv */
bool daYgcwp_c::_execute() {
    daPy_py_c* player = daPy_getPlayerActorClass();
    mStaffId = dComIfGp_evmng_getMyStaffId(M_arcname);
    if (mStaffId != -1) {
        int actIdx = dComIfGp_evmng_getMyActIdx(mStaffId, M_act_table, ARRAY_SIZE(M_act_table), FALSE, 0);
        if (dComIfGp_evmng_getIsAddvance(mStaffId)) {
            switch (actIdx) {
            case EVENT_WARP_START:
                make_shine();
                mBrkAnm[1].setFrame(0.0f);
                mBrkAnm[1].setPlaySpeed(1.0f);
                mCurBrk = 1;
                player->onNoResetFlg0(daPy_py_c::daPyFlg0_NO_DRAW);
                set_timer();
                fopAcM_seStartCurrent(this, JA_SE_LK_FG_ROOM_WARP_OUT, 0);
                break;
            case EVENT_WARP_APPEAR:
                set_timer();
                player->offNoResetFlg0(daPy_py_c::daPyFlg0_NO_DRAW);
                break;
            case EVENT_WARP_MAKE:
                mBrkAnm[0].setFrame(0.0f);
                mBrkAnm[0].setPlaySpeed(1.0f);
                mCurBrk = 0;
                break;
            }
        }

        switch (actIdx) {
        case EVENT_WARP_START:
        case EVENT_WARP_APPEAR:
            mTimer--;
            if (mTimer <= 0)
                dComIfGp_evmng_cutEnd(mStaffId);
            break;
        case EVENT_WARP_MAKE:
            break;
        }
    }
    
    for (s32 i = 0; i < (s32)ARRAY_SIZE(mBrkAnm); i++)
        mBrkAnm[i].play();

    return TRUE;
}

/* 00000834-000008DC       .text _draw__9daYgcwp_cFv */
bool daYgcwp_c::_draw() {
    g_env_light.settingTevStruct(TEV_TYPE_BG0, &current.pos, &tevStr);
    g_env_light.setLightTevColorType(mpModel, &tevStr);
    for (s32 i = 0; i < ARRAY_SIZE(mBrkAnm); i++) {
        if (mCurBrk == i) {
            mBrkAnm[i].entry(mpModel->getModelData());
            break;
        }
    }
    mDoExt_modelUpdateDL(mpModel);
    return TRUE;
}

namespace {
/* 000008DC-000008FC       .text Mthd_Create__23@unnamed@d_a_ygcwp_cpp@FPv */
cPhs_State Mthd_Create(void* i_this) {
    return ((daYgcwp_c*)i_this)->_create();
}

/* 000008FC-00000920       .text Mthd_Delete__23@unnamed@d_a_ygcwp_cpp@FPv */
BOOL Mthd_Delete(void* i_this) {
    return ((daYgcwp_c*)i_this)->_delete();
}

/* 00000920-00000944       .text Mthd_Execute__23@unnamed@d_a_ygcwp_cpp@FPv */
BOOL Mthd_Execute(void* i_this) {
    return ((daYgcwp_c*)i_this)->_execute();
}

/* 00000944-00000968       .text Mthd_Draw__23@unnamed@d_a_ygcwp_cpp@FPv */
BOOL Mthd_Draw(void* i_this) {
    return ((daYgcwp_c*)i_this)->_draw();
}

/* 00000968-00000970       .text Mthd_IsDelete__23@unnamed@d_a_ygcwp_cpp@FPv */
BOOL Mthd_IsDelete(void* i_this) {
    UNUSED(i_this);
    return TRUE;
}

static actor_method_class Ygcwp_Mthd_Table = {
    (process_method_func)Mthd_Create,
    (process_method_func)Mthd_Delete,
    (process_method_func)Mthd_Execute,
    (process_method_func)Mthd_IsDelete,
    (process_method_func)Mthd_Draw,
};
}; // namespace

actor_process_profile_definition g_profile_Ygcwp = {
    /* Layer ID     */ fpcLy_CURRENT_e,
    /* List ID      */ 0x0003,
    /* List Prio    */ fpcPi_CURRENT_e,
    /* Proc Name    */ fpcNm_Ygcwp_e,
    /* Proc SubMtd  */ &g_fpcLf_Method.base,
    /* Size         */ sizeof(daYgcwp_c),
    /* Size Other   */ 0,
    /* Parameters   */ 0,
    /* Leaf SubMtd  */ &g_fopAc_Method.base,
    /* Draw Prio    */ fpcDwPi_Ygcwp_e,
    /* Actor SubMtd */ &Ygcwp_Mthd_Table,
    /* Status       */ fopAcStts_CULL_e | fopAcStts_UNK40000_e,
    /* Group        */ fopAc_ACTOR_e,
    /* Cull Type    */ fopAc_CULLBOX_CUSTOM_e,
};
