/*
	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:   GuiCargoInfo
// Purpose:      This widget displays information about the cargo in the players
//               ship.
//
//
//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################

#include "GuiCargoInfo.h"

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

Name:     GuiCargoInfo::GuiCargoInfo()
Purpose:  Constructor for this widget
Params:   Pointer to the game
Returns:  n/a

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

GuiCargoInfo::GuiCargoInfo( Game * game )
{
    // Save the pointer to the game:

    this->game = game;
}

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

Name:     GuiCargoInfo::windowRedrawing()
Purpose:  Redraw event for this widget. Draws the information about the cargo
          present in the players ship
Params:   none
Returns:  none

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

void GuiCargoInfo::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 players ship from the game:
    //--------------------------------------------------------------------------

    // Get the ship object and assume it actually is a ship in the first place:

    GameObject_Ship * playerShip = (GameObject_Ship*)( game->getPlayer()->getShipHook()->getObject() );

    // If the ship does not exist then abort:

    if ( playerShip == 0 ) return;

    //--------------------------------------------------------------------------
    // Get the cargo inventory of the ship:
    //--------------------------------------------------------------------------

    GoodsInventory * cargoInventory = playerShip->getGoodsInventory();

    //--------------------------------------------------------------------------
    // Get the big font from the font manager for drawing text:
    //--------------------------------------------------------------------------

    GLFont * font = GLFontManager::getFont_big();

    //--------------------------------------------------------------------------
    // Draw two titles, one for the name of each good and one for the amount:
    //--------------------------------------------------------------------------

    font->drawString
    (
        "Item"								,
        float( this->position_x + 10	)	,
        float( this->position_y + 10	)	,
        16.0f	,
        1.0f	,
        1.0f	,
        1.0f	
    );

    font->drawString
    (
        "Units"								,
        float( this->position_x + 256	)	,
        float( this->position_y + 10	)	,
        16.0f	,
        1.0f	,
        1.0f	,
        1.0f	
    );

    //--------------------------------------------------------------------------
    // Draw a rule under these two titles:
    //--------------------------------------------------------------------------

    // Set rule size and position first

    rule.setSize( this->size_x - 20 , 4 );
    rule.setPosition( this->position_x + 10 , this->position_y + 32 );

    // Draw the rule:

    rule.windowRedrawing();

    //--------------------------------------------------------------------------
    // Set the font used to the main game font:
    //--------------------------------------------------------------------------

    font = GLFontManager::getFont_main();

    //--------------------------------------------------------------------------
    // Draw a list of the items the player has on board and the amount of each item:
    //--------------------------------------------------------------------------

    // Start drawing at this y offset from the widget position:

    int offsetY = 48;

    // Draw every item that the amount the player has on board:

    for ( unsigned i = 0 ; i < GoodsInventory::NUM_ITEMS ; i++ )
    {
        // Draw the name of this item first:

        font->drawString
        (
            cargoInventory->getItemName(i)			,
            float( this->position_x + 10		)	,
            float( this->position_y + offsetY	)	,
            8.0f	,
            1.0f	,
            1.0f	,
            1.0f	
        );

        // Print the amount of units the player has into a buffer

        char buffer[16]; sprintf_s( buffer , 16 , "%1u" , cargoInventory->getItemAmount(i) );

        // Draw the amount of units the player has of this item to the screen

        font->drawString
        (
            buffer									,
            float( this->position_x + 256		)	,
            float( this->position_y + offsetY	)	,
            8.0f	,
            1.0f	,
            1.0f	,
            1.0f	
        );

        // Move onto the next line for drawing the next good the player has

        offsetY += 12;

    }   // end for each item draw how much the player has of it

    //--------------------------------------------------------------------------
    // Draw a rule underneath all of this informatin
    //--------------------------------------------------------------------------

    // Move on the current y offset within the widget to start drawing first:

    offsetY += 8;

    // Set the rule position

    rule.setPosition( this->position_x + 10 , this->position_y + offsetY );

    // Draw the rule

    rule.windowRedrawing();

    //--------------------------------------------------------------------------
    // Draw the cargo icon:
    //--------------------------------------------------------------------------

    // Move on the current y offset within the widget to start drawing first:

    offsetY += 24;

    // Setup the gl state

    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

    // Bind the cargo texture

    GLTextureManager::getTexCargo()->bind();

    // Draw the icon

    glBegin(GL_QUADS);

        glColor4f(1,1,1,1);

        glTexCoord2f(0,0);  glVertex2f( float( this->position_x + 16 ), float( this->position_y + offsetY		) );
        glTexCoord2f(1,0);  glVertex2f( float( this->position_x + 48 ), float( this->position_y + offsetY		) );
        glTexCoord2f(1,1);  glVertex2f( float( this->position_x + 38 ), float( this->position_y + offsetY + 32	) );
        glTexCoord2f(0,1);  glVertex2f( float( this->position_x + 16 ), float( this->position_y + offsetY + 32	) );

    glEnd();

    //--------------------------------------------------------------------------
    // Draw the cargo held and the capacity
    //--------------------------------------------------------------------------

    // Get the big font from the font manager

    GLFont * bigFont = GLFontManager::getFont_big();

    // Print the cargo capacity and how many items there are into a buffer:

    char buffer[32];

    sprintf_s
    (
        buffer								,
        32									,
        "%1u / %2u"							,
        cargoInventory->getTotalAmount()	,
        playerShip->getCargoCapacity()
    );

    // Draw this amount:

    bigFont->drawString
    (
        buffer									,
        64.0f									,
        float( this->position_y + offsetY + 8 )	,
        16.0f	,
        1.0f	,
        1.0f	,
        1.0f
    );

    //--------------------------------------------------------------------------
    // Draw a rule under all this
    //--------------------------------------------------------------------------

    // Move on the current y offset within the widget to start drawing first:

    offsetY += 48;

    // Draw the rule

    rule.setSize( this->size_x - 20 , 4 );
    rule.setPosition( this->position_x + 10 , this->position_y + offsetY );
    rule.windowRedrawing();

    //--------------------------------------------------------------------------
    // Draw the 3D cargo model using the mesh viewer underneath this:
    //--------------------------------------------------------------------------

    // Move on the current y offset within the widget to start drawing first:

    offsetY += 12;

    // Draw the mesh viewer

    meshViewer.setSize(470,this->size_y - offsetY - 8);
    meshViewer.setPosition(8,offsetY);
    meshViewer.setMesh(MeshManager::getMeshCargo());
    meshViewer.setAutoRotatePitch(false);
    meshViewer.setPitch(0.5);
    meshViewer.setZoom(1.00);
    meshViewer.windowRedrawing();
}
