/*
 * File: z_bg_mori_bigst.c
 * Overlay: ovl_Bg_Mori_Bigst
 * Description: Forest Temple falling platform and Stalfos fight
 */

#include "z_bg_mori_bigst.h"

#define FLAGS 0x00000010

#define THIS ((BgMoriBigst*)thisx)

void BgMoriBigst_Init(Actor* thisx, GlobalContext* globalCtx);
void BgMoriBigst_Destroy(Actor* thisx, GlobalContext* globalCtx);
void BgMoriBigst_Update(Actor* thisx, GlobalContext* globalCtx);
void BgMoriBigst_Draw(Actor* thisx, GlobalContext* globalCtx);

void BgMoriBigst_SetupWaitForMoriTex(BgMoriBigst* this, GlobalContext* globalCtx);
void BgMoriBigst_WaitForMoriTex(BgMoriBigst* this, GlobalContext* globalCtx);
void BgMoriBigst_SetupNoop(BgMoriBigst* this, GlobalContext* globalCtx);
void BgMoriBigst_SetupStalfosFight(BgMoriBigst* this, GlobalContext* globalCtx);
void BgMoriBigst_StalfosFight(BgMoriBigst* this, GlobalContext* globalCtx);
void BgMoriBigst_SetupFall(BgMoriBigst* this, GlobalContext* globalCtx);
void BgMoriBigst_Fall(BgMoriBigst* this, GlobalContext* globalCtx);
void BgMoriBigst_SetupLanding(BgMoriBigst* this, GlobalContext* globalCtx);
void BgMoriBigst_Landing(BgMoriBigst* this, GlobalContext* globalCtx);
void BgMoriBigst_SetupStalfosPairFight(BgMoriBigst* this, GlobalContext* globalCtx);
void BgMoriBigst_StalfosPairFight(BgMoriBigst* this, GlobalContext* globalCtx);
void BgMoriBigst_SetupDone(BgMoriBigst* this, GlobalContext* globalCtx);

extern ColHeader D_0600221C;
extern Gfx D_06001E50[];

const ActorInit Bg_Mori_Bigst_InitVars = {
    ACTOR_BG_MORI_BIGST,
    ACTORTYPE_BG,
    FLAGS,
    OBJECT_MORI_OBJECTS,
    sizeof(BgMoriBigst),
    (ActorFunc)BgMoriBigst_Init,
    (ActorFunc)BgMoriBigst_Destroy,
    (ActorFunc)BgMoriBigst_Update,
    NULL,
};

static InitChainEntry sInitChain[] = {
    ICHAIN_F32(uncullZoneForward, 3000, ICHAIN_CONTINUE),      ICHAIN_F32(uncullZoneScale, 3000, ICHAIN_CONTINUE),
    ICHAIN_F32(uncullZoneDownward, 3000, ICHAIN_CONTINUE),     ICHAIN_F32_DIV1000(gravity, -500, ICHAIN_CONTINUE),
    ICHAIN_F32_DIV1000(minVelocityY, -12000, ICHAIN_CONTINUE), ICHAIN_VEC3F_DIV1000(scale, 1000, ICHAIN_STOP),
};

void BgMoriBigst_SetupAction(BgMoriBigst* this, BgMoriBigstActionFunc actionFunc) {
    this->actionFunc = actionFunc;
}

void BgMoriBigst_InitDynapoly(BgMoriBigst* this, GlobalContext* globalCtx, ColHeader* collision, s32 moveFlag) {
    s32 pad;
    ColHeader* colHeader = NULL;
    s32 pad2;

    DynaPolyInfo_SetActorMove(&this->dyna, moveFlag);
    DynaPolyInfo_Alloc(collision, &colHeader);
    this->dyna.dynaPolyId =
        DynaPolyInfo_RegisterActor(globalCtx, &globalCtx->colCtx.dyna, &this->dyna.actor, colHeader);

    if (this->dyna.dynaPolyId == 0x32) {
        // Warning : move BG login failed
        osSyncPrintf("Warning : move BG 登録失敗(%s %d)(name %d)(arg_data 0x%04x)\n", "../z_bg_mori_bigst.c", 190,
                     this->dyna.actor.id, this->dyna.actor.params);
    }
}

