/*
	Rogue Trader.
	Copyright (c) 2007, 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:   GuiStarInfo
// Purpose:      Very simple widget that displays information about the currently
//               selected star system in the galaxy. Shows star system name, the
//               type of planet it has and how far away it is.
//
//
//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################

#include "GuiStarInfo.h"

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Name:     GuiStarInfo::GuiStarInfo()
Purpose:  Constructo for this widget.
Params:   Pointer to a galactic map widget, pointer to the game
Returns:  n/a

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

GuiStarInfo::GuiStarInfo( GuiGalaxyMap * galacticMap , Game * game )
{
    // Save a pointer the galactic map widget

    this->galacticMap = galacticMap;

    // Save the pointer to the game object

    this->game = game;
}


/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Name:     GuiStarInfo::windowRedrawing()
Purpose:  Window redrawing event. Draws the information for the currently selected
          star system in the galactic map.
Params:   none
Returns:  none

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

void GuiStarInfo::windowRedrawing()
{
    //--------------------------------------------------------------------------
    // Clear the widget background to black:
    //--------------------------------------------------------------------------

    glDisable(GL_TEXTURE_2D);
    glDisable(GL_BLEND);
    glColor4f(0,0,0,1);

    glBegin(GL_QUADS);

        glVertex2f( float( this->position_x					)	, float( this->position_y					) );
        glVertex2f( float( this->position_x + this->size_x  )	, float( this->position_y					) );
        glVertex2f( float( this->position_x + this->size_x  )	, float( this->position_y + this->size_y	) );
        glVertex2f( float( this->position_x                 )	, float( this->position_y + this->size_y	) );

    glEnd();

    //--------------------------------------------------------------------------
    // Get the currently selected star system in the galaxy map:
    //--------------------------------------------------------------------------

    // Get the currently selected star system from the galactic map:

    StarSystem system = galacticMap->getSelectedSystem();

    // If the system is a null system with no name then do not draw anything:

    if ( system.isNullSystem() ) return;

    //--------------------------------------------------------------------------
    // Setup the opengl state for drawing the planet:
    //--------------------------------------------------------------------------

    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);

    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

    glColor4f(1,1,1,1);

    //--------------------------------------------------------------------------
    // Draw a picture of this systems planet:
    //--------------------------------------------------------------------------

    // Get the planet texture from the system and bind the texture:

    system.getPlanetTexture()->bind();

    // Draw the planet centered horizontally:

    glBegin(GL_QUADS);

        glTexCoord2f(0,0);  glVertex2f( float( this->position_x + this->size_x / 2 - 32	)	, float( this->position_y + 32 )	);
        glTexCoord2f(1,0);  glVertex2f( float( this->position_x + this->size_x / 2 + 32 )	, float( this->position_y + 32 )	);
        glTexCoord2f(1,1);  glVertex2f( float( this->position_x + this->size_x / 2 + 32 )	, float( this->position_y + 96 )	);
        glTexCoord2f(0,1);  glVertex2f( float( this->position_x + this->size_x / 2 - 32 )	, float( this->position_y + 96 )	);

    glEnd();

    //--------------------------------------------------------------------------
    // Draw the star systems name:
    //--------------------------------------------------------------------------

    // Get the main game font from the font manager:

    GLFont * font = GLFontManager::getFont_main();

    // Get the lenght of the star name:

    int lenght = strlen( system.getName() );

    // Draw the name of the star centered horizontally:

    font->drawString
    (
        system.getName()											,
        float( this->position_x + this->size_x / 2 - lenght * 4	)	,
        float( this->position_y + 8								)	,
        8.0f	,
        1.0f	,
        1.0f	,
        1.0f	
    );

    //--------------------------------------------------------------------------
    // Draw whether the currently selected system is within the hyperspace
    // range of the player:
    //--------------------------------------------------------------------------

    // Get a hook to the players ship from the game:

    const GameObjectHook * hookShip = game->getPlayer()->getShipHook();

    // Get the players ship from the hook: assume this game object is a ship

    GameObject_Ship * ship = (GameObject_Ship*)(hookShip->getObject());

    // If the players ship no longer exists then abort:

    if ( ship == 0 ) return;

    // Otherwise get the hyperspace range of the players ship:

    double range = ship->getHyperspaceRange();

    // Get the distance from the galactic map:

    double distance = galacticMap->getDistanceToSelectedSystem();

    // Now see if this star is reachable from where the player is:

    if ( distance == 0 )
    {
        // Get the lenght of the string that will be displayed:

        int lenght = strlen("Within system");

        // Draw this string to the screen

        font->drawString
        (
            "Within system"												,
            float( this->position_x + this->size_x / 2 - lenght * 4	)	,
            float( this->position_y + 104							)	,
            8.0f	,
            1.0f	,
            1.0f	,
            1.0f
        );

    }
    else if ( distance <= range )
    {
        // Get the lenght of the string that will be displayed:

        int lenght = strlen("In range");

        // Draw this string to the screen

        font->drawString
        (
            "In range"													,
            float( this->position_x + this->size_x / 2 - lenght * 4	)	,
            float( this->position_y + 104							)	,
            8.0f	,
            1.0f	,
            1.0f	,
            1.0f
        );
    }
    else
    {
        // Get the lenght of the string that will be displayed:

        int lenght = strlen("Out of range");

        // Draw this string to the screen

        font->drawString
        (
            "Out of range"												,
            float( this->position_x + this->size_x / 2 - lenght * 4	)	,
            float( this->position_y + 104							)	,
            8.0f	,
            1.0f	,
            1.0f	,
            1.0f
        );
    }

    //--------------------------------------------------------------------------
    // Draw the danger level for the system:
    //--------------------------------------------------------------------------

    const char * dangerLevelString;

    // Decide what string to show:

    switch ( system.getDangerLevel() )
    {
        case 1:     dangerLevelString = "Peaceful"          ; break;
        case 2:     dangerLevelString = "Mostly quiet"      ; break;
        case 3:     dangerLevelString = "Some danger"       ; break;
        case 4:     dangerLevelString = "Dangerous"         ; break;
        case 5:     dangerLevelString = "Lawless"           ; break;
        default:    dangerLevelString = "Unknown danger"    ; break;
    }

    // Decide what color to show the string in depending on the danger level:

    float dangerColor[3];

    switch ( system.getDangerLevel() )
    {
        case 1:     dangerColor[0] = 0.0f; dangerColor[1] = 1.0f; dangerColor[2] = 0.0f; ; break;
        case 2:     dangerColor[0] = 0.4f; dangerColor[1] = 0.7f; dangerColor[2] = 0.0f; ; break;
        case 3:     dangerColor[0] = 0.5f; dangerColor[1] = 0.5f; dangerColor[2] = 0.0f; ; break;
        case 4:     dangerColor[0] = 0.7f; dangerColor[1] = 0.4f; dangerColor[2] = 0.0f; ; break;
        case 5:     dangerColor[0] = 1.0f; dangerColor[1] = 0.0f; dangerColor[2] = 0.0f; ; break;
        default:    dangerColor[0] = 0.0f; dangerColor[1] = 1.0f; dangerColor[2] = 0.0f; ; break;
    }

    // Get the lenght of the string that will be displayed:

    lenght = strlen(dangerLevelString);

    // Draw this string to the screen

    font->drawString
    (
        dangerLevelString											,
        float( this->position_x + this->size_x / 2 - lenght * 4	)	,
        float( this->position_y + 120							)	,
        8.0f			,
        dangerColor[0]	,
        dangerColor[1]	,
        dangerColor[2]
    );
}
