#include "stdafx_ao.h"
#include "Function.hpp"
#include "CreditsController.hpp"
#include "Map.hpp"
#include "Game.hpp"
#include "stdlib.hpp"

namespace AO {

ALIVE_VAR(1, 0x507684, s16, gCreditsControllerExists_507684, false);

constexpr s32 kShowCreditScreenForTicks = 60;

void CreditsController::VUpdate()
{
    VUpdate_40D020();
}

BaseGameObject* CreditsController::VDestructor(s32 flags)
{
    return Vdtor_40D090(flags);
}

CreditsController* CreditsController::ctor_40CFC0(Path_CreditsController* /*pTlv*/, s32 /*tlvInfo*/)
{
    ctor_487E10(1);
    SetVTable(this, 0x4BA458);

    field_14_camera_number = gMap_507BA8.field_4_current_camera;
    field_10_next_cam_frame = gnFrameCount_507670 + kShowCreditScreenForTicks;

    gCreditsControllerExists_507684 = true;

    return this;
}

BaseGameObject* CreditsController::dtor_40D000()
{
    SetVTable(this, 0x4BA458);
    gCreditsControllerExists_507684 = false;
    return dtor_487DF0();
}

CreditsController* CreditsController::Vdtor_40D090(s32 flags)
{
    dtor_40D000();
    if (flags & 1)
    {
        ao_delete_free_447540(this);
    }
    return this;
}

void CreditsController::VUpdate_40D020()
{
    if (field_10_next_cam_frame <= static_cast<s32>(gnFrameCount_507670))
    {
        field_14_camera_number++;
        if (field_14_camera_number > 24u)
        {
            field_10_next_cam_frame = gnFrameCount_507670 + 60;
            field_14_camera_number = 1;
            gMap_507BA8.SetActiveCam_444660(LevelIds::eCredits_10, 1, 1, CameraSwapEffects::eTopToBottom_3, 0, 0);
            sBreakGameLoop_507B78 = 1;
        }
        else
        {
            gMap_507BA8.SetActiveCam_444660(LevelIds::eCredits_10, 1, field_14_camera_number, CameraSwapEffects::eTopToBottom_3, 0, 0);
            field_10_next_cam_frame = gnFrameCount_507670 + 60;
        }
    }
}

} // namespace AO
