//////////////////////////////////////////////////////////////////////////////
//
// Tools_Batch_Waypointing.cpp
//
// Created by Andrew Weinkove - 16/09/03
//
// Copyright (C) 2007 Rebellion, All Rights Reserved.
//
//////////////////////////////////////////////////////////////////////////////

// I N C L U D E S ///////////////////////////////////////////////////////////

// core
#include "Core/Asura.h"

// lib
#include <process.h>

// header
#include "Waypointing/Tools_Batch_Waypointing.h"

// asura
#include "Core/Asura_NameTree.h"
#include "Core/Asura_String.h"

// tools
#include "Tools_System.h"
#include "Misc/Tools_IniFile.h"
#include "Load&Save/Tools_BatchProcessing.h"
#include "Load&Save/Tools_SaveAsura.h"
#include "Waypointing/Tools_Mode_Waypointing.h"

//////////////////////////////////////////////////////////////////////////////
//
// Tools_Batch_Waypointing
//
//////////////////////////////////////////////////////////////////////////////

Tools_Batch_Waypointing* Tools_Batch_Waypointing::s_pxThis = NULL;
const char*              Tools_Batch_Waypointing::s_pszIdentifiers[ WID_NUM_WAYPOINTING_IDENTIFIERS ] =
{
    "File",
    "Generate Coverpoints",
    "Calculate Zone Information",
    "Min Waypoint Radius",
    "Max Waypoint Radius",
    "Min Dense Waypoint Radius",
    "Max Dense Waypoint Radius",
    "Min Link Length",
    "Max Link Length"
};

Tools_Batch_Waypointing::Tools_Batch_Waypointing()
{
	Asura_Assert(!s_pxThis, "Tools batch waypointing already allocated?");
	s_pxThis = this;
}

Tools_Batch_Waypointing::~Tools_Batch_Waypointing()
{
	s_pxThis = NULL;
}

Tools_Batch_Waypointing* Tools_Batch_Waypointing::GetThis()
{
	return s_pxThis;
}

