//////////////////////////////////////////////////////////////////////////////////////////
//
//  FILE        :  Tools_Mode_Extended_Weather.cpp
//
//  CREATED     :  Adam A. Brown  (19.6.2003)
//
//  Copyright (C) 2003 Rebellion, All Rights Reserved.
//
//////////////////////////////////////////////////////////////////////////////////////////


#include "Core/Asura.h"

// This file's header.
#include "Weather/Tools_Mode_Extended_Weather.h"


// asura
#include "Camera/Asura_Camera.h"
#include "Core/Asura_LOS.h"
#include "Core/Asura_UserInput.h"
#include "Core/Asura_Maths.h"
#include "Core/Asura_Timers.h"
#include "Core/Asura_TextureManagement.h"
#include "Debugging/Asura_Assert.h"
#include "Entity/Asura_Entity.h"
#include "Interface/Asura_Overlay_System.h"
#include "Interface/Asura_Menubar_System.h"
#include "Interface/Asura_Menubar.h"
#include "Interface/Asura_Overlay.h"
#include "Interface/Asura_Cursor.h"
#include "Interface/Asura_Interface.h"
#include "Interface/Asura_Dialog.h"
#include "Mesh/Asura_Mesh.h"
#include "Rendering/Asura_Module.h"
#include "Rendering/Asura_Surface.h"
#include "Rendering/Asura_Render.h"
#include "Rendering/Asura_Viewport.h"
#include "Rendering/Asura_Text_Output.h"
#include "Rendering/Asura_Font_System.h"
#include "SpecialFX///Asura_Weather_System.h"

// tools
#include "Tools_Overlays.h"
#include "Entity/Tools_Entity_Data.h"
#include "Load&Save/Tools_ModeDataFile.h"
#include "Load&Save/Tools_SaveAsura.h"
#include "Misc/Tools_Timed_Messages.h"
#include "Misc/Tools_Help.h"
#include "Misc/Tools_Render.h"
#include "Misc/Tools_Modal_Overlay.h"
#include "Objects/Tools_Object_System.h"
#include "Objects/Tools_Object.h"
#include "Weather/Tools_Extended_Particle_Rain_Shelter.h"



//////////////////
// Help Strings //
//////////////////
//static const char* Weather_ModeHelpStrings[] =
//{
//    "C - Create Rain Shelter",
//    NULL
//};

/////////////////////////////
// Static member variables //
/////////////////////////////
Asura_Vector_2  Tools_Mode_Extended_Weather::s_xWeatherOverlayPosition = Asura_Vector_2(3.0f,28.0f);
bool            Tools_Mode_Extended_Weather::s_bWeatherOverlayOpen = true;
Asura_Handle    Tools_Mode_Extended_Weather::s_hMenuHandle;
bool			Tools_Mode_Extended_Weather::s_bMadeChanges = false;

bool Tools_Mode_Extended_Weather::s_bRaining = false;
bool Tools_Mode_Extended_Weather::s_b2DEffect = false;

Tools_Mode_Extended_Weather* Tools_Mode_Extended_Weather::s_pxThis = NULL;

//TOOLS_WEATHER_SUBMODES  Tools_Mode_Extended_Weather::s_eSubMode  = TOOLS_WEATHER_SUBMODE_NONE;




//------------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
//
// Tools_Mode_Extended_Weather
//
////////////////////////////////////////////////////////////////////////////////
//------------------------------------------------------------------------------

////////////////////////////////////////////////////////////////////////////////
//
// Initialise Mode
//
////////////////////////////////////////////////////////////////////////////////
void Tools_Mode_Extended_Weather::InitialiseMode ( void )
{
    // Create the menubar
    s_hMenuHandle = Asura_Menubar_System::CreateMenuSection();
    Asura_Menubar_Title* title = Asura_Menubar_System::AddTitle(s_hMenuHandle, "Overlays");

    title->AddItem("Extended Weather", ToggleWeatherOverlay, WeatherOverlayIsOn );

    // Load values from .dat file
    TransferModeData( TOOLSMODEDATAFILE_LOAD );

    // Create overlays
    CreateWeatherOverlay();

    Tools_Object_System::EnableAllControls(true);

    // create tools objects for all the currently existing rain shelters
	for( int i = 0; i < Asura_Extended_Particle_Rain_System::GetNumberOfRainShelters(); i++)
	{
		Asura_Extended_Particle_Rain_Shelter *pxShelter = Asura_Extended_Particle_Rain_System::GetRainShelter(i);

		if(pxShelter != NULL)
		{
			new Tools_Extended_Particle_Rain_Shelter(pxShelter);
		}
	}

    s_bMadeChanges = false;
	s_bRaining = Asura_Extended_Particle_Rain_System::IsRaining();
	s_b2DEffect = Asura_Extended_Particle_Rain_System::Is2DEffectEnabled();

    // Add help
    //Tools_Help::AddHelpTopic("Weather Mode",Weather_ModeHelpStrings);

    //g_bSunGlareEnabled = //Asura_Weather_System::IsSunGlareEnabled();
	//g_bLensFlareEnabled = //Asura_Weather_System::IsLensFlareEnabled();
}