void BgMoriBigst_Init(Actor* thisx, GlobalContext* globalCtx) {
    s32 pad;
    BgMoriBigst* this = THIS;

    // mori (bigST.keyceiling)
    osSyncPrintf("mori (bigST.鍵型天井)(arg : %04x)(sw %d)(noE %d)(roomC %d)(playerPosY %f)\n", this->dyna.actor.params,
                 Flags_GetSwitch(globalCtx, (this->dyna.actor.params >> 8) & 0x3F),
                 Flags_GetTempClear(globalCtx, this->dyna.actor.room), Flags_GetClear(globalCtx, this->dyna.actor.room),
                 PLAYER->actor.posRot.pos.y);
    BgMoriBigst_InitDynapoly(this, globalCtx, &D_0600221C, DPM_UNK);
    Actor_ProcessInitChain(&this->dyna.actor, sInitChain);
    this->moriTexObjIndex = Object_GetIndex(&globalCtx->objectCtx, OBJECT_MORI_TEX);
    if (this->moriTexObjIndex < 0) {
        // 【Big Stalfos key ceiling】 bank danger!
        osSyncPrintf("【ビッグスタルフォス鍵型天井】 バンク危険！\n");
        osSyncPrintf("%s %d\n", "../z_bg_mori_bigst.c", 234);
        Actor_Kill(&this->dyna.actor);
        return;
    }
    if (Flags_GetSwitch(globalCtx, (this->dyna.actor.params >> 8) & 0x3F)) {
        this->dyna.actor.posRot.pos.y = this->dyna.actor.initPosRot.pos.y;
    } else {
        this->dyna.actor.posRot.pos.y = this->dyna.actor.initPosRot.pos.y + 270.0f;
    }
    Actor_SetHeight(&this->dyna.actor, 50.0f);
    BgMoriBigst_SetupWaitForMoriTex(this, globalCtx);
}

void BgMoriBigst_Destroy(Actor* thisx, GlobalContext* globalCtx) {
    s32 pad;
    BgMoriBigst* this = THIS;

    DynaPolyInfo_Free(globalCtx, &globalCtx->colCtx.dyna, this->dyna.dynaPolyId);
}

void BgMoriBigst_SetupWaitForMoriTex(BgMoriBigst* this, GlobalContext* globalCtx) {
    BgMoriBigst_SetupAction(this, BgMoriBigst_WaitForMoriTex);
}

void BgMoriBigst_WaitForMoriTex(BgMoriBigst* this, GlobalContext* globalCtx) {
    Actor* thisx = &this->dyna.actor;

    if (Object_IsLoaded(&globalCtx->objectCtx, this->moriTexObjIndex)) {
        thisx->draw = BgMoriBigst_Draw;
        if (Flags_GetClear(globalCtx, thisx->room) && (PLAYER->actor.posRot.pos.y > 700.0f)) {
            if (Flags_GetSwitch(globalCtx, (thisx->params >> 8) & 0x3F)) {
                BgMoriBigst_SetupDone(this, globalCtx);
            } else {
                BgMoriBigst_SetupStalfosFight(this, globalCtx);
            }
        } else {
            BgMoriBigst_SetupNoop(this, globalCtx);
        }
    }
}

void BgMoriBigst_SetupNoop(BgMoriBigst* this, GlobalContext* globalCtx) {
    BgMoriBigst_SetupAction(this, NULL);
}

void BgMoriBigst_SetupStalfosFight(BgMoriBigst* this, GlobalContext* globalCtx) {
    Actor* stalfos;

    BgMoriBigst_SetupAction(this, BgMoriBigst_StalfosFight);
    Flags_UnsetClear(globalCtx, this->dyna.actor.room);
    stalfos = Actor_SpawnAsChild(&globalCtx->actorCtx, &this->dyna.actor, globalCtx, ACTOR_EN_TEST, 209.0f, 827.0f,
                                 -3320.0f, 0, 0, 0, 1);
    if (stalfos != NULL) {
        this->dyna.actor.child = NULL;
        this->dyna.actor.initPosRot.rot.z++;
    } else {
        // Second Stalfos failure
        osSyncPrintf("Warning : 第２スタルフォス発生失敗\n");
    }
    Flags_SetClear(globalCtx, this->dyna.actor.room);
}

void BgMoriBigst_StalfosFight(BgMoriBigst* this, GlobalContext* globalCtx) {
    Player* player = PLAYER;

    if ((this->dyna.actor.initPosRot.rot.z == 0) &&
        ((this->dyna.actor.initPosRot.pos.y - 5.0f) <= PLAYER->actor.posRot.pos.y)) {
        BgMoriBigst_SetupFall(this, globalCtx);
        func_800800F8(globalCtx, 0xC94, 0x48, &this->dyna.actor, 0);
    }
}

void BgMoriBigst_SetupFall(BgMoriBigst* this, GlobalContext* globalCtx) {
    BgMoriBigst_SetupAction(this, BgMoriBigst_Fall);
}

