//
// SecretRewardsLayer::onSelectItem from 0xa61db0 to 0xa621a0 (1008 bytes)
//
// 3 xrefs:
// > ??> @ 0x7ab08
// > SecretRewardsLayer::generateChestItems @ 0xa6a450
// > ??> @ 0x122c190
//
// This function is hookable on this platform!
//
// Using BromaIDA 8.0.0 @ https://github.com/Stazzical/BromaIDA
// Using bindings at commit ba9f177b at Thu Jul 16 22:52:06 2026 from https://github.com/geode-sdk/bindings
//
void SecretRewardsLayer::onSelectItem(SecretRewardsLayer *a1, cocos2d::CCObject *a2)
{
GameStatsManager *v4; // x0
int Stat; // w23
int v6; // w25
__int64 v7; // x0
int v8; // w26
GameStatsManager *v9; // x0
int v10; // w22
int v11; // w23
cocos2d::CCString *v12; // x0
size_t v13; // x0
int v14; // w22
DialogObject *v15; // x0
DialogLayer *v16; // x22
void *v17; // x0
void *v18; // x0
void *v19; // x0
GameStatsManager *v20; // x0
GJRewardItem *v21; // x22
RewardUnlockLayer *v22; // x19
cocos2d::CCLabelBMFont *m_keysLabel; // x19
void (__fastcall *setString)(cocos2d::CCLabelBMFont *__hidden, const unsigned __int8 *); // x22
GameStatsManager *v25; // x0
unsigned int v26; // w0
cocos2d::CCString *v27; // x0
const char *CString; // x0
unsigned int *v29; // x1
int v30; // w2
unsigned int *v31; // x1
int v32; // w2
unsigned int *v33; // x1
int v34; // w2
const char *v35; // [xsp+58h] [xbp+58h]
__int64 v36; // [xsp+70h] [xbp+70h] BYREF
__int64 v37; // [xsp+78h] [xbp+78h] BYREF
__int64 v38; // [xsp+80h] [xbp+80h] BYREF
v4 = GameStatsManager::sharedState();
Stat = GameStatsManager::getStat(v4, "21");
v6 = a2->getTag(a2);
v7 = ((__int64 (__fastcall *)(cocos2d::CCObject *))a2->__vftable[13].~CCObject_0)(a2);
v8 = (*(__int64 (__fastcall **)(__int64))(*(_QWORD *)v7 + 64LL))(v7);
v9 = GameStatsManager::sharedState();
v10 = GameStatsManager::keyCostForSecretChest(v9, v6);
if ( Stat >= v10 )
{
v20 = GameStatsManager::sharedState();
v21 = GameStatsManager::unlockSecretChest(v20, v6);
if ( v21 )
{
v21->m_chestID = v8;
SecretRewardsLayer::switchToOpenedState(a1, (CCMenuItemSpriteExtra *)a2);
v22 = RewardUnlockLayer::create(v21->m_rewardType, nullptr);
a1->addChild_0(a1, v22, 100);
RewardUnlockLayer::showCollectReward(v22, v21);
m_keysLabel = a1->m_keysLabel;
setString = m_keysLabel->setString;
v25 = GameStatsManager::sharedState();
v26 = GameStatsManager::getStat(v25, "21");
v27 = (cocos2d::CCString *)cocos2d::CCString::createWithFormat((cocos2d::CCString *)"%i", (const char *)v26);
CString = cocos2d::CCString::getCString(v27);
setString(m_keysLabel, (const unsigned __int8 *)CString);
SecretRewardsLayer::updateUnlockedLabel(a1);
}
}
else
{
v11 = v10 - Stat;
sub_D153B8((int)&v36, &byte_F288A0);
if ( v10 == 1 )
{
sub_D1798C(&v36, "You need a key to unlock this chest.", 52);
}
else
{
v12 = (cocos2d::CCString *)cocos2d::CCString::createWithFormat(
(cocos2d::CCString *)"You need %i more keys to unlock this chest",
(const char *)(unsigned int)v11);
v35 = cocos2d::CCString::getCString(v12);
v13 = strlen(v35);
sub_D1798C(&v36, v35, v13);
}
v14 = 18;
if ( v11 <= 50 )
{
v14 = 17;
if ( v11 <= 25 )
{
if ( v11 >= 11 )
v14 = 4;
else
v14 = 2;
}
}
sub_D153B8((int)&v37, "The Keymaster");
sub_D18D28(&v38);
v15 = (DialogObject *)DialogObject::create(1.0, (__int64)&v37, (__int64)&v38, v14, 0, (int)&unk_FFFFFF);
v16 = DialogLayer::create(v15, (int)&dword_0 + 2);
v17 = (void *)(v38 - 24);
if ( (__int64 *)(v38 - 24) != &qword_124B6B0 )
{
if ( &pthread_create )
{
v33 = (unsigned int *)(v38 - 8);
do
v34 = __ldaxr(v33);
while ( __stlxr(v34 - 1, v33) );
}
else
{
v34 = *(_DWORD *)(v38 - 8);
*(_DWORD *)(v38 - 8) = v34 - 1;
}
if ( v34 <= 0 )
operator delete(v17);
}
v18 = (void *)(v37 - 24);
if ( (__int64 *)(v37 - 24) != &qword_124B6B0 )
{
if ( &pthread_create )
{
v31 = (unsigned int *)(v37 - 8);
do
v32 = __ldaxr(v31);
while ( __stlxr(v32 - 1, v31) );
}
else
{
v32 = *(_DWORD *)(v37 - 8);
*(_DWORD *)(v37 - 8) = v32 - 1;
}
if ( v32 <= 0 )
operator delete(v18);
}
DialogLayer::addToMainScene(v16);
DialogLayer::updateChatPlacement(v16, Center);
DialogLayer::animateInRandomSide(v16);
v19 = (void *)(v36 - 24);
if ( (__int64 *)(v36 - 24) != &qword_124B6B0 )
{
if ( &pthread_create )
{
v29 = (unsigned int *)(v36 - 8);
do
v30 = __ldaxr(v29);
while ( __stlxr(v30 - 1, v29) );
}
else
{
v30 = *(_DWORD *)(v36 - 8);
*(_DWORD *)(v36 - 8) = v30 - 1;
}
if ( v30 <= 0 )
operator delete(v19);
}
}
}