/**
 * d_a_player_food.inc
 *
 * Code relating to the All-Purpose Bait and Hyoi Pear items.
 * 
 * This file is not a standalone translation unit and is instead directly 
 * included into d_a_player_main.cpp.
 * 
 * The original name of this file is not known, but a best guess was taken 
 * based on the original names of the functions it contains.
 */

#include "d/dolzel.h" // IWYU pragma: keep
#include "d/actor/d_a_player_main.h"
#include "d/actor/d_a_player_HIO.h"
#include "d/d_camera.h"
#include "d/d_com_inf_game.h"
#include "d/d_item_data.h"

/* 80154334-801543A0       .text setHyoiModel__9daPy_lk_cFv */
void daPy_lk_c::setHyoiModel() {
    if (mEquipItem == dItemNo_HYOI_PEAR_e) {
        return;
    }
    JKRHeap* oldHeap = setItemHeap();
    initModel(&mpEquipItemModel, dRes_INDEX_LINK_BDL_HYOINOMI_e, 0x13000022);
    mDoExt_setCurrentHeap(oldHeap);
    mEquipItem = dItemNo_HYOI_PEAR_e;
}

/* 801543A0-801546F4       .text procFoodThrow_init__9daPy_lk_cFv */
BOOL daPy_lk_c::procFoodThrow_init() {
    if (mCurProc == daPyProc_FOOD_THROW_e) {
        return TRUE;
    }
    if (m3630 != fpcM_ERROR_PROCESS_ID_e) {
        seStartSystem(JA_SE_ITEM_TARGET_OUT);
        return FALSE;
    }

    if (!dComIfGp_event_runCheck()) {
        if (!dComIfGp_event_compulsory(this)) {
            return FALSE;
        } else {
            mDemo.setSpecialDemoType();
        }
    } else {
        deleteEquipItem(FALSE);
    }

    int temp = checkShipRideUseItem(1);

    commonProcInit(daPyProc_FOOD_THROW_e);

    if (temp == 0) {
        f32 offset1 = cM_ssin(shape_angle.y) * 150.0f;
        f32 offset2 = cM_scos(shape_angle.y) * 150.0f;
        cXyz start;
        start.set(attention_info.position.x, attention_info.position.y - 50.0f, attention_info.position.z);
        cXyz end;
        end.set(start.x + offset1, start.y, start.z + offset2);
        mLinkLinChk.Set(&start, &end, this);
        if (dComIfG_Bgsp()->LineCross(&mLinkLinChk)) {
            end.x = start.x - offset1;
            end.z = start.z - offset2;
            mLinkLinChk.Set(&start, &end, this);
            if (!dComIfG_Bgsp()->LineCross(&mLinkLinChk)) {
                shape_angle.y += 0x8000;
            } else {
                end.x = start.x + offset2;
                end.z = start.z - offset1;
                mLinkLinChk.Set(&start, &end, this);
                if (!dComIfG_Bgsp()->LineCross(&mLinkLinChk)) {
                    shape_angle.y += 0x4000;
                } else {
                    end.x = start.x - offset2;
                    end.z = start.z + offset1;
                    mLinkLinChk.Set(&start, &end, this);
                    if (!dComIfG_Bgsp()->LineCross(&mLinkLinChk)) {
                        shape_angle.y -= 0x4000;
                    }
                }
            }
        }
    }
    current.angle.y = shape_angle.y;
    mNormalSpeed = 0.0f;

    daPy_matAnm_c::offMabaFlg();
    setSingleMoveAnime(
        ANM_ESAMAKI,
        m_HIO->mItem.mFood.m.field_0x4,
        m_HIO->mItem.mFood.m.field_0x8,
        m_HIO->mItem.mFood.m.field_0x0,
        m_HIO->mItem.mFood.m.field_0xC
    );
    keepItemData();

    if (checkSpecialDemoMode()) {
        mProcVar6.m3570 = 6;
        dCam_getBody()->StartEventCamera(0x12, fopAcM_GetID(this), "Type", &mProcVar6.m3570, 0);
    }

    initShipRideUseItem(temp, 2);
    m3630 = fpcM_ERROR_PROCESS_ID_e;

    return TRUE;
}

