/*
	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:   GuiScreen_Startup
// Purpose:      Startup GUI screen. Displays when the game starts.
//
//
//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################

#include "GuiScreen_Startup.h"
#include "SaveDataFolder.h"

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

Name:     GuiScreen_Startup::GuiScreen_Startup()
Purpose:  Constructor for this gui screen. populates the screen with widgets.
Params:   none
Returns:  n/a

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

GuiScreen_Startup::GuiScreen_Startup()
{
    //--------------------------------------------------------------------------
    // Create the event listeners for the gui screen
    //--------------------------------------------------------------------------

    eventNewGame        = new EventNewGame(this);
    eventLoadGame       = new EventLoadGame(this);
    eventSettings       = new EventSettings(this);
    eventQuit           = new EventQuit(this);

    eventResolution800x600_windowed     = new EventResolution800x600_windowed(this);
    eventResolution800x600_fullscreen   = new EventResolution800x600_fullscreen(this);
    eventResolution1024x768_fullscreen  = new EventResolution1024x768_fullscreen(this);
    eventResolution1280x1024_fullscreen = new EventResolution1280x1024_fullscreen(this);
    eventResolution1600x1200_fullscreen = new EventResolution1600x1200_fullscreen(this);
    eventResolution1440x900_fullscreen  = new EventResolution1440x900_fullscreen(this);
    eventResolution1680x1050_fullscreen = new EventResolution1680x1050_fullscreen(this);
    eventResolution1920x1080_fullscreen = new EventResolution1920x1080_fullscreen(this);

    eventGammaChange =  new EventGammaChange();

    //--------------------------------------------------------------------------
    // Create the main gui panel and set its attributes
    //--------------------------------------------------------------------------

    // Create panel

    mainPanel = new GuiPanel();

    // Set panel size

    mainPanel->setSize( 750 , 550 );

    // center the panel in the middle of the window

	centerPanels();

    // Add the main panel into the gui

    addWidget(mainPanel);

    //--------------------------------------------------------------------------
    // Create the side bar title panel: this is acually an image
    //--------------------------------------------------------------------------

    // Create panel

    GuiImage * sideTitlePanel = new GuiImage();

    // Set image for the panel:

    sideTitlePanel->setImage( const_cast<GLTexture*>(GLTextureManager::getTexMenuTitlePanel()) );

    // Set panel size

    sideTitlePanel->setSize( 256, 64 );

    // Set panel position

    sideTitlePanel->setPosition(8,8);

    // Add the main panel into the gui

    mainPanel->addWidget(sideTitlePanel);

    //--------------------------------------------------------------------------
    // Create a side bar panel to hold the buttons: again an image
    //--------------------------------------------------------------------------

    // Create panel

    GuiImage * sidePanel = new GuiImage();

    // Set image for the panel:

    sidePanel->setImage( const_cast<GLTexture*>(GLTextureManager::getTexMenuButtonPanel()) );

    // Set panel size

    sidePanel->setSize( 256, 512 );

    // Set panel position

    sidePanel->setPosition(8,58);

    // Add the main panel into the gui

    mainPanel->addWidget(sidePanel);

    //--------------------------------------------------------------------------
    // Create the side bar title
    //--------------------------------------------------------------------------

    // Create the label

    GuiLabel * sideTitle = new GuiLabel();

    // Set the label

    sideTitle->setLabel("Main Menu");

    // Get the big font from the font manager

    GLFont * bigFont = GLFontManager::getFont_big();

    // Set the label font

    sideTitle->setFont(bigFont);

    // Set the label font size

    sideTitle->setFontSize(16);

    // Set the label position

    sideTitle->setPosition(32,20);

    // Add it into the side bar panel

    mainPanel->addWidget(sideTitle);

    //--------------------------------------------------------------------------
    // Create the intro panel
    //--------------------------------------------------------------------------

    // Create panel

    panelIntro = new GuiPanel();

    panelIntro->setRaised(false);
    panelIntro->setSize( 526, 534 );
    panelIntro->setPosition(216,8);
    panelIntro->setColor(0.3f,0.3f,0.3f);

    // Create the intro screen image

    GuiImage * introImage = new GuiImage("gfx//titlescreen.tga");

    introImage->setSize(526,534);
    introImage->setPosition(0,0);

    panelIntro->addWidget(introImage);

    // Create the title for this screen

    GuiLabel * introTitle = new GuiLabel();

    introTitle->setFont(bigFont);
    introTitle->setLabel("Rogue Trader");
    introTitle->setFontSize(16);
    introTitle->setPosition(20,15);
    introTitle->setColor(1,1,1);

    panelIntro->addWidget(introTitle);

    // Create a horizontal rule to go under the title

    GuiHRule * introTitleRule = new GuiHRule();

    introTitleRule->setColor(1,1,1);
    introTitleRule->setSize(486,4);
    introTitleRule->setPosition(20,40);

    panelIntro->addWidget(introTitleRule);

    // Create the a label for my name

    GuiLabel * nameLabel = new GuiLabel();

    nameLabel->setFont(bigFont);
    nameLabel->setLabel("By Darragh Coy" );
    nameLabel->setFontSize(16);
    nameLabel->setPosition(20,60);
    nameLabel->setColor(1,1,1);

    panelIntro->addWidget(nameLabel);

    // Create the version label for this screen

    GuiLabel * versionLabel = new GuiLabel();

    versionLabel->setFont(bigFont);
    versionLabel->setLabel("Version: " GAME_VERSION );
    versionLabel->setFontSize(16);
    versionLabel->setPosition(20,505);
    versionLabel->setColor(1,1,1);

    panelIntro->addWidget(versionLabel);

    // Create a horizontal rule to go above this

    GuiHRule * versionRule = new GuiHRule();

    versionRule->setColor(1,1,1);
    versionRule->setSize(486,4);
    versionRule->setPosition(20,490);

    panelIntro->addWidget(versionRule);

    // Add the panel into the main gui panel

    mainPanel->addWidget(panelIntro);

    //--------------------------------------------------------------------------
    // Create the new game panel
    //--------------------------------------------------------------------------

    // Create panel

    panelNewGame = new GuiPanel();

    panelNewGame->setRaised(false);
    panelNewGame->setSize( 526, 534 );
    panelNewGame->setPosition(216,8);
    panelNewGame->setColor(0.3f,0.3f,0.3f);

    // Create the new game panel tiltle:

    GuiLabel * newGameTitle = new GuiLabel();

    newGameTitle->setFont(bigFont);
    newGameTitle->setLabel("Loading..");
    newGameTitle->setFontSize(16);
    newGameTitle->setPosition(20,15);
    newGameTitle->setColor(1,1,1);

    panelNewGame->addWidget(newGameTitle);

    // Create a horizontal rule to go under the title

    GuiHRule * newGameRule = new GuiHRule();

    newGameRule->setColor(1,1,1);
    newGameRule->setSize(486,4);
    newGameRule->setPosition(20,40);

    panelNewGame->addWidget(newGameRule);

    // Add the panel into the main gui panel

    mainPanel->addWidget(panelNewGame);

    //--------------------------------------------------------------------------
    // Create the load game panel
    //--------------------------------------------------------------------------

    // Create panel

    panelLoadGame = new GuiPanel();

    panelLoadGame->setRaised(false);
    panelLoadGame->setSize( 526, 534 );
    panelLoadGame->setPosition(216,8);
    panelLoadGame->setColor(0.3f,0.3f,0.3f);

    // Create the panel tiltle:

    GuiLabel * loadGameTitle = new GuiLabel();

    loadGameTitle->setFont(bigFont);
    loadGameTitle->setLabel("Load game");
    loadGameTitle->setFontSize(16);
    loadGameTitle->setPosition(20,15);
    loadGameTitle->setColor(1,1,1);

    panelLoadGame->addWidget(loadGameTitle);

    // Create a horizontal rule to go under the title

    GuiHRule * loadGameRule = new GuiHRule();

    loadGameRule->setColor(1,1,1);
    loadGameRule->setSize(486,4);
    loadGameRule->setPosition(20,40);

    panelLoadGame->addWidget(loadGameRule);
    
    // Get the paths to the save game files:
    
    std::string save_file_1 = SaveDataFolder::GetSaveFolder("Darragh Coy\\Rogue Trader") + std::string("\\save1.sav");
    std::string save_file_2 = SaveDataFolder::GetSaveFolder("Darragh Coy\\Rogue Trader") + std::string("\\save2.sav");
    std::string save_file_3 = SaveDataFolder::GetSaveFolder("Darragh Coy\\Rogue Trader") + std::string("\\save3.sav");
    std::string save_file_4 = SaveDataFolder::GetSaveFolder("Darragh Coy\\Rogue Trader") + std::string("\\save4.sav");
    std::string save_file_5 = SaveDataFolder::GetSaveFolder("Darragh Coy\\Rogue Trader") + std::string("\\save5.sav");

    // Create a couple of load game widgets to go on this panel:

    GuiLoadGame * loadGame1 = new GuiLoadGame( 0 , save_file_1.c_str() );

    loadGame1->setSize(486,75);
    loadGame1->setPosition(20,60);

    panelLoadGame->addWidget(loadGame1);

    GuiLoadGame * loadGame2 = new GuiLoadGame( 0 , save_file_2.c_str());

    loadGame2->setSize(486,75);
    loadGame2->setPosition(20,140);

    panelLoadGame->addWidget(loadGame2);

    GuiLoadGame * loadGame3 = new GuiLoadGame( 0 , save_file_3.c_str() );

    loadGame3->setSize(486,75);
    loadGame3->setPosition(20,220);

    panelLoadGame->addWidget(loadGame3);

    GuiLoadGame * loadGame4 = new GuiLoadGame( 0 , save_file_4.c_str() );

    loadGame4->setSize(486,75);
    loadGame4->setPosition(20,300);

    panelLoadGame->addWidget(loadGame4);

    GuiLoadGame * loadGame5 = new GuiLoadGame( 0 , save_file_5.c_str() );

    loadGame5->setSize(486,75);
    loadGame5->setPosition(20,380);

    panelLoadGame->addWidget(loadGame5);

    // Add the panel into the main gui panel

    mainPanel->addWidget(panelLoadGame);

    //--------------------------------------------------------------------------
    // Create the settings panel:
    //--------------------------------------------------------------------------

    // Create panel

    panelSettings = new GuiPanel();

    panelSettings->setRaised(false);
    panelSettings->setSize( 526, 534 );
    panelSettings->setPosition(216,8);
    panelSettings->setColor(0.3f,0.3f,0.3f);

    // Create the settings title:

    GuiLabel * settingsTitle = new GuiLabel();

    settingsTitle->setFont(bigFont);
    settingsTitle->setLabel("Settings");
    settingsTitle->setFontSize(16);
    settingsTitle->setPosition(20,15);
    settingsTitle->setColor(1,1,1);

    panelSettings->addWidget(settingsTitle);

    // Create a horizontal rule to go under the title

    GuiHRule * settingsRule = new GuiHRule();

    settingsRule->setColor(1,1,1);
    settingsRule->setSize(486,4);
    settingsRule->setPosition(20,40);

    panelSettings->addWidget(settingsRule);

    // Create a label to go above the resolution buttons:

    GuiLabel * resolutionLabel = new GuiLabel();

    resolutionLabel->setColor(1,1,1);
    resolutionLabel->setPosition(20,60);
    resolutionLabel->setLabel("Resolution");

    panelSettings->addWidget(resolutionLabel);
    
    // Create a label to go above the sound button:
    
    GuiLabel * soundLabel = new GuiLabel();
    
    soundLabel->setColor(1,1,1);
    soundLabel->setPosition(320,60);
    soundLabel->setLabel("Sound");

    panelSettings->addWidget(soundLabel);
    
    // Create a label to go above the music button:
    
    GuiLabel * musicLabel = new GuiLabel();
    
    musicLabel->setColor(1,1,1);
    musicLabel->setPosition(320,140);
    musicLabel->setLabel("Music");

    panelSettings->addWidget(musicLabel);    
    
    // Create the sound on/off button
    
    GuiButton * buttonSoundOnOff = new GuiButton();
    
    buttonSoundOnOff->setSize(180,35);
    buttonSoundOnOff->setPosition(320,80);
    buttonSoundOnOff->setLabel("Enabled");
    
    panelSettings->addWidget(buttonSoundOnOff);
    
    // Create the music on/off button
    
    GuiButton * buttonMusicOnOff = new GuiButton();
    
    buttonMusicOnOff->setSize(180,35);
    buttonMusicOnOff->setPosition(320,160);
    buttonMusicOnOff->setLabel("Enabled");
    
    panelSettings->addWidget(buttonMusicOnOff); 
    
    // Create the event listener for sound on/off and add to button:
    
    eventSoundOnOff = new EventSoundOnOff(buttonSoundOnOff);
    buttonSoundOnOff->setEventListener(eventSoundOnOff);

    // Create the event listener for music on/off and add to button:
    
    eventMusicOnOff = new EventMusicOnOff(buttonMusicOnOff);
    buttonMusicOnOff->setEventListener(eventMusicOnOff);

    // Create the change resolution buttons:

    GuiButton * button800x600_windowed      = new GuiButton();
    GuiButton * button800x600_fullscreen    = new GuiButton();
    GuiButton * button1024x768_fullscreen   = new GuiButton();
    GuiButton * button1280x1024_fullscreen  = new GuiButton();
    GuiButton * button1600x1200_fullscreen  = new GuiButton();
    GuiButton * button1440x900_fullscreen   = new GuiButton();
    GuiButton * button1680x1050_fullscreen  = new GuiButton();
    GuiButton * button1920x1080_fullscreen  = new GuiButton();

    button800x600_windowed->setSize(180,35);
    button800x600_fullscreen->setSize(180,35);
    button1024x768_fullscreen->setSize(180,35);
    button1280x1024_fullscreen->setSize(180,35);
    button1600x1200_fullscreen->setSize(180,35);
    button1440x900_fullscreen->setSize(180,35);
    button1680x1050_fullscreen->setSize(180,35);
    button1920x1080_fullscreen->setSize(180,35);

    button800x600_windowed->setPosition(20,80);
    button800x600_fullscreen->setPosition(20,120);
    button1024x768_fullscreen->setPosition(20,160);
    button1280x1024_fullscreen->setPosition(20,200);
    button1600x1200_fullscreen->setPosition(20,240);
    button1440x900_fullscreen->setPosition(20,280);
    button1680x1050_fullscreen->setPosition(20,320);
    button1920x1080_fullscreen->setPosition(20,360);

    button800x600_windowed->setLabel("800x600 windowed");
    button800x600_fullscreen->setLabel("800x600 fullscreen");
    button1024x768_fullscreen->setLabel("1024x768 fullscreen");
    button1280x1024_fullscreen->setLabel("1280x1024 fullscreen");
    button1600x1200_fullscreen->setLabel("1600x1200 fullscreen");
    button1440x900_fullscreen->setLabel("1440x900 fullscreen");
    button1680x1050_fullscreen->setLabel("1680x1050 fullscreen");
    button1920x1080_fullscreen->setLabel("1920x1080 fullscreen");

    button800x600_windowed->setEventListener(eventResolution800x600_windowed);
    button800x600_fullscreen->setEventListener(eventResolution800x600_fullscreen);
    button1024x768_fullscreen->setEventListener(eventResolution1024x768_fullscreen);
    button1280x1024_fullscreen->setEventListener(eventResolution1280x1024_fullscreen);
    button1600x1200_fullscreen->setEventListener(eventResolution1600x1200_fullscreen);
    button1440x900_fullscreen->setEventListener(eventResolution1440x900_fullscreen);
    button1680x1050_fullscreen->setEventListener(eventResolution1680x1050_fullscreen);    
    button1920x1080_fullscreen->setEventListener(eventResolution1920x1080_fullscreen);
    
    panelSettings->addWidget(button800x600_windowed);
    panelSettings->addWidget(button800x600_fullscreen);
    panelSettings->addWidget(button1024x768_fullscreen);
    panelSettings->addWidget(button1280x1024_fullscreen);
    panelSettings->addWidget(button1600x1200_fullscreen);
    panelSettings->addWidget(button1440x900_fullscreen);
    panelSettings->addWidget(button1680x1050_fullscreen);
    panelSettings->addWidget(button1920x1080_fullscreen);

    // Create the gamma correction label

    GuiLabel * gammaLabel = new GuiLabel();

    gammaLabel->setColor(1,1,1);
    gammaLabel->setPosition(20,480);
    gammaLabel->setLabel("Screen brightness");

    panelSettings->addWidget(gammaLabel);

    // Create the gamma slider:

    GuiSlider * gammaSlider = new GuiSlider();

    gammaSlider->setPosition(20,500);
    gammaSlider->setSize(400,20);
    gammaSlider->setRange(0.5f,1.5f);
    gammaSlider->setValue(1.0f);
    gammaSlider->setEventListener(eventGammaChange);
    eventGammaChange->setSlider(gammaSlider);

    panelSettings->addWidget(gammaSlider);

    // Add the panel into the main gui panel

    mainPanel->addWidget(panelSettings);

    //--------------------------------------------------------------------------
    // Create the side buttons for new game, load game, quit and change resolution
    //--------------------------------------------------------------------------

    // Create all four buttons

    GuiButton * btnNewGame      = new GuiButton();
    GuiButton * btnLoadGame     = new GuiButton();
    GuiButton * btnSettings     = new GuiButton();
    GuiButton * btnQuit         = new GuiButton();

    // Set size of all four buttons

    btnNewGame->setSize(180,35);
    btnLoadGame->setSize(180,35);
    btnSettings->setSize(180,35);
    btnQuit->setSize(180,35);

    // Set position of all four buttons

    btnNewGame->setPosition(18,80);
    btnLoadGame->setPosition(18,120);
    btnSettings->setPosition(18,455);
    btnQuit->setPosition(18,495);

    // Set label of all four buttons

    btnNewGame->setLabel("New Game");
    btnLoadGame->setLabel("Load Game");
    btnSettings->setLabel("Settings");
    btnQuit->setLabel("Quit Program");

    // Set button event listeners

    btnNewGame->setEventListener(eventNewGame);
    btnLoadGame->setEventListener(eventLoadGame);
    btnSettings->setEventListener(eventSettings);
    btnQuit->setEventListener(eventQuit);

    // Add all four buttons into the main panel

    mainPanel->addWidget(btnNewGame);
    mainPanel->addWidget(btnLoadGame);
    mainPanel->addWidget(btnSettings);
    mainPanel->addWidget(btnQuit);

    //--------------------------------------------------------------------------
    // Set the intro panel as the current gui panel shown
    //--------------------------------------------------------------------------

    this->setCurrentPanel_intro();
}

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

Name:     GuiScreen_Startup::~GuiScreen_Startup()
Purpose:  Destructor for this gui screen. frees resources used.
Params:   n/a
Returns:  n/a

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

GuiScreen_Startup::~GuiScreen_Startup()
{
    // Delete the event listeners for the screen

    delete eventNewGame;
    delete eventLoadGame;
    delete eventSettings;
    delete eventQuit;
    delete eventResolution800x600_windowed;
    delete eventResolution800x600_fullscreen;
    delete eventResolution1024x768_fullscreen;
    delete eventResolution1280x1024_fullscreen;
    delete eventResolution1600x1200_fullscreen;
    delete eventResolution1440x900_fullscreen;
    delete eventResolution1680x1050_fullscreen;
    delete eventResolution1920x1080_fullscreen;
    delete eventSoundOnOff;
    delete eventMusicOnOff;
    delete eventGammaChange;
}

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

Name:     GuiScreen_Startup::centerPanels()
Purpose:  Centers the main panel. Useful after a resolution change.
Params:   none
Returns:  none

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

void GuiScreen_Startup::centerPanels()
{
	// Get the effective or virtual resolution of the game:
	float effectiveResX = 0;
	float effectiveResY = 0;
	Game::getEffectiveResolution(effectiveResX, effectiveResY);

	// Center the panel within that 'effective' resolution
    mainPanel->setPosition
    (
        (int) effectiveResX / 2 - mainPanel->getSizeX() / 2,
        (int) effectiveResY / 2 - mainPanel->getSizeY() / 2
    );
}
