#include "z_bg_spot15_rrbox.h"

#define FLAGS 0x00000000

#define THIS ((BgSpot15Rrbox*)thisx)

void BgSpot15Rrbox_Init(Actor* thisx, GlobalContext* globalCtx);
void BgSpot15Rrbox_Destroy(Actor* thisx, GlobalContext* globalCtx);
void BgSpot15Rrbox_Update(Actor* thisx, GlobalContext* globalCtx);
void BgSpot15Rrbox_Draw(Actor* thisx, GlobalContext* globalCtx);

void func_808B4084(BgSpot15Rrbox* this, GlobalContext* globalCtx);
void func_808B40AC(BgSpot15Rrbox* this, GlobalContext* globalCtx);
void func_808B4194(BgSpot15Rrbox* this, GlobalContext* globalCtx);
void func_808B4380(BgSpot15Rrbox* this, GlobalContext* globalCtx);
void func_808B43D0(BgSpot15Rrbox* this, GlobalContext* globalCtx);
void func_808B44B8(BgSpot15Rrbox* this, GlobalContext* globalCtx);
void func_808B4178(BgSpot15Rrbox* this, GlobalContext* globalCtx);
void func_808B44CC(BgSpot15Rrbox* this, GlobalContext* globalCtx);

s16 D_808B4590 = 0;

extern UNK_TYPE D_06000348;
extern Gfx D_06000180[];

const ActorInit Bg_Spot15_Rrbox_InitVars = {
    ACTOR_BG_SPOT15_RRBOX,
    ACTORTYPE_BG,
    FLAGS,
    OBJECT_SPOT15_OBJ,
    sizeof(BgSpot15Rrbox),
    (ActorFunc)BgSpot15Rrbox_Init,
    (ActorFunc)BgSpot15Rrbox_Destroy,
    (ActorFunc)BgSpot15Rrbox_Update,
    (ActorFunc)BgSpot15Rrbox_Draw,
};

static InitChainEntry sInitChain[] = {
    ICHAIN_VEC3F_DIV1000(scale, 100, ICHAIN_CONTINUE),
    ICHAIN_F32(uncullZoneForward, 1000, ICHAIN_CONTINUE),
    ICHAIN_F32(uncullZoneScale, 500, ICHAIN_CONTINUE),
    ICHAIN_F32(uncullZoneDownward, 1000, ICHAIN_STOP),
};

Vec3f D_808B45C4[] = {
    { 770.0f, 1490.0f, -299.0f },
    { 770.0f, 1550.0f, -299.0f },
};

Vec3f D_808B45DC[] = {
    { 29.99f, 0.01f, -29.99f }, { -29.99f, 0.01f, -29.99f }, { -29.99f, 0.01f, 29.99f },
    { 29.99f, 0.01f, 29.99f },  { 0.0f, 0.01f, 0.0f },
};