void BgMoriBigst_Fall(BgMoriBigst* this, GlobalContext* globalCtx) {
    Actor_MoveForward(&this->dyna.actor);
    if (this->dyna.actor.posRot.pos.y <= this->dyna.actor.initPosRot.pos.y) {
        this->dyna.actor.posRot.pos.y = this->dyna.actor.initPosRot.pos.y;
        BgMoriBigst_SetupLanding(this, globalCtx);
        Audio_PlayActorSound2(&this->dyna.actor, NA_SE_EV_STONE_BOUND);
        func_800800F8(globalCtx, 0x3FC, 8, &this->dyna.actor, 0);
        func_8002DF38(globalCtx, NULL, 0x3C);
    }
}

void BgMoriBigst_SetupLanding(BgMoriBigst* this, GlobalContext* globalCtx) {
    s32 pad;
    s32 quake;

    BgMoriBigst_SetupAction(this, BgMoriBigst_Landing);
    this->waitTimer = 18;
    quake = Quake_Add(ACTIVE_CAM, 3);
    Quake_SetSpeed(quake, 25000);
    Quake_SetQuakeValues(quake, 5, 0, 0, 0);
    Quake_SetCountdown(quake, 16);
}

void BgMoriBigst_Landing(BgMoriBigst* this, GlobalContext* globalCtx) {
    if (this->waitTimer <= 0) {
        BgMoriBigst_SetupStalfosPairFight(this, globalCtx);
    }
}

void BgMoriBigst_SetupStalfosPairFight(BgMoriBigst* this, GlobalContext* globalCtx) {
    Actor* stalfos1;
    Actor* stalfos2;

    BgMoriBigst_SetupAction(this, BgMoriBigst_StalfosPairFight);
    Flags_UnsetClear(globalCtx, this->dyna.actor.room);
    stalfos1 = Actor_SpawnAsChild(&globalCtx->actorCtx, &this->dyna.actor, globalCtx, ACTOR_EN_TEST, 70.0f, 827.0f,
                                  -3383.0f, 0, 0, 0, 5);
    if (stalfos1 != NULL) {
        this->dyna.actor.child = NULL;
        this->dyna.actor.initPosRot.rot.z++;
    } else {
        // Warning: 3-1 Stalfos failure
        osSyncPrintf("Warning : 第３-1スタルフォス発生失敗\n");
    }
    stalfos2 = Actor_SpawnAsChild(&globalCtx->actorCtx, &this->dyna.actor, globalCtx, ACTOR_EN_TEST, 170.0f, 827.0f,
                                  -3260.0f, 0, 0, 0, 5);
    if (stalfos2 != NULL) {
        this->dyna.actor.child = NULL;
        this->dyna.actor.initPosRot.rot.z++;
    } else {
        // Warning: 3-2 Stalfos failure
        osSyncPrintf("Warning : 第３-2スタルフォス発生失敗\n");
    }
    Flags_SetClear(globalCtx, this->dyna.actor.room);
}

void BgMoriBigst_StalfosPairFight(BgMoriBigst* this, GlobalContext* globalCtx) {
    if ((this->dyna.actor.initPosRot.rot.z == 0) && !Player_InCsMode(globalCtx)) {
        Flags_SetSwitch(globalCtx, (this->dyna.actor.params >> 8) & 0x3F);
        BgMoriBigst_SetupDone(this, globalCtx);
    }
}

void BgMoriBigst_SetupDone(BgMoriBigst* this, GlobalContext* globalCtx) {
    BgMoriBigst_SetupAction(this, NULL);
}

void BgMoriBigst_Update(Actor* thisx, GlobalContext* globalCtx) {
    s32 pad;
    BgMoriBigst* this = THIS;

    Actor_SetHeight(&this->dyna.actor, 50.0f);
    if (this->waitTimer > 0) {
        this->waitTimer--;
    }
    if (func_80043590(&this->dyna)) {
        func_80074CE8(globalCtx, 6);
    }
    if (this->actionFunc != NULL) {
        this->actionFunc(this, globalCtx);
    }
}

void BgMoriBigst_Draw(Actor* thisx, GlobalContext* globalCtx) {
    s32 pad;
    BgMoriBigst* this = THIS;

    OPEN_DISPS(globalCtx->state.gfxCtx, "../z_bg_mori_bigst.c", 541);
    func_80093D18(globalCtx->state.gfxCtx);

    gSPSegment(oGfxCtx->polyOpa.p++, 0x08, globalCtx->objectCtx.status[this->moriTexObjIndex].segment);

    gSPMatrix(oGfxCtx->polyOpa.p++, Matrix_NewMtx(globalCtx->state.gfxCtx, "../z_bg_mori_bigst.c", 548),
              G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_MODELVIEW);

    gSPDisplayList(oGfxCtx->polyOpa.p++, D_06001E50);
    CLOSE_DISPS(globalCtx->state.gfxCtx, "../z_bg_mori_bigst.c", 553);
}