/* 801546F4-80154880       .text procFoodThrow__9daPy_lk_cFv */
BOOL daPy_lk_c::procFoodThrow() {
    J3DFrameCtrl& frameCtrl = mFrameCtrlUnder[UNDER_MOVE0_e];
    setShipRidePosUseItem();
    if (frameCtrl.getRate() < 0.01f) {
        if (!checkSpecialDemoMode()) {
            dComIfGp_evmng_cutEnd(mStaffIdx);
        } else {
            dComIfGp_event_reset();
            fpc_ProcID playerPID = fopAcM_GetID(this);
            dCam_getBody()->EndEventCamera(playerPID);
            endDemoMode();
        }
    } else if (frameCtrl.checkPass(8.0f)) {
        seStartOnlyReverb(JA_SE_LK_ESA_THROW);
        void* esa = fopAcM_fastCreate(fpcNm_ESA_e, 0xFFFF0014, &mLeftHandPos, fopAcM_GetRoomNo(this), &shape_angle);
        if (esa) {
            m3630 = fopAcM_GetID(esa);
            if (!checkSpecialDemoMode()) {
                u8 talkBtn = dComIfGp_event_getTalkXYBtn();
                if (talkBtn == dTalkBtn_X_e) {
                    dComIfGs_setReserveBaitEmpty(dItemBtn_X_e);
                } else if (talkBtn == dTalkBtn_Y_e) {
                    dComIfGs_setReserveBaitEmpty(dItemBtn_Y_e);
                } else {
                    dComIfGs_setReserveBaitEmpty(dItemBtn_Z_e);
                }
            } else {
                dComIfGs_setReserveBaitEmpty(mReadyItemBtn);
            }
        }
    }
    return TRUE;
}

/* 80154880-80154A48       .text procFoodSet_init__9daPy_lk_cFv */
BOOL daPy_lk_c::procFoodSet_init() {
    if (mCurProc == daPyProc_FOOD_SET_e) {
        if (mDemo.getParam0() == 1) {
            if (mEquipItem == dItemNo_HYOI_PEAR_e) {
                u8 talkBtn = dComIfGp_event_getTalkXYBtn();
                if (talkBtn == dTalkBtn_X_e) {
                    dComIfGs_setEquipBottleItemEmpty(dItemBtn_X_e);
                } else if (talkBtn == dTalkBtn_Y_e) {
                    dComIfGs_setEquipBottleItemEmpty(dItemBtn_Y_e);
                } else {
                    dComIfGs_setEquipBottleItemEmpty(dItemBtn_Z_e);
                }
            }

            deleteEquipItem(FALSE);
        } else {
            setHyoiModel();
        }
        return TRUE;
    }

    if (!dComIfGp_event_runCheck()) {
        if (!dComIfGp_event_compulsory(this)) {
            return FALSE;
        }
        mDemo.setSpecialDemoType();
    }

    int temp = checkShipRideUseItem(1);
    commonProcInit(daPyProc_FOOD_SET_e);
    current.angle.y = shape_angle.y;

    mNormalSpeed = 0.0f;
    setSingleMoveAnime(
        ANM_SETHYOINOMI,
        m_HIO->mItem.mFood.m.field_0x10,
        m_HIO->mItem.mFood.m.field_0x14,
        m_HIO->mItem.mFood.m.field_0x2,
        m_HIO->mItem.mFood.m.field_0x18
    );
    keepItemData();
    setHyoiModel();

    if (checkSpecialDemoMode()) {
        mProcVar6.m3570 = 1;
        dCam_getBody()->StartEventCamera(0x12, fopAcM_GetID(this), "Type", &mProcVar6.m3570, 0);
    }

    initShipRideUseItem(temp, 2);
    mProcVar0.m34D0 = 1 * 30;

    return TRUE;
}

/* 80154A48-80154B80       .text procFoodSet__9daPy_lk_cFv */
BOOL daPy_lk_c::procFoodSet() {
    J3DFrameCtrl& frameCtrl = mFrameCtrlUnder[UNDER_MOVE0_e];

    setShipRidePosUseItem();

    if (frameCtrl.getRate() > 0.0f && frameCtrl.checkPass(9.0f)) {
        seStartOnlyReverb(JA_SE_LK_HYOI_SET);
    }

    if (std::abs(frameCtrl.getRate()) < 0.01f) {
        if (!checkSpecialDemoMode()) {
            dComIfGp_evmng_cutEnd(mStaffIdx);
        } else if (mProcVar0.m34D0 < 0) {
            dComIfGp_event_reset();
            deleteEquipItem(FALSE);
            fpc_ProcID playerPID = fopAcM_GetID(this);
            dCam_getBody()->EndEventCamera(playerPID);
            endDemoMode();
        } else if (mProcVar0.m34D0 == 0) {
            mProcVar0.m34D0 = -1;
            frameCtrl.setRate(-m_HIO->mItem.mFood.m.field_0x10);
        } else if (mProcVar0.m34D0 > 0) {
            mProcVar0.m34D0--;
        }
    }

    return TRUE;
}
