#include "d/t/d_t_gate_to_ground.h"

#include "common.h"
#include "d/a/d_a_player.h"
#include "d/d_camera.h"
#include "d/d_sc_game.h"
#include "d/flag/storyflag_manager.h"
#include "f/f_base.h"
#include "toBeSorted/actor_event.h"
#include "toBeSorted/area_math.h"
#include "toBeSorted/event.h"

SPECIAL_ACTOR_PROFILE(GATE2GND_TAG, dTgGateToGround_c, fProfile::GATE2GND_TAG, 0x22, 0, 0);

int dTgGateToGround_c::create() {
    matrixCreateFromPosRotYScale(matrix, mPosition, mRotation.y, mScale, nullptr, 0.0f);

    delayFrames = 0;

    union {
        u32 val;
        struct {
            u16 a;
            u8 b;
            u8 c;
        };
    } v;
    v.val = mParams;
    params_FF_FF_00_00 = v.a;
    params_00_00_FF_00 = v.b;
    params_00_00_00_FF = v.c;

    return SUCCEEDED;
}

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

int dTgGateToGround_c::actorExecute() {
    dAcPy_c *player;

    if (params_FF_FF_00_00 != -1 && StoryflagManager::sInstance->getCounterOrFlag(params_FF_FF_00_00) == 0) {
        return SUCCEEDED;
    }

    player = dAcPy_c::LINK;

    if (checkAreaBox(matrix, player->mPosition)) {
        if (player->getRidingActorType() != dAcPy_c::RIDING_LOFTWING) {
            if (delayFrames > 15) {
                Event e("CloudHole", 100, 0, nullptr, nullptr);
                mEventRelated.scheduleEvent(e, 0);
            } else {
                delayFrames++;
            }
        } else {
            if (params_00_00_00_FF != -1) {
                // TODO Cast is maybe fake but I can't get overrideCam to match otherwise
                dScGame_c::getCamera(0)->getGameCam1()->overrideCam(params_00_00_00_FF, 0, (u16)mRoomID, false);
            }
            delayFrames = 0;
        }
    } else {
        delayFrames = 0;
    }

    return SUCCEEDED;
}

int dTgGateToGround_c::actorExecuteInEvent() {
    if (mEventRelated.isThisActorInEvent() != 0) {
        mEventRelated.advanceNext();
    }
    return SUCCEEDED;
}

int dTgGateToGround_c::draw() {
    return SUCCEEDED;
}
