/*
	Wheels Of Steel
	Copyright (c) 2006, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL DARRAGH COY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################
//
//
// Class Name:   Menu_EndRace
// Purpose:      Just displays the end of race results and waits for the enter key
//               until returning.
//
//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################

#include "Menu_EndRace.h"
#include <cstdio>

//------------------------------------------------------------------------------
// Static initialisers
//------------------------------------------------------------------------------

bool            Menu_EndRace::runMenu           = 0;   // Run the window and consequently the menu ?
int             Menu_EndRace::playerFinishPos   = 0;   // the players finishing position
char *          Menu_EndRace::screenTitle       = 0;   // Title of the end race screen
char *          Menu_EndRace::positionString    = 0;   // String showing what position the player came in
Bitmap *        Menu_EndRace::bmpEndRace        = 0;   // image to display in center of screen
Bitmap *        Menu_EndRace::bmpMenuBar        = 0;   // The menu bar horizontal rule divider
BitmapFont *    Menu_EndRace::bitFont           = 0;   // Bitmap font to use for the screen

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Name:     Menu_EndRace::display
Purpose:  starts showing the end of race menu.
Params:   as commented.
Returns:  none

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void Menu_EndRace::display
(
    int         playerFinishPos,    // position the player finished in
    Window *    window              // the games window to draw to.
)

{
    //--------------------------------------------------------------------------
    // Save the players position
    //--------------------------------------------------------------------------

    Menu_EndRace::playerFinishPos = playerFinishPos;

    //--------------------------------------------------------------------------
    // Show the menu initially
    //--------------------------------------------------------------------------

    runMenu = true;

    //--------------------------------------------------------------------------
    // Load the menu bar resource
    //--------------------------------------------------------------------------

    bmpMenuBar = window->loadBitmap("gfx\\menu\\menuBar1.bmp",true);

    //--------------------------------------------------------------------------
    // Load the bitmap font used for the end of race screen
    //--------------------------------------------------------------------------

    bitFont = new BitmapFont("gfx\\font\\font1sm.bmp",window);

    //--------------------------------------------------------------------------
    // Depending on if the player came first, quit or came in some arbitrary grid
    // position, load the appropriate image to display in the center of the screen
    //--------------------------------------------------------------------------

    if ( playerFinishPos <= 0 )
    {
        //----------------------------------------------------------------------
        // Player didnt finish the race
        //----------------------------------------------------------------------

        bmpEndRace  = window->loadBitmap("gfx\\menu\\raceQuit.bmp",false);
        screenTitle = "Feeling a bit peckish?";

    }
    else if ( playerFinishPos == 1 )
    {
        //----------------------------------------------------------------------
        // Player came first
        //----------------------------------------------------------------------

        bmpEndRace = window->loadBitmap("gfx\\menu\\raceWon.bmp",false);
        screenTitle = "You rock! Lets celebrate!";
    }
    else
    {
        //----------------------------------------------------------------------
        // Player came in some other position
        //----------------------------------------------------------------------

        bmpEndRace = window->loadBitmap("gfx\\menu\\raceLost.bmp",false);
        screenTitle = "Too slow! Try again.";
    }

    //--------------------------------------------------------------------------
    // Make up the position string showing the position of the player
    //--------------------------------------------------------------------------

    // Declare and init the string

    positionString = new char[256]; for (int i = 0 ; i < 256 ; i++ ) positionString[i] = 0;

    // See if player finished race or not

    if ( playerFinishPos <= 0 )
    {
        //----------------------------------------------------------------------
        // Player didn't finish race
        //----------------------------------------------------------------------

        strcat_s(positionString,256,"You did not finish!");
    }
    else
    {
        //----------------------------------------------------------------------
        // Player finished the race
        //----------------------------------------------------------------------

        strcat_s(positionString,256,"You came ");

        // Convert the position number to a sring and concatenate onto the text
        
        char pos_num_string[32] = {0}; sprintf_s(pos_num_string,32,"%i",playerFinishPos);
        
        // Add onto the position string:
        
        strcat_s(positionString,256,pos_num_string);

        //----------------------------------------------------------------------
        // Decide what postfix text to concatenate onto the position number
        //----------------------------------------------------------------------

        if ( playerFinishPos % 10 == 1 && ( playerFinishPos < 10 || playerFinishPos > 20 ) )
        {
            // in position-st position

            strcat_s(positionString,256,"st");
        }
        else if ( playerFinishPos % 10 == 2 && ( playerFinishPos < 10 || playerFinishPos > 20 ) )
        {
            // in position-nd position

            strcat_s(positionString,256,"nd");
        }
        else if ( playerFinishPos % 10 == 3 && ( playerFinishPos < 10 || playerFinishPos > 20 ) )
        {
            // in position-rd position

            strcat_s(positionString,256,"rd");
        }
        else
        {
            // in position-th position

            strcat_s(positionString,256,"th");
        }

        //----------------------------------------------------------------------
        // Add a full stop to the end
        //----------------------------------------------------------------------

        strcat_s(positionString,256,".");
    }

    //--------------------------------------------------------------------------
    // Set the paint and window closed event of the window
    //--------------------------------------------------------------------------

    window->setEvent_paint(event_draw);
    window->setEvent_keyDown(event_keyDown);
    window->setEvent_closed(event_closed);

    //--------------------------------------------------------------------------
    // Continue running the window until the 'runMenu' flag is false
    //--------------------------------------------------------------------------

    while ( runMenu )
    {
        // Request a repaint of the window.

        window->requestRepaint();

        // do message processing of the window

        window->run();
    }

    //--------------------------------------------------------------------------
    // Disengage the event handlers
    //--------------------------------------------------------------------------

    window->setEvent_paint(0);
    window->setEvent_keyDown(0);
    window->setEvent_closed(0);

    //--------------------------------------------------------------------------
    // Free all resources allocated
    //--------------------------------------------------------------------------

    delete bmpMenuBar;          bmpMenuBar      = 0;
    delete bmpEndRace;          bmpEndRace      = 0;
    delete bitFont;             bitFont         = 0;
    delete positionString;      positionString  = 0;

}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Name:     Menu_EndRace::event_keyDown
Purpose:  Stops running the menu if enter was pressed.
Params:   Window
Returns:  none

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

// No unreferenced param warning

#pragma warning(disable:4100)

void Menu_EndRace::event_keyDown ( Window & window , Window::KeyboardKey key )
{
    // If the user has hit the enter key then stop showing the menu

    if ( key == Window::KEYBOARD_RETURN )
    {
        // Stop showing the menu

        runMenu = false;
    }
}

// Restore this warning to normal

#pragma warning(default:4100)

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Name:     Menu_EndRace::event_closed
Purpose:  Just stops the execution of the window after it is closed.
Params:   Window
Returns:  none

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

// No unreferenced param warning

#pragma warning(disable:4100)

void Menu_EndRace::event_closed ( Window & window )
{
    // Stop running the menu

    runMenu = false;
}

// Restore this warning to normal

#pragma warning(default:4100)

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Name:     Menu_EndRace::event_draw
Purpose:  Draws the menu screen.
Params:   Window to paint to.
Returns:  none

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void Menu_EndRace::event_draw( Window & window )
{
    // Clear the screen to black

    window.clear(0,0,0);

    // Figure out the coordinates of the end of race image. Tis centered in the middle of the screen.

    int lx = Game::RESOLUTION_X / 2 - bmpEndRace->getWidth()  / 2;   // left    x of image
    int rx = Game::RESOLUTION_X / 2 + bmpEndRace->getWidth()  / 2;   // right   x of image
    int ty = Game::RESOLUTION_Y / 2 - bmpEndRace->getHeight() / 2;   // top     y of image
    int by = Game::RESOLUTION_Y / 2 + bmpEndRace->getHeight() / 2;   // bottom  y of image

    // Draw the end of race image centered in the middle of the screen

    window.drawBitmap
    (
        bmpEndRace, // bitmap to draw
        lx,         // x pos
        ty          // y pos
    );

    // Draw a 3 pixel gradient border around the image

    window.setLineWidth(1);
    window.setLinePattern(Window::LINE_PATTERN_NORMAL);

    window.setLineColor(150,150,150);

    window.drawLine(lx,ty,rx,ty);
    window.drawLine(rx,ty,rx,by+1);
    window.drawLine(lx,by,rx+1,by);
    window.drawLine(lx,ty,lx,by);

    window.setLineColor(100,100,100);

    window.drawLine(lx-1,ty-1,rx+1,ty-1);
    window.drawLine(rx+1,ty-1,rx+1,by+2);
    window.drawLine(lx-1,by+1,rx+2,by+1);
    window.drawLine(lx-1,ty-1,lx-1,by+1);

    window.setLineColor(60,60,60);

    window.drawLine(lx-2,ty-2,rx+2,ty-2);
    window.drawLine(rx+2,ty-2,rx+2,by+3);
    window.drawLine(lx-2,by+2,rx+3,by+2);
    window.drawLine(lx-2,ty-2,lx-2,by+2);

    // Draw the title of the screen centered

    bitFont->drawText
    (
        screenTitle,
        Game::RESOLUTION_X / 2 - ( strlen(screenTitle) * bitFont->getWidth() ) / 2,
        bitFont->getHeight(),
        &window
    );

    // Draw a the menu bar under the title

    window.drawBitmap
    (
        bmpMenuBar,
        0,
        bitFont->getHeight() * 3
    );

    // Draw a the menu bar above the position string the title

    window.drawBitmap
    (
        bmpMenuBar,
        0,
        Game::RESOLUTION_Y - bitFont->getHeight() * 5
    );

    // Draw the position string

    bitFont->drawText
    (
        positionString,
        Game::RESOLUTION_X / 2 - ( strlen(positionString) * bitFont->getWidth() ) / 2,
        Game::RESOLUTION_Y - bitFont->getHeight() * 3,
        &window
    );

    // Draw the 'press enter to continue' prompt

    bitFont->drawText
    (
        "Press enter to continue.",
        Game::RESOLUTION_X / 2 - ( strlen("Press enter to continue.") * bitFont->getWidth() ) / 2,
        Game::RESOLUTION_Y - bitFont->getHeight() * 1,
        &window
    );
}
