//
// Generated by dtk
// Translation Unit: m_Do_controller_pad.cpp
//

#include "m_Do/m_Do_controller_pad.h"
#include "JSystem/JUtility/JUTGba.h"
#include "SSystem/SComponent/c_lib.h"
#include "f_ap/f_ap_game.h"
#include "m_Do/m_Do_Reset.h"
#include "m_Do/m_Do_gba_com.h"
#include "m_Do/m_Do_main.h"

JUTGamePad* g_mDoCPd_gamePad[4];
interface_of_controller_pad g_mDoCPd_cpadInfo[4];

/* 80007598-800078C0       .text mDoCPd_Convert__FP27interface_of_controller_padP10JUTGamePad */
static s32 mDoCPd_Convert(interface_of_controller_pad* pInterface, JUTGamePad* pPad) {
    pInterface->mButtonHold.up    = pPad->mButton.mButton & CButton::DPAD_UP;
    pInterface->mButtonHold.down  = pPad->mButton.mButton & CButton::DPAD_DOWN;
    pInterface->mButtonHold.left  = pPad->mButton.mButton & CButton::DPAD_LEFT;
    pInterface->mButtonHold.right = pPad->mButton.mButton & CButton::DPAD_RIGHT;
    pInterface->mButtonHold.z     = pPad->mButton.mButton & CButton::Z;
    pInterface->mButtonHold.r     = pPad->mButton.mButton & CButton::R;
    pInterface->mButtonHold.l     = pPad->mButton.mButton & CButton::L;
    pInterface->mButtonHold.a     = pPad->mButton.mButton & CButton::A;
    pInterface->mButtonHold.b     = pPad->mButton.mButton & CButton::B;
    pInterface->mButtonHold.x     = pPad->mButton.mButton & CButton::X;
    pInterface->mButtonHold.y     = pPad->mButton.mButton & CButton::Y;
    pInterface->mButtonHold.start = pPad->mButton.mButton & CButton::START;

    pInterface->mButtonTrig.up    = pPad->mButton.mTrigger & CButton::DPAD_UP;
    pInterface->mButtonTrig.down  = pPad->mButton.mTrigger & CButton::DPAD_DOWN;
    pInterface->mButtonTrig.left  = pPad->mButton.mTrigger & CButton::DPAD_LEFT;
    pInterface->mButtonTrig.right = pPad->mButton.mTrigger & CButton::DPAD_RIGHT;
    pInterface->mButtonTrig.z     = pPad->mButton.mTrigger & CButton::Z;
    pInterface->mButtonTrig.r     = pPad->mButton.mTrigger & CButton::R;
    pInterface->mButtonTrig.l     = pPad->mButton.mTrigger & CButton::L;
    pInterface->mButtonTrig.a     = pPad->mButton.mTrigger & CButton::A;
    pInterface->mButtonTrig.b     = pPad->mButton.mTrigger & CButton::B;
    pInterface->mButtonTrig.x     = pPad->mButton.mTrigger & CButton::X;
    pInterface->mButtonTrig.y     = pPad->mButton.mTrigger & CButton::Y;
    pInterface->mButtonTrig.start = pPad->mButton.mTrigger & CButton::START;
    
    pInterface->mMainStickPosX = pPad->getMainStickX();
    pInterface->mMainStickPosY = pPad->getMainStickY();
    pInterface->mMainStickValue = pPad->getMainStickValue();
    pInterface->mMainStickAngle = pPad->getMainStickAngle();
    pInterface->mCStickPosX = pPad->getSubStickX();
    pInterface->mCStickPosY = pPad->getSubStickY();
    pInterface->mCStickValue = pPad->getSubStickValue();
    pInterface->mCStickAngle = pPad->getSubStickAngle();

    mDoCPd_ANALOG_CONV(pPad->getAnalogA(), pInterface->mAnalogA);
    mDoCPd_ANALOG_CONV(pPad->getAnalogB(), pInterface->mAnalogB);
    mDoCPd_TRIGGER_CONV(pPad->getAnalogL(), pInterface->mTriggerLeft);
    mDoCPd_TRIGGER_CONV(pPad->getAnalogR(), pInterface->mTriggerRight);

    pInterface->mGamepadErrorFlags = pPad->getErrorStatus();
    return 1;
}

