/*
	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:   GuiMarketInfo
// Purpose:      Simple widget that shows market information for a selected
//               star system. Just displays a list of goods and their current
//               prices.
//
//
//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################

#include "GuiMarketInfo.h"

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

Name:     GuiMarketInfo::GuiMarketInfo()
Purpose:  Constructo for this widget.
Params:   Pointer to a galactic map widget
Returns:  n/a

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

GuiMarketInfo::GuiMarketInfo( Game * game, GuiGalaxyMap * galacticMap )
{
    // Save pointer to the game

    this->game = game;

    // Save a pointer the galactic map widget

    this->galacticMap = galacticMap;
}

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

Name:     GuiMarketInfo::windowRedrawing()
Purpose:  Redraw event for this widget. Draws market information if a system
          is selected on the galactic map.
Params:   none
Returns:  none

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

void GuiMarketInfo::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 from the map:
    //--------------------------------------------------------------------------

    // Get the system:

    StarSystem system = galacticMap->getSelectedSystem();

    // If no system is selected (system is null) then do not draw anything further:

    if ( system.isNullSystem() ) return;

    // See if the system selected is the system the player is currently in:

    bool playerSystemSelected = false;

    if ( system == * game->getSystem() ) playerSystemSelected = true;

    //--------------------------------------------------------------------------
    // Draw the field headers for each good and a rule underneath them:
    //--------------------------------------------------------------------------

    // Get the main game font first:

    GLFont * font = GLFontManager::getFont_main();

    // Draw the headers:

        // Good name:

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

        // Demand:

        font->drawString
        (
            "Supply"							,
            float( this->position_x + 120	)	,
            float( this->position_y + 10	)	,
            8.0f	,
            1.0f	,
            1.0f	,
            1.0f	
        );

        // Draw buy price if the current system selected is the players system, draw sell price otherwise

        if ( playerSystemSelected )
        {
            font->drawString
            (
                "Buy price"							,
                float( this->position_x + 256	)	,
                float( this->position_y + 10	)	,
                8.0f	,
                1.0f	,
                1.0f	,
                1.0f
            );
        }
        else
        {
            font->drawString
            (
                "Sell price"						,
                float( this->position_x + 256	)	,
                float( this->position_y + 10	)	,
                8.0f	,
                1.0f	,
                1.0f	,
                1.0f
            );
        }

    // Set the size and position of the headings rule:

    headingRule.setPosition(10,25);
    headingRule.setSize(this->size_x - 20 , 2 );

    // Draw the heading rule

    headingRule.windowRedrawing();

    //--------------------------------------------------------------------------
    // Draw the names and prices of each good in this system:
    //--------------------------------------------------------------------------

    // Get the goods inventory for the system:

    GoodsInventory & goods = system.getGoodsInventory();

    // Draw the information for each good

    for ( unsigned i = 0 ; i < GoodsInventory::NUM_ITEMS ; i++ )
    {
        // Draw the good name:

        font->drawString
        (
            goods.getItemName(i)						,
            float( this->position_x + 10			)	,
            float( this->position_y + 35 + i * 12	)	,
            8.0f	,
            1.0f	,
            1.0f	,
            1.0f
        );

        // Get how much of the good there is:

        unsigned stock = goods.getItemAmount(i);

        // If stock is over twice the normal supply then clamp for visual purposes:

        if ( stock > GoodsInventory::NORMAL_SUPPLY * 2 ) stock = GoodsInventory::NORMAL_SUPPLY * 2;

        // Figure out what color to show the supply bar in from the level of stock

        float color[3] =
        {
            1.0f - float(stock) / float( GoodsInventory::NORMAL_SUPPLY * 2.0f ),
            0.0f + float(stock) / float( GoodsInventory::NORMAL_SUPPLY * 2.0f ),
            0.0f
        };

        // Draw a rectangle indicating the supply of a good there is

        glColor4f( color[0], color[1], color[2], 1 );

        glRecti
        (
            this->position_x + 120,
            this->position_y + 35  + i * 12,
            this->position_x + 120 + int( ( stock / float( GoodsInventory::NORMAL_SUPPLY * 2.0f ) ) * 100.0f ),
            this->position_y + 35  + i * 12 + 8
        );

        // Print the buy price of the good to a string buffer if the player is in this system, print sell price otherwise:

        char buffer[32];

        if ( playerSystemSelected )
        {
            sprintf_s( buffer , 32 , "Cr %1.2f" , goods.getItemBuyPrice(i) );
        }
        else
        {
            sprintf_s( buffer , 32 , "Cr %1.2f" , goods.getItemSellPrice(i) );
        }

        // Draw the sell or buy price of the good:

        font->drawString
        (
            buffer										,
            float( this->position_x + 256			)	,
            float( this->position_y + 35 + i * 12	)	,
            8.0f	,
            1.0f	,
            1.0f	,
            1.0f
        );
    }
}
