/**
 * d_a_tag_river_back.cpp
 * Tag - River Back
 */

#include "d/dolzel_rel.h" // IWYU pragma: keep

#include "d/actor/d_a_tag_river_back.h"
#include "f_pc/f_pc_name.h"

static DUSK_CONSTEXPR char DUSK_CONST* l_arcName = "RvBack";

static DUSK_CONSTEXPR char DUSK_CONST* l_evName = "RIVER_BACK";

static DUSK_CONSTEXPR char DUSK_CONST* l_staffName = "rvback";

int daTagRiverBack_c::Create() {
    mMapToolID = -1;
    eventInfo.setArchiveName(l_arcName);
    mEventID = (s16)dComIfGp_getEventManager().getEventIdx(this, l_evName, -1);
    // "River flow reversal tag: evid<%d>tool<%d>"
    OS_REPORT("川流れ戻しタグ：evid<%d>tool<%d>\n", mEventID, mMapToolID);
    return 1;
}

int daTagRiverBack_c::create() {
    fopAcM_ct(this, daTagRiverBack_c);
    int phase = dComIfG_resLoad(&mPhase, l_arcName);
    if (phase == cPhs_COMPLEATE_e) {
        if (!Create()) {
            return cPhs_ERROR_e;
        }
    }
    return phase;
}

int daTagRiverBack_c::execute() {
    if (getSwbit() != 0xFF && fopAcM_isSwitch(this, getSwbit()) != 0) {
        return 1;
    }

    if (getSwbit2() != 0xFF && fopAcM_isSwitch(this, getSwbit2()) == 0) {
        return 1;
    }

    event_proc_call();

    return 1;
}

void daTagRiverBack_c::event_proc_call() {
    typedef void (daTagRiverBack_c::*actionFunc)();
    static actionFunc l_func[] = {&daTagRiverBack_c::actionWait,
                                  &daTagRiverBack_c::actionOrderEvent,
                                  &daTagRiverBack_c::actionEvent, &daTagRiverBack_c::actionDead};

    (this->*l_func[mAction])();
}

void daTagRiverBack_c::actionWait() {
    u32 result = dComIfGp_checkPlayerStatus0(0, 0x100000);
    if (result != 0) {
        mCount = 0;
        setAction(ACTION_ORDER_EVENT_e);
        fopAcM_orderOtherEventId(this, mEventID, mMapToolID, 0xFFFF, 0, 1);
        eventInfo.onCondition(2);
    }
}

void daTagRiverBack_c::actionOrderEvent() {
    daPy_py_c* player = daPy_getPlayerActorClass();
    if (eventInfo.checkCommandDemoAccrpt()) {
        setAction(ACTION_EVENT_e);
        mStaffID = dComIfGp_evmng_getMyStaffId(l_staffName, NULL, 0);
        player->onDemoStreamAccept();
        demoProc();
    } else {
        fopAcM_orderOtherEventId(this, mEventID, mMapToolID, 0xFFFF, 0, 1);
        eventInfo.onCondition(2);
    }
}

void daTagRiverBack_c::actionEvent() {
    daPy_py_c* player = daPy_getPlayerActorClass();
    S16_ADD(mCount, 1);
    if (dComIfGp_evmng_endCheck(mEventID)) {
        setAction(ACTION_DEAD_e);
        dComIfGp_event_reset();
    } else {
        demoProc();
        if (mCount == 30) {
            player->voiceStart(Z2SE_WL_V_FALL_TO_RESTART);
        }
    }
}

void daTagRiverBack_c::actionDead() {}

int daTagRiverBack_c::demoProc() {
    static DUSK_CONSTEXPR char DUSK_CONST* action_table[] = {"WAIT", "SCENE_CHG"};

    daPy_py_c* player = daPy_getPlayerActorClass();
    int act_id = dComIfGp_evmng_getMyActIdx(mStaffID, action_table, ARRAY_SIZEU(action_table), 0, 0);

    int* timer_p;
    if (dComIfGp_evmng_getIsAddvance(mStaffID)) {
        switch (act_id) {
        case WAIT_e:
            timer_p = dComIfGp_evmng_getMyIntegerP(mStaffID, "Timer");
            if (timer_p == NULL) {
                mTimer = 1;
            } else {
                mTimer = *timer_p;
            }
            break;
        case SCENE_CHG_e:
            mDoAud_seStart(Z2SE_FORCE_BACK, 0, 0, 0);
        }
    }
    switch (act_id) {
    case WAIT_e:
        if (cLib_calcTimer(&mTimer) == 0) {
            dComIfGp_evmng_cutEnd(mStaffID);
        }
        break;
    case SCENE_CHG_e:
        dStage_changeScene(getNextSceneNo(), 0.0f, 0, fopAcM_GetRoomNo(this),
                           0, -1);
        break;
    default:
        dComIfGp_evmng_cutEnd(mStaffID);
    }

    return 0;
}

int daTagRiverBack_c::_delete() {
    dComIfG_resDelete(&mPhase, l_arcName);
    return 1;
}

static int daTagRiverBack_Execute(daTagRiverBack_c* i_this) {
    return i_this->execute();
}

static int daTagRiverBack_Delete(daTagRiverBack_c* i_this) {
    fopAcM_RegisterDeleteID(i_this, "RiverBack");
    return i_this->_delete();
}

static int daTagRiverBack_Create(daTagRiverBack_c* i_this) {
    fopAcM_RegisterCreateID(i_this, "RiverBack");
    return i_this->create();
}

static DUSK_CONST actor_method_class l_daTagRiverBack_Method = {(process_method_func)daTagRiverBack_Create,
                                                     (process_method_func)daTagRiverBack_Delete,
                                                     (process_method_func)daTagRiverBack_Execute};

DUSK_PROFILE actor_process_profile_definition DUSK_CONST g_profile_Tag_RiverBack = {
    /* Layer ID     */ fpcLy_CURRENT_e,
    /* List ID      */ 7,
    /* List Prio    */ fpcPi_CURRENT_e,
    /* Proc Name    */ fpcNm_Tag_RiverBack_e,
    /* Proc SubMtd  */ &g_fpcLf_Method.base,
    /* Size         */ sizeof(daTagRiverBack_c),
    /* Size Other   */ 0,
    /* Parameters   */ 0,
    /* Leaf SubMtd  */ &g_fopAc_Method.base,
    /* Draw Prio    */ fpcDwPi_Tag_RiverBack_e,
    /* Actor SubMtd */ &l_daTagRiverBack_Method,
    /* Status       */ fopAcStts_UNK_0x40000_e,
    /* Group        */ fopAc_ACTOR_e,
    /* Cull Type    */ fopAc_CULLBOX_CUSTOM_e,
};