/* 800078C0-80007A70       .text mDoCPd_Read__Fv */
int mDoCPd_Read() {
    JUTGamePad::read();

    if (!mDoRst::isReset() && mDoRst::is3ButtonReset()) {
        JUTGamePad* pad = JUTGamePad::getGamePad(mDoRst::get3ButtonResetPort());

        if (!pad->isPushing3ButtonReset()) {
            mDoRst::off3ButtonReset();
        }
    }

    JUTGamePad** pad = g_mDoCPd_gamePad;
    interface_of_controller_pad* interface = g_mDoCPd_cpadInfo;

    for (int i = 0; i < 4; i++) {
        if (*pad == NULL) {
            cLib_memSet(interface++, 0, sizeof(interface_of_controller_pad));
        } else {
            mDoCPd_Convert(interface++, *pad);
        }

        pad++;
    }

    for (int i = 0; i < 4; i++) {
        f32 threshold_lo = g_HIO.mTriggerThreshLo;
        f32 threshold_hi = g_HIO.mTriggerThreshHi;

        f32 trigL = g_mDoCPd_cpadInfo[i].mTriggerLeft;
        g_mDoCPd_cpadInfo[i].mTrigLockL = false;
        g_mDoCPd_cpadInfo[i].mTrigLockR = false;

        if (trigL > threshold_lo) {
            if (g_mDoCPd_cpadInfo[i].mHoldLockL != true) {
                g_mDoCPd_cpadInfo[i].mTrigLockL = true;
            }

            g_mDoCPd_cpadInfo[i].mHoldLockL = true;
        } else if (trigL < threshold_hi) {
            g_mDoCPd_cpadInfo[i].mHoldLockL = false;
        }

        f32 trigR = g_mDoCPd_cpadInfo[i].mTriggerRight;
        if (trigR > threshold_lo) {
            if (g_mDoCPd_cpadInfo[i].mHoldLockR != true) {
                g_mDoCPd_cpadInfo[i].mTrigLockR = true;
            }

            g_mDoCPd_cpadInfo[i].mHoldLockR = true;
        } else if (trigR < threshold_hi) {
            g_mDoCPd_cpadInfo[i].mHoldLockR = false;
        }
    }

    g_mDoGaC_gbaCom.mDoGaC_Connect();
    return 1;
}

/* 80007A70-80007BBC       .text mDoCPd_Create__Fv */
int mDoCPd_Create() {
    JUTGamePad::mSuppressPadReset = 1;

    JUTGamePad* pad = new JUTGamePad(JUTGamePad::Port_1);
    g_mDoCPd_gamePad[0] = pad;
    g_mDoCPd_gamePad[1] = NULL;

    if (mDoMain::developmentMode) {
        g_mDoCPd_gamePad[2] = new JUTGamePad(JUTGamePad::Port_3);
        g_mDoCPd_gamePad[3] = new JUTGamePad(JUTGamePad::Port_4);
    } else {
        g_mDoCPd_gamePad[2] = NULL;
        g_mDoCPd_gamePad[3] = NULL;
    }

    JUTGamePad::setAnalogMode(3);

#if VERSION <= VERSION_JPN
    JUTGamePad::clearResetOccurred();
    JUTGamePad::setResetCallback(mDoRst_resetCallBack, NULL);
#else
    if (!mDoRst::isReset()) {
        JUTGamePad::clearResetOccurred();
        JUTGamePad::setResetCallback(mDoRst_resetCallBack, NULL);
    }
#endif

    JUTGba::create();
    g_mDoGaC_gbaCom.mDoGaC_Initial(TestDataManager, 16);

    for (int i = 0; i < 4; i++) {
        g_mDoCPd_cpadInfo[i].mHoldLockL = g_mDoCPd_cpadInfo[i].mTrigLockL = false;
        g_mDoCPd_cpadInfo[i].mHoldLockR = g_mDoCPd_cpadInfo[i].mTrigLockR = false;
    }

    return 1;
}
