/*
	Asteroids
    Copyright (C) 2006  Darragh Coy.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*##############################################################################

Module name: ShowScores.cpp
Purpose:     Displays all of the scores of all players to the user. All scores are
             written to a html file and then IE-EXPLORER is opened to display them.
             This saves the bother of worrying whether all the scores will fit
             on the screen or not. 

##############################################################################*/

#include "ShowScores.h"

/*******************************************************************************
Private module prototypes: Not for outside use!
*******************************************************************************/

   //..     

/*******************************************************************************
Variables
*******************************************************************************/

   //..


/*******************************************************************************

Name:       ShowScores_Show
Purpose:    Writes all of the score details to a a html file and then displays them
Parameters: None
Returns:    None

*******************************************************************************/

void ShowScores_Show()
{
     /**************************************************************************
     Pause the game first
     **************************************************************************/          
     
     gamePaused = true;
     
     /**************************************************************************
     Make the html file directory first if it doesnt exist
     **************************************************************************/     
     
     CreateDirectory(HTML_DIR,0);
     
     /**************************************************************************
     Try and open the file to write to: open in text mode...
     **************************************************************************/
     
     ofstream out(HTML_FILE_NAME,ios::out);
     
     // If not open then display error message and abort
     
     if ( ! out.is_open() )
     { 
          GLWin_Message
             (
             "Could not display the scores in a html file because\n"
             "The file could not be saved to. ",
             "Error displaying scores"
             );
          
          out.close(); return; 
          
     }

     /**************************************************************************
     Write the html file header
     **************************************************************************/     
     
     out << "<html><head><title>Asteroids: High scores </title></head>"
         << "<body bgcolor=\"#000000\">"
         << "<font color  =\"#aaffff\">"
         << "<h1>Asteroids User High Scores: </h1><p>"
         << "<table cellpadding=5 cellspacing=20>";
         
     /**************************************************************************
     Search the user directory for user information. Keep going while there
     are files to be found or an error occurs. Write each users name and score
     to the html file
     **************************************************************************/
     
     // Set the current directory to search in as the user data directory
     
     SetCurrentDirectory(USER_DATA_DIR);
     
     // Windows stores search results in this structure. Needed to search for
     // files and hold onto their names
     
     WIN32_FIND_DATA findResults;
     
     // Windows uses a search handle between consequtive searches. Presumably
     // so it knows where it searched from last. Declare here
     
     HANDLE searchHandle = NULL;
     
     // Look for the first file
     
     searchHandle = FindFirstFile("*.*",&findResults);
     
     // Continue searching the directory for users until all files have been examined
     // or the search handle is invalid (probably no files left to search in that case)
     
     while ( searchHandle != INVALID_HANDLE_VALUE )
     {
           // Open an input stream to the current file
           
           ifstream in(findResults.cFileName,ios::binary | ios::in);
           
           // if opened successfully then read the user data struct from the file
           
           if ( in.is_open() )
           {
              // Declare a user data
              
              UserFile_t userScoreFile;
              
              // Read user score structure from the file
              
              in.read((char*)&userScoreFile,sizeof userScoreFile);
              
              // If IO didnt fail and file id is ok then write this users
              // name and info to the text file
              
              if ( in.good() && userScoreFile.file_id == USER_FILE_ID )
              {
                   // Green to go. Save this users name and score to the html file
              
                   out << "<tr>"
                       << "<td><font color=\"#ffaaaa\">"    << findResults.cFileName   << "</font></td>" 
                       << "<td><font color=\"#ffffff\"><i>" << userScoreFile.bestScore << "</font></td>"
                       << "</tr>"
                       << "<p>";
              }
    
           } // end if file opened successfully
           
           // Close the file opened
           
           in.close();
           
           // Now search for the next file. If not found then set handle to invalid
                  
           if ( ! FindNextFile(searchHandle,&findResults) )
           {
              // File hasn't been found. Set handle to invalid
              
              searchHandle = INVALID_HANDLE_VALUE;
           }

     } // end while data left to write to html file

     /**************************************************************************
     End the html file
     **************************************************************************/     
     
     out  << "</table></body></html>";
     
     /**************************************************************************
     Close up
     **************************************************************************/         

     out.close();
     
     /**************************************************************************
     Set the programs directory back to its normal directory
     **************************************************************************/ 
     
     SetCurrentDirectory("..\\");  
     
     /**************************************************************************
     Run internet explorer to display the file: 
     **************************************************************************/ 
     
     // Declare a string command to hold program to run and command line string
     // Make big enough to hold both!
     
     char command[4096] = {0};
     
     // Add the path and name of internet explorer to the string
     
     strcpy_s(command,4096,IE_PATH);
     
     // Add space to command string to allow for arguments
     
     strcat_s(command,4096," ");
     
     // Declare string to hold path to html file
     
     char htmlpath[2048] = {0};
     
     // Save current program directory in the html path string
     
     GetCurrentDirectory(2048,htmlpath);
     
     // Append a backslash to path
     
     strcat_s(htmlpath,2048,"\\"); 
     
     // Add path from program dir to html path to full html path
     
     strcat_s(htmlpath,2048,HTML_FILE_NAME);       
     
     // Now add the full path name of the html file to the command argument
     
     strcat_s(command,4096,htmlpath);
     
     // Now run IE and show the window
     
     WinExec(command,SW_SHOWNORMAL);
}