////////////////////////////////////////////////////////////////////////////////
//
// ShutdownMode
//
////////////////////////////////////////////////////////////////////////////////
void Tools_Mode_Extended_Weather::ShutdownMode ( void )
{
    // Exit the sub mode
    //ExitMode();

    //Tools_Help::RemoveHelpTopic("Weather Mode");

    // Delete all the objects
    Tools_Object_System::DeleteAllModeSpecific();

    // Save values to .dat file
    TransferModeData(TOOLSMODEDATAFILE_SAVE);
    
    // Shutdown the menu
    if(s_hMenuHandle != ASURA_INVALID_HANDLE)
    {
        Asura_Menubar_System::DestroyMenuSection(s_hMenuHandle);
        s_hMenuHandle = ASURA_INVALID_HANDLE;
    }

    // Close overlays
    Asura_Overlay_System::CloseOverlays( TOOLS_OVERLAY_WEATHER );

    Tools_Object_System::EnableAllControls(false);
    
}




////////////////////////////////////////////////////////////////////////////////
//
// TransferModeData
//
////////////////////////////////////////////////////////////////////////////////
void Tools_Mode_Extended_Weather::TransferModeData( int iMode )
{
    // Open the file
    Tools_ModeDataFile file("ExtendedWeatherMode.dat", iMode);

    // Transfer the data
    file.Transfer(s_xWeatherOverlayPosition);
    file.Transfer(s_bWeatherOverlayOpen);
}



////////////////////////////////////////////////////////////////////////////////
//
// Update
//
////////////////////////////////////////////////////////////////////////////////
void Tools_Mode_Extended_Weather::Update( void )
{
	//Keyboard input
    HandleKeyboard();
    
    if(s_bMadeChanges)
    {
		//TODO
        //1: Dirty savedata.
		Tools_SaveAsura::SetSkyboxModified();
		
		//2: Transfer data.
		Asura_Extended_Particle_Rain_System::EnableRain(s_bRaining);
		Asura_Extended_Particle_Rain_System::Enable2DEffect(s_b2DEffect);

        s_bMadeChanges = false;
    }
}



////////////////////////////////////////////////////////////////////////////////
//
// Render
//
////////////////////////////////////////////////////////////////////////////////

//void Tools_Mode_Extended_Weather::Render ( void )
//{
//    
//}



//#include "Asura_SaveBlock.h"
//#include "Asura_Chunk_WeatherSystem.h"


////////////////////////////////////////////////////////////////////////////////
//
// QuickSave
//
////////////////////////////////////////////////////////////////////////////////
void Tools_Mode_Extended_Weather::QuickSave ( void )
{
    Tools_SaveAsura::SaveSkybox();
    if(!IsModified())
    {
        Tools_Timed_Message_System::AddMessage( 0x00ff00, "Saved extended eather mode successfully" );
    }
}



////////////////////////////////////////////////////////////////////////////////
//
// IsModified
//
////////////////////////////////////////////////////////////////////////////////
bool Tools_Mode_Extended_Weather::IsModified ( void )
{
    return Tools_SaveAsura::IsSkyboxModified();
	//return s_bMadeChanges;
}