void func_808B3960(BgSpot15Rrbox* this, GlobalContext* globalCtx, UNK_TYPE* arg2, DynaPolyMoveFlag flags) {
    s32 pad;
    UNK_TYPE temp = 0;
    u32 pad2;

    DynaPolyInfo_SetActorMove(&this->dyna, flags);
    DynaPolyInfo_Alloc(arg2, &temp);

    this->dyna.dynaPolyId = DynaPolyInfo_RegisterActor(globalCtx, &globalCtx->colCtx.dyna, &this->dyna.actor, temp);

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

void func_808B39E8(Vec3f* arg0, Vec3f* arg1, f32 arg2, f32 arg3) {
    arg0->x = (arg1->z * arg2) + (arg1->x * arg3);
    arg0->y = arg1->y;
    arg0->z = (arg1->z * arg3) - (arg1->x * arg2);
}

void func_808B3A34(BgSpot15Rrbox* this) {
    this->bgId = 50;
}

s32 func_808B3A40(BgSpot15Rrbox* this, GlobalContext* globalCtx) {
    DynaPolyActor* dynaPolyActor = DynaPolyInfo_GetActor(&globalCtx->colCtx, this->bgId);

    if (dynaPolyActor != NULL &&
        Math3D_Dist2DSq(dynaPolyActor->actor.posRot.pos.x, dynaPolyActor->actor.posRot.pos.z,
                        this->dyna.actor.posRot.pos.x, this->dyna.actor.posRot.pos.z) < 0.01f) {
        return true;
    }
    return false;
}

s32 func_808B3AAC(BgSpot15Rrbox* this, GlobalContext* globalCtx) {
    s16 rotY;
    Actor* actor = &this->dyna.actor;

    if (globalCtx->sceneNum == SCENE_SOUKO) {
        return true;
    } else if (func_808B3A40(this, globalCtx)) {
        return false;
    }

    if (actor->posRot.pos.x <= 930.0f && actor->posRot.pos.z >= -360.0f) {
        if (this->dyna.unk_150 >= 0.0f) {
            rotY = actor->posRot.rot.y;
        } else {
            rotY = actor->posRot.rot.y + 0x8000;
        }

        if (rotY < 0x2000 && rotY > -0x6000) {
            return gSaveContext.eventChkInf[1] & 0x10;
        }
        return true;
    }

    return true;
}

void BgSpot15Rrbox_Init(Actor* thisx, GlobalContext* globalCtx) {
    BgSpot15Rrbox* this = THIS;

    func_808B3960(this, globalCtx, &D_06000348, DPM_UNK);
    Actor_ProcessInitChain(&this->dyna.actor, sInitChain);
    func_808B3A34(this);
    if (Flags_GetSwitch(globalCtx, (this->dyna.actor.params & 0x3F))) {
        func_808B44B8(this, globalCtx);
        this->dyna.actor.posRot.pos = D_808B45C4[D_808B4590];
        D_808B4590++;
    } else {
        func_808B4084(this, globalCtx);
    }
    osSyncPrintf("(spot15 ロンロン木箱)(arg_data 0x%04x)\n", this->dyna.actor.params);
}

void BgSpot15Rrbox_Destroy(Actor* thisx, GlobalContext* globalCtx) {
    BgSpot15Rrbox* this = THIS;

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

s32 func_808B3CA0(BgSpot15Rrbox* this, GlobalContext* globalCtx, s32 arg2) {
    f32 chkDist = 0.0f;
    Vec3f actorPosition;
    Vec3f actorScale;

    func_808B3A34(this);

    actorScale.x = D_808B45DC[arg2].x * (this->dyna.actor.scale.x * 10.0f);
    actorScale.y = D_808B45DC[arg2].y * (this->dyna.actor.scale.y * 10.0f);
    actorScale.z = D_808B45DC[arg2].z * (this->dyna.actor.scale.z * 10.0f);

    func_808B39E8(&actorPosition, &actorScale, this->unk_16C, this->unk_170);

    actorPosition.x += this->dyna.actor.posRot.pos.x;
    actorPosition.y += this->dyna.actor.pos4.y;
    actorPosition.z += this->dyna.actor.posRot.pos.z;

    this->dyna.actor.groundY = func_8003CA64(&globalCtx->colCtx, &this->dyna.actor.floorPoly, &this->bgId,
                                             &this->dyna.actor, &actorPosition, chkDist);

    if ((this->dyna.actor.groundY - this->dyna.actor.posRot.pos.y) >= -0.001f) {
        this->dyna.actor.posRot.pos.y = this->dyna.actor.groundY;
        return true;
    }
    return false;
}

f32 func_808B3DDC(BgSpot15Rrbox* this, GlobalContext* globalCtx) {
    s32 i;
    Vec3f position;
    Vec3f scale;
    Actor* actor = &this->dyna.actor;
    f32 yIntersect;
    f32 returnValue = -32000.0f;
    s32 bgId;

    func_808B3A34(this);
    for (i = 0; i < ARRAY_COUNT(D_808B45DC); i++) {
        scale.x = D_808B45DC[i].x * (actor->scale.x * 10.0f);
        scale.y = D_808B45DC[i].y * (actor->scale.y * 10.0f);
        scale.z = D_808B45DC[i].z * (actor->scale.z * 10.0f);

        func_808B39E8(&position, &scale, this->unk_16C, this->unk_170);

        position.x += actor->posRot.pos.x;
        position.y += actor->pos4.y;
        position.z += actor->posRot.pos.z;

        yIntersect = func_8003CA64(&globalCtx->colCtx, &actor->floorPoly, &bgId, actor, &position, 0);

        if (returnValue < yIntersect) {
            returnValue = yIntersect;
            this->bgId = bgId;
        }
    }
    return returnValue;
}

s32 func_808B3F58(BgSpot15Rrbox* this, GlobalContext* globalCtx) {
    if (func_808B3CA0(this, globalCtx, 0)) {
        return true;
    }
    if (func_808B3CA0(this, globalCtx, 1)) {
        return true;
    }
    if (func_808B3CA0(this, globalCtx, 2)) {
        return true;
    }
    if (func_808B3CA0(this, globalCtx, 3)) {
        return true;
    }
    if (func_808B3CA0(this, globalCtx, 4)) {
        return true;
    }
    return false;
}

s32 func_808B4010(BgSpot15Rrbox* this, GlobalContext* globalCtx) {
    return !func_800435D8(globalCtx, &this->dyna, this->dyna.actor.scale.x * 290.0f,
                          this->dyna.actor.scale.x * 290.0f + 20.0f, 1.0f);
}

void func_808B4084(BgSpot15Rrbox* this, GlobalContext* globalCtx) {
    this->actionFunc = func_808B40AC;
    this->dyna.actor.gravity = 0.0f;
    this->dyna.actor.velocity.x = 0.0f;
    this->dyna.actor.velocity.y = 0.0f;
    this->dyna.actor.velocity.z = 0.0f;
}

void func_808B40AC(BgSpot15Rrbox* this, GlobalContext* globalCtx) {
    Player* player = PLAYER;

    if (this->unk_168 <= 0 && fabsf(this->dyna.unk_150) > 0.001f) {
        if (func_808B3AAC(this, globalCtx) && !func_808B4010(this, globalCtx)) {
            this->unk_17C = this->dyna.unk_150;
            func_808B4178(this, globalCtx);
        } else {
            player->stateFlags2 &= ~0x10;
            this->dyna.unk_150 = 0.0f;
        }
    } else {
        player->stateFlags2 &= ~0x10;
        this->dyna.unk_150 = 0.0f;
    }
}

void func_808B4178(BgSpot15Rrbox* this, GlobalContext* globalCtx) {
    this->actionFunc = func_808B4194;
    this->dyna.actor.gravity = 0.0f;
}

void func_808B4194(BgSpot15Rrbox* this, GlobalContext* globalCtx) {
    f32 sign;
    Player* player = PLAYER;
    f32 tempUnk178;
    s32 approxFResult;
    Actor* actor = &this->dyna.actor;

    this->unk_174 += 0.5f;

    this->unk_174 = CLAMP_MAX(this->unk_174, 2.0f);

    approxFResult = Math_ApproxF(&this->unk_178, 20.0f, this->unk_174);

    sign = this->unk_17C >= 0.0f ? 1.0f : -1.0f;

    tempUnk178 = (f32)sign * this->unk_178;
    actor->posRot.pos.x = actor->initPosRot.pos.x + (tempUnk178 * this->unk_16C);
    actor->posRot.pos.z = actor->initPosRot.pos.z + (tempUnk178 * this->unk_170);

    if (!func_808B3F58(this, globalCtx)) {
        actor->initPosRot.pos.x = actor->posRot.pos.x;
        actor->initPosRot.pos.z = actor->posRot.pos.z;
        player->stateFlags2 &= ~0x10;
        this->dyna.unk_150 = 0.0f;
        this->unk_178 = 0.0f;
        this->unk_174 = 0.0f;
        func_808B4380(this, globalCtx);
    } else if (approxFResult) {
        player = PLAYER;
        if (func_808B4010(this, globalCtx)) {
            Audio_PlayActorSound2(actor, NA_SE_EV_WOOD_BOUND);
        }
        if (func_808B3A40(this, globalCtx)) {
            func_80078884(NA_SE_SY_CORRECT_CHIME);
        }
        actor->initPosRot.pos.x = actor->posRot.pos.x;
        actor->initPosRot.pos.z = actor->posRot.pos.z;
        player->stateFlags2 &= ~0x10;
        this->dyna.unk_150 = 0.0f;
        this->unk_178 = 0.0f;
        this->unk_174 = 0.0f;
        this->unk_168 = 10;
        func_808B4084(this, globalCtx);
    }
    Audio_PlayActorSound2(actor, NA_SE_EV_ROCK_SLIDE - SFX_FLAG);
}

void func_808B4380(BgSpot15Rrbox* this, GlobalContext* globalCtx) {
    this->dyna.actor.velocity.x = 0.0f;
    this->dyna.actor.velocity.y = 0.0f;
    this->dyna.actor.velocity.z = 0.0f;
    this->dyna.actor.gravity = -1.0f;
    this->dyna.actor.groundY = func_808B3DDC(this, globalCtx);
    this->actionFunc = func_808B43D0;
}

void func_808B43D0(BgSpot15Rrbox* this, GlobalContext* globalCtx) {
    f32 groundY;
    Player* player = PLAYER;
    Actor* actor = &this->dyna.actor;

    if (fabsf(this->dyna.unk_150) > 0.001f) {
        this->dyna.unk_150 = 0.0f;
        player->stateFlags2 &= ~0x10;
    }

    Actor_MoveForward(actor);

    if (actor->posRot.pos.y <= -31990.0f) {
        osSyncPrintf("Warning : ロンロン木箱落ちすぎた(%s %d)(arg_data 0x%04x)\n", "../z_bg_spot15_rrbox.c", 599,
                     actor->params);

        Actor_Kill(actor);

        return;
    }

    groundY = actor->groundY;

    if ((groundY - actor->posRot.pos.y) >= -0.001f) {
        actor->posRot.pos.y = groundY;
        func_808B4084(this, globalCtx);
        Audio_PlayActorSound2(&this->dyna.actor, NA_SE_EV_WOOD_BOUND);
    }
}

void func_808B44B8(BgSpot15Rrbox* this, GlobalContext* globalCtx) {
    this->actionFunc = func_808B44CC;
}

void func_808B44CC(BgSpot15Rrbox* this, GlobalContext* globalCtx) {
    Player* player = PLAYER;

    player->stateFlags2 &= ~0x10;
    this->dyna.unk_150 = 0.0f;
}

void BgSpot15Rrbox_Update(Actor* thisx, GlobalContext* globalCtx) {
    BgSpot15Rrbox* this = THIS;

    if (this->unk_168 > 0) {
        this->unk_168--;
    }
    this->dyna.actor.posRot.rot.y = this->dyna.unk_158;
    this->unk_16C = Math_Sins(this->dyna.actor.posRot.rot.y);
    this->unk_170 = Math_Coss(this->dyna.actor.posRot.rot.y);
    this->actionFunc(this, globalCtx);
}

void BgSpot15Rrbox_Draw(Actor* thisx, GlobalContext* globalCtx) {
    Gfx_DrawDListOpa(globalCtx, D_06000180);
}
