/*
========================
idMenuScreen_Shell_Singleplayer::ContinueGame
========================
*/
void idMenuScreen_Shell_Singleplayer::ContinueGame() {
	const saveGameDetailsList_t & saveGameInfo = session->GetSaveGameManager().GetEnumeratedSavegames();
	saveGameDetailsList_t sortedSaves = saveGameInfo;
	sortedSaves.Sort( idSort_SavesByDate() );	
	if ( sortedSaves.Num() > 0 ) {
		if ( sortedSaves[0].damaged ) {
			class idSWFScriptFunction_ContinueDamaged : public idSWFScriptFunction_RefCounted {
			public:
				idSWFScriptVar Call( idSWFScriptObject * thisObject, const idSWFParmList & parms ) {
					common->Dialog().ClearDialog( GDM_CORRUPT_CONTINUE );
					return idSWFScriptVar();
				}
			};

			idStaticList< idSWFScriptFunction *, 4 > callbacks;
			callbacks.Append( new (TAG_SWF) idSWFScriptFunction_ContinueDamaged() );
			idStaticList< idStrId, 4 > optionText;
			optionText.Append( idStrId( "#str_04339" ) );	// OK
			common->Dialog().AddDynamicDialog( GDM_CORRUPT_CONTINUE, callbacks, optionText, false, "" );
		} else {
			const idStr & name = sortedSaves[ 0 ].slotName;
			cmdSystem->AppendCommandText( va( "loadgame %s\n", name.c_str() ) );
		}
	}
}
/*
========================
idMenuWidget_Shell_SaveInfo::UpdateSavePreviews
========================
*/
void idMenuWidget_Shell_SaveInfo::UpdateSavePreviews()
{
	const saveGameDetailsList_t& saveGameInfo = session->GetSaveGameManager().GetEnumeratedSavegames();
	saveGameDetailsList_t sortedSaves = saveGameInfo;
	idStr thumbNailFile;
	for(int i = 0; i < MAX_SAVEGAMES; i++) {
		saveGameThumbnails[i] = NULL;
	}

	/*
	 *	Generate each auto-save preview at this time.
	 */
	if(sortedSaves.Num() != 0) {
		const char* saveFolder = "savegame";
		// Sort the Games before looking up the image preview.
		sortedSaves.Sort( idSort_SavesByDate() );
		for(int i =0; i < sortedSaves.Num();i++ )
		{
	        /* Test to See if there is a Generated Save Image */
			const idSaveGameDetails& details = sortedSaves[ i ];
			if(details.GetHasThumbnail()) {
				thumbNailFile = saveFolder;
				thumbNailFile.Append("/");
				thumbNailFile.Append(details.GetSlotName());
				thumbNailFile.Append("/" SAVEGAME_THUMB_FILENAME);
				saveGameThumbnails[i] = declManager->FindMaterial( thumbNailFile.c_str() );
			}
		}
		for(int i =0; i < sortedSaves.Num();i++ )
		{
			const idSaveGameDetails& details = sortedSaves[ i ];
			if(saveGameThumbnails[i] != NULL)
				continue;
			/* Check for pre-generated Thumbnail */
			thumbNailFile = "maps/";
			thumbNailFile.Append(details.GetMapFileName());
			saveGameThumbnails[i] = declManager->FindMaterial( thumbNailFile.c_str(), false );
		}
	    renderSystem->LoadLevelImages();
    }
}
/*
========================
idMenuScreen_Shell_Save::UpdateSaveEnumerations
========================
*/
void idMenuScreen_Shell_Save::UpdateSaveEnumerations()
{

	const saveGameDetailsList_t& saveGameInfo = session->GetSaveGameManager().GetEnumeratedSavegames();
	sortedSaves = saveGameInfo;
	idList< idList< idStr, TAG_IDLIB_LIST_MENU >, TAG_IDLIB_LIST_MENU > saveList;
	int newSaveOffset = 1;
	bool hasAutosave = false;
	
	if( session->GetSaveGameManager().IsWorking() )
	{
		idList< idStr > saveName;
		saveName.Append( "#str_dlg_refreshing" );
		saveList.Append( saveName );
		
		if( options != NULL )
		{
			options->SetListData( saveList );
			options->Update();
		}
	}
	else
	{
	
		for( int i = 0; i < saveGameInfo.Num(); ++i )
		{
			if( saveGameInfo[i].slotName.Icmp( "autosave" ) == 0 )
			{
				hasAutosave = true;
				break;
			}
		}
		
		if( saveGameInfo.Num() == MAX_SAVEGAMES || ( !hasAutosave && saveGameInfo.Num() == MAX_SAVEGAMES - 1 ) )
		{
			newSaveOffset = 0;
		}
		
		if( newSaveOffset != 0 )
		{
			idList< idStr > newSave;
			newSave.Append( "#str_swf_new_save_game" );
			saveList.Append( newSave );
		}
		
		if( options != NULL )
		{
			sortedSaves.Sort( idSort_SavesByDate() );
			
			for( int slot = 0; slot < sortedSaves.Num(); ++slot )
			{
				const idSaveGameDetails& details = sortedSaves[slot];
				if( details.slotName.Icmp( "autosave" ) == 0 )
				{
					sortedSaves.RemoveIndex( slot );
					slot--;
				}
			}
			// +1 because the first item is "New Save"
			saveList.SetNum( sortedSaves.Num() + newSaveOffset );
			for( int slot = 0; slot < sortedSaves.Num(); ++slot )
			{
				idStr& slotSaveName = saveList[ slot + newSaveOffset ].Alloc();
				const idSaveGameDetails& details = sortedSaves[slot];
				if( details.damaged )
				{
					slotSaveName =  va( S_COLOR_RED "%s", idLocalization::GetString( "#str_swf_corrupt_file" ) );
				}
				else if( details.GetSaveVersion() > BUILD_NUMBER )
				{
					slotSaveName =  va( S_COLOR_RED "%s", idLocalization::GetString( "#str_swf_wrong_version" ) );
				}
				else
				{
					if( details.slotName.Icmp( "autosave" ) == 0 )
					{
						slotSaveName.Append( S_COLOR_YELLOW );
					}
					else if( details.slotName.Icmp( "quick" ) == 0 )
					{
						slotSaveName.Append( S_COLOR_ORANGE );
					}
					slotSaveName.Append( details.GetMapName() );
				}
			}
			
			options->SetListData( saveList );
			options->Update();
		}
	}
	
	if( menuData != NULL )
	{
		idMenuWidget_CommandBar* cmdBar = menuData->GetCmdBar();
		if( cmdBar != NULL )
		{
			cmdBar->ClearAllButtons();
			idMenuWidget_CommandBar::buttonInfo_t* buttonInfo;
			buttonInfo = cmdBar->GetButton( idMenuWidget_CommandBar::BUTTON_JOY2 );
			if( menuData->GetPlatform() != 2 )
			{
				buttonInfo->label = "#str_00395";	// BACK
			}
			buttonInfo->action.Set( WIDGET_ACTION_GO_BACK );
			
			
			if( !session->GetSaveGameManager().IsWorking() )
			{
				buttonInfo = cmdBar->GetButton( idMenuWidget_CommandBar::BUTTON_JOY1 );
				if( menuData->GetPlatform() != 2 )
				{
					buttonInfo->label = "#str_02179";	// SAVE GAME
				}
				buttonInfo->action.Set( WIDGET_ACTION_PRESS_FOCUSED );
				
				if( options != NULL )
				{
					if( options->GetViewIndex() != 0 || ( options->GetViewIndex() == 0 && newSaveOffset == 0 ) )
					{
						buttonInfo = cmdBar->GetButton( idMenuWidget_CommandBar::BUTTON_JOY3 );
						if( menuData->GetPlatform() != 2 )
						{
							buttonInfo->label = "#str_02315";	// DELETE
						}
						buttonInfo->action.Set( WIDGET_ACTION_JOY3_ON_PRESS );
						
						if( btnDelete != NULL )
						{
							idSWFScriptObject& root = GetSWFObject()->GetRootObject();
							if( btnDelete->BindSprite( root ) )
							{
								if( menuData->GetPlatform() != 2 )
								{
									btnDelete->SetLabel( "" );
								}
								else
								{
									btnDelete->GetSprite()->SetVisible( true );
									btnDelete->SetLabel( "#str_02315" );
								}
							}
							btnDelete->Update();
						}
					}
					else
					{
						if( btnDelete != NULL )
						{
							idSWFScriptObject& root = GetSWFObject()->GetRootObject();
							if( btnDelete->BindSprite( root ) )
							{
								btnDelete->SetLabel( "" );
								btnDelete->Update();
							}
						}
					}
				}
			}
			cmdBar->Update();
		}
	}
	
	if( saveInfo != NULL )
	{
		saveInfo->Update();
	}
	
	if( options != NULL && options->GetTotalNumberOfOptions() > 0 && options->GetViewIndex() >= options->GetTotalNumberOfOptions() )
	{
		options->SetViewIndex( options->GetTotalNumberOfOptions() - newSaveOffset );
		if( options->GetViewOffset() > options->GetViewIndex() )
		{
			options->SetViewOffset( options->GetViewIndex() );
		}
		options->SetFocusIndex( options->GetViewIndex() - options->GetViewOffset() );
	}
}
/*
========================
idMenuScreen_Shell_Load::UpdateSaveEnumerations
========================
*/
void idMenuScreen_Shell_Load::UpdateSaveEnumerations()
{

	const saveGameDetailsList_t& saveGameInfo = session->GetSaveGameManager().GetEnumeratedSavegames();
	sortedSaves = saveGameInfo;
	sortedSaves.Sort( idSort_SavesByDate() );
	
	if( options != NULL )
	{
		idList< idList< idStr, TAG_IDLIB_LIST_MENU >, TAG_IDLIB_LIST_MENU > saveList;
		if( session->GetSaveGameManager().IsWorking() )
		{
			idList< idStr > saveName;
			saveName.Append( "#str_dlg_refreshing" );
			saveList.Append( saveName );
		}
		else if( sortedSaves.Num() == 0 )
		{
			idList< idStr > saveName;
			saveName.Append( "#str_no_saves_found" );
			saveList.Append( saveName );
		}
		else
		{
		
			saveList.SetNum( sortedSaves.Num() );
			for( int slot = 0; slot < sortedSaves.Num(); ++slot )
			{
				idStr& slotSaveName = saveList[slot].Alloc();
				const idSaveGameDetails& details = sortedSaves[slot];
				if( details.damaged )
				{
					slotSaveName =  va( S_COLOR_RED "%s", idLocalization::GetString( "#str_swf_corrupt_file" ) );
				}
				else if( details.GetSaveVersion() > BUILD_NUMBER )
				{
					slotSaveName =  va( S_COLOR_RED "%s", idLocalization::GetString( "#str_swf_wrong_version" ) );
				}
				else
				{
					if( details.slotName.Icmp( "autosave" ) == 0 )
					{
						slotSaveName.Append( S_COLOR_YELLOW );
					}
					else if( details.slotName.Icmp( "quick" ) == 0 )
					{
						slotSaveName.Append( S_COLOR_ORANGE );
					}
					slotSaveName.Append( details.GetMapName() );
				}
			}
		}
		options->SetListData( saveList );
		options->Update();
	}
	
	if( menuData != NULL )
	{
		idMenuWidget_CommandBar* cmdBar = menuData->GetCmdBar();
		if( cmdBar != NULL )
		{
			cmdBar->ClearAllButtons();
			idMenuWidget_CommandBar::buttonInfo_t* buttonInfo;
			buttonInfo = cmdBar->GetButton( idMenuWidget_CommandBar::BUTTON_JOY2 );
			if( menuData->GetPlatform() != 2 )
			{
				buttonInfo->label = "#str_swf_back";	// BACK
			}
			buttonInfo->action.Set( WIDGET_ACTION_GO_BACK );
			
			
			
			if( sortedSaves.Num() > 0 && !session->GetSaveGameManager().IsWorking() )
			{
				buttonInfo = cmdBar->GetButton( idMenuWidget_CommandBar::BUTTON_JOY1 );
				if( menuData->GetPlatform() != 2 )
				{
					buttonInfo->label = "#str_swf_load_heading";	// LOAD GAME
				}
				buttonInfo->action.Set( WIDGET_ACTION_PRESS_FOCUSED );
				
				buttonInfo = cmdBar->GetButton( idMenuWidget_CommandBar::BUTTON_JOY3 );
				if( menuData->GetPlatform() != 2 )
				{
					buttonInfo->label = "#str_swf_load_delete";	// DELETE GAME
				}
				buttonInfo->action.Set( WIDGET_ACTION_JOY3_ON_PRESS );
				
				if( btnDelete != NULL )
				{
					idSWFScriptObject& root = GetSWFObject()->GetRootObject();
					if( btnDelete->BindSprite( root ) )
					{
						if( menuData->GetPlatform() != 2 )
						{
							btnDelete->SetLabel( "" );
						}
						else
						{
							btnDelete->GetSprite()->SetVisible( true );
							btnDelete->SetLabel( "#str_swf_load_delete" );
						}
					}
					btnDelete->Update();
				}
			}
			else
			{
				if( btnDelete != NULL )
				{
					idSWFScriptObject& root = GetSWFObject()->GetRootObject();
					if( btnDelete->BindSprite( root ) )
					{
						btnDelete->SetLabel( "" );
						btnDelete->Update();
					}
				}
			}
			cmdBar->Update();
		}
	}
	
	if( saveInfo != NULL )
	{
		saveInfo->Update();
	}
	
	if( options != NULL && options->GetTotalNumberOfOptions() > 0 && options->GetViewIndex() >= options->GetTotalNumberOfOptions() )
	{
		options->SetViewIndex( options->GetTotalNumberOfOptions() - 1 );
		if( options->GetViewOffset() > options->GetViewIndex() )
		{
			options->SetViewOffset( options->GetViewIndex() );
		}
		options->SetFocusIndex( options->GetViewIndex() - options->GetViewOffset() );
	}
}
/*
========================
idMenuWidget_Shell_SaveInfo::Update
========================
*/
void idMenuWidget_Shell_SaveInfo::Update()
{

	if( GetSWFObject() == NULL )
	{
		return;
	}
	
	idSWFScriptObject& root = GetSWFObject()->GetRootObject();
	if( !BindSprite( root ) || GetSprite() == NULL )
	{
		return;
	}
	
	const saveGameDetailsList_t& saveGameInfo = session->GetSaveGameManager().GetEnumeratedSavegames();
	
	saveGameDetailsList_t sortedSaves = saveGameInfo;
	sortedSaves.Sort( idSort_SavesByDate() );
	
	for( int slot = 0; slot < sortedSaves.Num(); ++slot )
	{
		const idSaveGameDetails& details = sortedSaves[slot];
		if( forSaveScreen && details.slotName.Icmp( "autosave" ) == 0 )
		{
			sortedSaves.RemoveIndex( slot );
			slot--;
		}
	}
	
	idStr info;
	if( loadIndex >= 0 && sortedSaves.Num() != 0 && loadIndex < sortedSaves.Num() )
	{
		const idSaveGameDetails& details = sortedSaves[ loadIndex ];
		
		info.Append( Sys_TimeStampToStr( details.date ) );
		info.Append( "\n" );
		
		// PS3 only strings that use the dict just set
		const char* expansionStr = "";
		switch( details.GetExpansion() )
		{
			case GAME_D3XP:
				expansionStr = idLocalization::GetString( "#str_swf_resurrection" );
				break;
			case GAME_D3LE:
				expansionStr = idLocalization::GetString( "#str_swf_lost_episodes" );
				break;
			case GAME_BASE:
				expansionStr = idLocalization::GetString( "#str_swf_doom3" );
				break;
			default:
				expansionStr = idLocalization::GetString( "#str_savegame_title" );
				break;
		}
		
		const char* difficultyStr = "";
		switch( details.GetDifficulty() )
		{
			case 0:
				difficultyStr = idLocalization::GetString( "#str_swf_difficulty_easy" ); // easy
				break;
			case 1:
				difficultyStr = idLocalization::GetString( "#str_swf_difficulty_medium" ); // medium
				break;
			case 2:
				difficultyStr = idLocalization::GetString( "#str_swf_difficulty_hard" ); // hard
				break;
			case 3:
				difficultyStr = idLocalization::GetString( "#str_swf_difficulty_impossible" ); // impossible
				break;
		}
		
		idStr summary;
		summary.Format( idLocalization::GetString( "#str_swf_save_info_format" ), difficultyStr, Sys_SecToStr( details.GetPlaytime() ), expansionStr );
		
		info.Append( summary );
		
		if( details.damaged )
		{
			info.Append( "\n" );
			info.Append( va( "^1%s^0", idLocalization::GetString( "#str_swf_damaged" ) ) );
		}
		else if( details.GetSaveVersion() > BUILD_NUMBER )
		{
			info.Append( "\n" );
			info.Append( va( "^1%s^0", idLocalization::GetString( "#str_swf_wrong_version" ) ) );
		}
	}
	
	idSWFTextInstance* infoSprite = GetSprite()->GetScriptObject()->GetNestedText( "txtDesc" );
	if( infoSprite != NULL )
	{
		infoSprite->SetText( info );
	}
	
	idSWFSpriteInstance* img = GetSprite()->GetScriptObject()->GetNestedSprite( "img" );
	if( img != NULL )
	{
	    // TODO_SPARTY: until we have a thumbnail hide the image
		if(forSaveScreen )
			img->SetVisible( false );
		else
			img->SetVisible( true );
	    if(	saveGameThumbnails[loadIndex] != NULL ) {
		    // Use Detected Thumbnail
		    img->SetMaterial( saveGameThumbnails[loadIndex] );
	    } else {
		    if(saveThumbDefault != NULL) {
			    // Use Default Thumbnail.
			    img->SetMaterial( saveThumbDefault );
		    } else {
			    // No thumbnail Image exists.
			    img->SetVisible( false );
		    }
	    }
	}
}