#include "d/a/obj/d_a_obj_pool_cock.h"

#include "d/a/obj/d_a_obj_vortex.h"
#include "d/d_stage.h"
#include "d/flag/sceneflag_manager.h"
#include "s/s_Math.h"

SPECIAL_ACTOR_PROFILE(OBJ_POOL_COCK, dAcOPoolCock_c, fProfile::OBJ_POOL_COCK, 0x024D, 0, 7);

STATE_DEFINE(dAcOPoolCock_c, Wait);

bool dAcOPoolCock_c::createHeap() {
    mBrres = nw4r::g3d::ResFile(getOarcResFile("WaterD101"));
    dStage_c::bindStageResToFile(&mBrres);
    nw4r::g3d::ResMdl mdl = mBrres.GetResMdl("PoolCockD101");
    for (int i = 0; i < 2; i++) {
        TRY_CREATE(mModels[i].create(mdl, &mAllocator, 0x120));
    }
    return true;
}

int dAcOPoolCock_c::actorCreate() {
    CREATE_ALLOCATOR(dAcOPoolCock_c);

    mAcceleration = -0.0f;
    mMaxSpeed = -40.0f;
    mOpenSceneflag = mParams & 0xFF;
    mOpenDirection = mVec3_c::Ez;
    mOpenDirection.rotY(mRotation.y);

    if (mOpenSceneflag < 0xFF && SceneflagManager::sInstance->checkBoolFlag(mRoomID, mOpenSceneflag)) {
        mOpenProgress = 400.0f;
    }

    mStateMgr.changeState(StateID_Wait);

    mBoundingBox.Set(mVec3_c(-300.0f, -100.0f, -300.0f), mVec3_c(300.0f, 100.0f, 300.0f));
    return SUCCEEDED;
}

int dAcOPoolCock_c::actorPostCreate() {
    return SUCCEEDED;
}

int dAcOPoolCock_c::doDelete() {
    return SUCCEEDED;
}

int dAcOPoolCock_c::actorExecute() {
    mStateMgr.executeState();
    calcVelocity();
    mPosition += mVelocity;
    mPosition += mStts.mCcMove;
    updateMatrix();

    mMtx_c mdl1Transform(mWorldMtx);
    mMtx_c mdl2Transform(mWorldMtx);

    mMtx_c tmp1;
    mMtx_c tmp2;

    mVec3_c translation = mOpenDirection * mOpenProgress;
    MTXTrans(tmp1, translation.x, translation.y, translation.z);
    MTXConcat(mdl1Transform, tmp1, mdl1Transform);

    mVec3_c translation2 = -translation;
    MTXTrans(tmp2, translation2.x, translation2.y, translation2.z);
    MTXConcat(mdl2Transform, tmp2, mdl2Transform);

    mdl2Transform.YrotM(-0x8000);
    mModels[0].setLocalMtx(mdl1Transform);
    mModels[1].setLocalMtx(mdl2Transform);

    return SUCCEEDED;
}

int dAcOPoolCock_c::draw() {
    if (mOpenProgress < 400.0f) {
        drawModelType1(&mModels[0]);
        drawModelType1(&mModels[1]);
    }
    return SUCCEEDED;
}

void dAcOPoolCock_c::initializeState_Wait() {}

void dAcOPoolCock_c::executeState_Wait() {
    if (mOpenSceneflag < 0xFF && SceneflagManager::sInstance->checkBoolFlag(mRoomID, mOpenSceneflag)) {
        sLib::chase(&mOpenProgress, 400.0f, 5.0f);
        if (!mHasActivatedVortex) {
            dAcOVortex_c *vortex = (dAcOVortex_c *)fManager_c::searchBaseByProfName(fProfile::OBJ_VORTEX, nullptr);
            if (vortex) {
                vortex->triggerActivation();
            }
            mHasActivatedVortex = true;
        }
    }
}

void dAcOPoolCock_c::finalizeState_Wait() {}