////////////////////////////////////////////////////////////////////////////////
//
// Handle Keyboard
//
////////////////////////////////////////////////////////////////////////////////
void Tools_Mode_Extended_Weather::HandleKeyboard ( void )
{
    if(Asura_Overlay_System::OverlaysHaveKeyboardFocus()) return;

    if(Asura_UserInput::KeyPressed(ASURA_KEYID_CTRL))
    {
    }
    else if(Asura_UserInput::KeyPressed(ASURA_KEYID_SHIFT))
    {
    }
    else
    {   
//        switch( s_eSubMode )
//        {
//            case TOOLS_WEATHER_SUBMODE_NONE:
//            {       
                if( Asura_UserInput::DebouncedKeyPressed('c') )
                {
                    CreateShelter();
                }

//				break;
//			}

//            default : 
//            {
//                Asura_Assert( false, "ERROR - Invalid Submode!" );
//                break;
//            }
//        } 
    }

}
/* end of function HandleKeyboard */



////////////////////////////////////////////////////////////////////////////////
//
// ToggleOverlay
//
////////////////////////////////////////////////////////////////////////////////
void Tools_Mode_Extended_Weather::ToggleWeatherOverlay()
{
    s_bWeatherOverlayOpen = !s_bWeatherOverlayOpen;

    if(s_bWeatherOverlayOpen)
    {
        CreateWeatherOverlay();
    }
    else
    {
        Asura_Overlay_System::CloseOverlays(TOOLS_OVERLAY_WEATHER);
    }
}



////////////////////////////////////////////////////////////////////////////////
//
// WeatherOverlayIsOn
//
////////////////////////////////////////////////////////////////////////////////
bool Tools_Mode_Extended_Weather::WeatherOverlayIsOn()
{
    return s_bWeatherOverlayOpen;
}



////////////////////////////////////////////////////////////////////////////////
//
// CreateWeatherOverlay
//
////////////////////////////////////////////////////////////////////////////////
void Tools_Mode_Extended_Weather::CreateWeatherOverlay ( void )
{
    // if the Overlay isn't open, we can't create it!
    if(!s_bWeatherOverlayOpen) return;

    Asura_Overlay* pxNewOverlay;
    pxNewOverlay = new Asura_Overlay("Extended Weather Mode", false, &s_xWeatherOverlayPosition);
    
    Asura_Vector_2 xOffset = Asura_Vector_2(1.0f,3.0f);

    if(s_pxThis == NULL)
	{
		Asura_Assert(false, "s_pxThis == NULL");
		return;
	}

    s_pxThis->AddWeatherOverlayControls(pxNewOverlay, xOffset);
    
	//TODO - new overlay id
    pxNewOverlay->ID = TOOLS_OVERLAY_WEATHER;
    Asura_Overlay_System::SubmitNewOverlay(pxNewOverlay,false);
}



////////////////////////////////////////////////////////////////////////////////
//
// AddWeatherOverlayControls
//
////////////////////////////////////////////////////////////////////////////////
void Tools_Mode_Extended_Weather::AddWeatherOverlayControls(class Asura_Overlay* pxNewOverlay, class Asura_Vector_2& xOffset)
{
	Asura_Dialog* pxNewDialog = new Asura_Tickbox_Dialog("Rain", xOffset, &s_bRaining, &s_bMadeChanges );
	pxNewOverlay->AddDialog(pxNewDialog);
	xOffset.y += 1.5f;

	pxNewDialog = new Asura_Tickbox_Dialog("2D Effect", xOffset, &s_b2DEffect, &s_bMadeChanges );
	pxNewOverlay->AddDialog(pxNewDialog);
	xOffset.y += 2.0f;

	pxNewDialog = new Asura_Function_Dialog("Create Rain Shelter (C)", xOffset, CreateShelter, NULL, NULL, true );
	pxNewOverlay->AddDialog(pxNewDialog);
	xOffset.y += 2.0f;
}



////////////////////////////////////////////////////////////////////////////////
//
// Undo functionality. Ahem.
//
////////////////////////////////////////////////////////////////////////////////
void Tools_Mode_Extended_Weather::Undo()
{
}


void Tools_Mode_Extended_Weather::Redo()
{
}


void Tools_Mode_Extended_Weather::PrepareUndo()
{
}



////////////////////////////////////////////////////////////////////////////////
//
// CreateShelter
//
////////////////////////////////////////////////////////////////////////////////
void Tools_Mode_Extended_Weather::CreateShelter()
{
    if(Tools_Extended_Particle_Rain_Shelter::Create())
    {
        s_bMadeChanges = true;
        Tools_Timed_Message_System::AddMessage( 0x00ff00, "Created Rain Shelter" );
    }
    else
    {
        Tools_Timed_Message_System::AddMessage( 0xff0000, "Failed to create shelter" );
    }   
}