void Tools_Batch_Waypointing::BatchWaypointing_WaypointLevel()
{
	Asura_Assert( Tools_BatchProcessing::IsWaypointing(), "Tools_Batch_Waypointing::BatchWaypointing_WaypointLevel - should be waypointing" );

    Tools_Mode_Waypointing::SetIsBatchWaypointing( true );

    int  iPreviousToolsMode    = TOOLS_MODE_NULL;
    bool bCreatedModeOurselves = false;
    if ( Tools_System::GetCurrentMode() != TOOLS_MODE_WAYPOINTING )
    {
        iPreviousToolsMode = Tools_System::GetCurrentMode();
        Tools_System::RequestWaypointingModeSuppressSavePrompt();
        bCreatedModeOurselves = true;
    }

	Tools_IniFile* pxIniFile = Tools_BatchProcessing::GetIniFile();
	
	bool  bGenerateCoverpoints      = Tools_Mode_Waypointing::GetShouldGenCoverpoints();
	bool  bCalculateZoneInformation = Tools_Mode_Waypointing::GetShouldCalculateWaypointZoningData();
    float fMinWPRadius              = Tools_Mode_Waypointing::GetMinWaypointRadius();
    float fMaxWPRadius              = Tools_Mode_Waypointing::GetMaxWaypointRadius();
    float fMinDenseWPRadius         = Tools_Mode_Waypointing::GetMinDenseWaypointRadius();
    float fMaxDenseWPRadius         = Tools_Mode_Waypointing::GetMaxDenseWaypointRadius();
    float fMinLinkLength            = Tools_Mode_Waypointing::GetMinLinkLength();
    float fMaxLinkLength            = Tools_Mode_Waypointing::GetMaxLinkLength();
	// Read the settings in the default section, then in the section for this level.
	for ( int iLoop = 0; iLoop < 2; ++iLoop )
	{
		if(iLoop == 0)
		{
			if(!pxIniFile->GetIniTextSection("Default"))
			{
				continue;
			}
		}
		else
		{
			if(!pxIniFile->GetIniTextSectionByIndex(Tools_BatchProcessing::GetSectionIndex()))
			{
				continue;
			}
		}

        pxIniFile->GetBoolInCurrentSection( s_pszIdentifiers[ WID_GENERATE_COVERPOINTS ],       bGenerateCoverpoints      );
		pxIniFile->GetBoolInCurrentSection( s_pszIdentifiers[ WID_CALCULATE_ZONE_INFORMATION ], bCalculateZoneInformation );
        pxIniFile->GetFloatInCurrentSection( s_pszIdentifiers[ WID_MIN_WP_RADIUS ],             fMinWPRadius              );
        pxIniFile->GetFloatInCurrentSection( s_pszIdentifiers[ WID_MAX_WP_RADIUS ],             fMaxWPRadius              );
        pxIniFile->GetFloatInCurrentSection( s_pszIdentifiers[ WID_MIN_DENSE_WP_RADIUS ],       fMinDenseWPRadius         );
        pxIniFile->GetFloatInCurrentSection( s_pszIdentifiers[ WID_MAX_DENSE_WP_RADIUS ],       fMaxDenseWPRadius         );
        pxIniFile->GetFloatInCurrentSection( s_pszIdentifiers[ WID_MIN_LINK_LENGTH ],           fMinLinkLength            );
        pxIniFile->GetFloatInCurrentSection( s_pszIdentifiers[ WID_MAX_LINK_LENGTH ],           fMaxLinkLength            );
	}

	if(!Tools_BatchProcessing::IsAborted())
	{
		// (AW - 17.09.2003) - Generate the waypoints.
		Tools_Mode_Waypointing::SetShouldGenerateCoverpoints( bGenerateCoverpoints );
		Tools_Mode_Waypointing::SetShouldCalculateWaypointZoningData( bCalculateZoneInformation );
        Tools_Mode_Waypointing::SetMinWaypointRadius( fMinWPRadius );
        Tools_Mode_Waypointing::SetMaxWaypointRadius( fMaxWPRadius );
        Tools_Mode_Waypointing::SetMinDenseWaypointRadius( fMinDenseWPRadius );
        Tools_Mode_Waypointing::SetMaxDenseWaypointRadius( fMaxDenseWPRadius );
        Tools_Mode_Waypointing::SetMinLinkLength( fMinLinkLength );
        Tools_Mode_Waypointing::SetMaxLinkLength( fMaxLinkLength );
		Tools_Mode_Waypointing::GenerateWaypointsStatic();

		// (AW - 17.09.2003) - Don't forget to save!
		Tools_SaveAsura::QuickSaveFile();
		Tools_SaveAsura::SaveWaypointing();
		//entities may need saving if blocked waypoints for doors have been changed
		if(Tools_SaveAsura::IsEntityModified())	Tools_SaveAsura::SaveEntities();

		//and these files may need updating as a results of templates being changed
		if(Tools_SaveAsura::IsSoundsModified()) Tools_SaveAsura::SaveSounds();
		if(Tools_SaveAsura::IsGlobalIlluminationModified()) Tools_SaveAsura::SaveGlobalIllumination();
		if(Tools_SaveAsura::IsLightingModified()) Tools_SaveAsura::SaveLights();
		if(Tools_SaveAsura::IsEnvironmentalLightTransportModified()) Tools_SaveAsura::SaveEnvironmentalLightTransport();
	}

    if ( bCreatedModeOurselves )
    {
        Tools_System::RequestNextMode( iPreviousToolsMode );
    }

    Tools_Mode_Waypointing::SetIsBatchWaypointing( false );
}

void Tools_Batch_Waypointing::BatchWaypointing_GetIdentifiers( List<const char*>* pxIdentifiers )
{
    for ( int i = 0; i < WID_NUM_WAYPOINTING_IDENTIFIERS; ++i )
    {
        pxIdentifiers->add_entry_end( s_pszIdentifiers[ i ] );
    }
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////
