//----------------------------------------------------------
// Query the system interface for this game's list of achievements
//----------------------------------------------------------
void CAchievementsDialog_XBox::ApplySettings( KeyValues *pResourceData )
{
	BaseClass::ApplySettings( pResourceData );

	m_bCenterOnScreen = pResourceData->GetInt( "center", 1 ) == 1;

	if ( !achievementmgr )
	{
		AssertOnce( 0 && "No achievement manager interface in GameUI.dll" );
		return;
	}

	achievementmgr->EnsureGlobalStateLoaded();

	int iAllAchievements = achievementmgr->GetAchievementCount();
	
	for ( int i = 0; i < iAllAchievements; ++i )
	{		
		IAchievement* pCurAchievement = (IAchievement*)achievementmgr->GetAchievementByIndex( i );
		Assert ( pCurAchievement );

		if ( pCurAchievement->IsAchieved() )
			++m_nUnlocked;

		// don't show hidden achievements if not achieved
		if ( pCurAchievement->ShouldHideUntilAchieved() && !pCurAchievement->IsAchieved() )
			continue;

		CAchievementItem *pItem = m_Menu.AddAchievementItem(  ACHIEVEMENT_LOCALIZED_NAME( pCurAchievement ), 
									ACHIEVEMENT_LOCALIZED_DESC( pCurAchievement ), 
									pCurAchievement->GetPointValue(),
									pCurAchievement->IsAchieved(),
									pCurAchievement
									);

		SETUP_PANEL( pItem );
	}

	m_nTotalAchievements = m_Menu.GetItemCount();
}
//=============================================================================
void Achievements::Activate()
{
	BaseClass::Activate();

	// Populate the achievements list.
	m_GplAchievements->RemoveAllPanelItems();

	if ( !achievementmgr )
		return;

	m_iAwardCompleteCount = 0;
	m_iAchCompleteCount = 0;
	int incompleteCount= 0;
	int gamerScore = 0;

	//
	// Add the "Achieved" ones
	//
	for(int i = 0; i < achievementmgr->GetAchievementCount(); i++)
	{
		IAchievement* achievement = achievementmgr->GetAchievementByDisplayOrder( i, m_iStartingUserSlot );

		if ( achievement && achievement->IsAchieved() )
		{
			AchievementListItem *panelItem = new AchievementListItem( achievement );
			if ( panelItem )
			{
				m_GplAchievements->AddPanelItem( panelItem, true );
			}

			gamerScore += achievement->GetPointValue();
			++m_iAchCompleteCount;
		}
	}

	//
	// Add the "Unattained" ones
	//
	for(int i = 0; i < achievementmgr->GetAchievementCount(); i++)
	{
		IAchievement* achievement = achievementmgr->GetAchievementByDisplayOrder( i, m_iStartingUserSlot );

		if ( achievement && !achievement->IsAchieved() )
		{
			AchievementListItem *panelItem = new AchievementListItem( achievement );
			if ( panelItem )
			{
				m_GplAchievements->AddPanelItem( panelItem, true );
			}

			++incompleteCount;
		}
	} 

	// Populate the awards list.
	int awardIncompleteCount= 0;
	if ( IsX360() )
	{
		m_GplAwards->RemoveAllPanelItems();

		for ( int i = 0; i < achievementmgr->GetAchievementCount(); i++ )
		{
			IAchievement* achievement = achievementmgr->GetAchievementByIndex( i, m_iStartingUserSlot );
			if ( achievement && achievement->IsAchieved() )
			{
				AchievementListItem *panelItem = new AchievementListItem( achievement );
				if ( panelItem )
				{
					m_GplAchievements->AddPanelItem( panelItem, true );
				}

				gamerScore += achievement->GetPointValue();
				++m_iAwardCompleteCount;
			}
		}

		for( int i = 0; i < achievementmgr->GetAchievementCount(); i++ )
		{
			IAchievement* achievement = achievementmgr->GetAchievementByIndex( i, m_iStartingUserSlot );
			if ( achievement && !achievement->IsAchieved() )
			{
				AchievementListItem *panelItem = new AchievementListItem( achievement );
				if ( panelItem )
				{
					m_GplAchievements->AddPanelItem( panelItem, true );
				}

				++awardIncompleteCount;
			}
		} 
	}
	if ( m_GplAwards )
	{
		m_GplAwards->SetVisible( false );
	}

	//
	// Update achievement and gamerscore progress
	//
	wchar_t localizedGamerscoreProgress[128]; 
	char buffer[64];
	wchar_t wNumAchieved[64];
	wchar_t wTotalAchievements[64];
	wchar_t wGamerscore[64];

	// Construct achievement progress string
	itoa( achievementmgr->GetAchievementCount(), buffer, 10 );
	V_UTF8ToUnicode( buffer, wTotalAchievements, sizeof( wNumAchieved ) );
	itoa( m_iAchCompleteCount, buffer, 10 );
	V_UTF8ToUnicode( buffer, wNumAchieved, sizeof( wTotalAchievements ) );
	g_pVGuiLocalize->ConstructString( m_wAchievementsProgress, sizeof( m_wAchievementsProgress ), g_pVGuiLocalize->Find( "#L4D360UI_Achievement_Progress" ), 2, wNumAchieved, wTotalAchievements );
	m_LblComplete->SetText( m_wAchievementsProgress );

	// Construct gamerscore progress string
	itoa( gamerScore, buffer, 10 );
	V_UTF8ToUnicode( buffer, wGamerscore, sizeof( wGamerscore ) );
	g_pVGuiLocalize->ConstructString( localizedGamerscoreProgress, sizeof( localizedGamerscoreProgress ), g_pVGuiLocalize->Find( "#L4D360UI_Gamerscore_Progress" ), 2, wGamerscore );
	m_LblGamerscore->SetText( localizedGamerscoreProgress );

	// Focus on the first item in the list
	m_GplAchievements->NavigateTo();
	m_GplAchievements->SelectPanelItem( 0 );

	// Set the progress bar
	m_flTotalProgress = static_cast<float>(m_iAchCompleteCount) / static_cast<float>(achievementmgr->GetAchievementCount());

	UpdateFooter();

	ToggleDisplayType( m_bShowingAssets );
}