//
// Tools_GeneralOptions.cpp
//
// Created by Richard Rice - 13/01/2006
// 
// Copyright (C) 2006 Rebellion, All Rights Reserved.
//


#include "Core/Asura.h"
#include "Misc/Tools_GeneralOptions.h"

#include "Interface/Asura_Overlay_System.h"
#include "Interface/Asura_Overlay.h"
#include "Interface/Asura_Dialog.h"

#include "Tools_Overlays.h"
#include "Tools_System.h"
#include "Entity/Tools_Entity_Simulation.h"
#include "Load&Save/Tools_ModeDataFile.h"
#include "Load&Save/Tools_AsrFileManager.h"
#include "Load&Save/Tools_SaveAsura.h"
#include "Misc/Tools_Render_Options.h"
#include "Movement/Tools_Movement.h"
#include "Sound/Tools_Sound_System.h"


Asura_Vector_2 Tools_GeneralOptions::s_xOptionsOverlayPosition(20.0f,3.0f);

static u_int s_uSelectedTab = 0;


void Tools_GeneralOptions::Initialise()
{
}

void Tools_GeneralOptions::Shutdown()
{
	CloseOptionsOverlay();
}



void Tools_GeneralOptions::ToggleOptionsOverlay()
{
	if(OptionsOverlayIsOn())
	{
		CloseOptionsOverlay();
	}
	else
	{
		CreateOptionsOverlay();		
	}
}

bool Tools_GeneralOptions::OptionsOverlayIsOn()
{
	return Asura_Overlay_System::DoesOverlayExist(TOOLS_OVERLAY_GENERALOPTIONS);
}

void Tools_GeneralOptions::CloseOptionsOverlay()
{
	Asura_Overlay_System::CloseOverlays(TOOLS_OVERLAY_GENERALOPTIONS);
}


void Tools_GeneralOptions::CreateOptionsOverlay()
{
	Asura_Overlay* pxOverlay = new Asura_Overlay("Options",false,&s_xOptionsOverlayPosition);	

	//Asura_Dialog* pxDialog;
	Asura_Vector_2 xOffset = Asura_Vector_2(1.0f,3.0f);

	static const char* s_aszTabs[TOOLS_GENERALOPTIONS_NUMTABS] =
	{
		"Mouse",
		"Movement",
		"Sound",
		"Message Log",
		"Visibility",
		"Render Modes",
		"Display",
		"Subdivision",
		"Colours",
		"SFX",
		"Status Text",
		"Performance",
		"File",

	};

	//start of tabs
	Asura_Tabs_Dialog* pxTabsDialog = new Asura_Tabs_Dialog(xOffset,s_aszTabs, TOOLS_GENERALOPTIONS_NUMTABS, 40);
	pxOverlay->AddDialog(pxTabsDialog);
	pxOverlay->PushCurrentParentDialog(pxTabsDialog);

	xOffset = pxTabsDialog->GetTopDialogPosition();

	float fTabsStartY = xOffset.y;
	float fTabsEndY = xOffset.y;

	for(int iTab=0;iTab<TOOLS_GENERALOPTIONS_NUMTABS;iTab++)
	{
		pxTabsDialog->SelectTab(iTab);
		xOffset.y = fTabsStartY;
		
		if(iTab == TOOLS_GENERALOPTIONS_SOUND)
		{
			Tools_Sound_System::AddOverlayControls(pxOverlay,xOffset);
		}
		if(iTab == TOOLS_GENERALOPTIONS_MESSAGELOG)
		{
			Tools_Entity_Simulation::AddMessageLogFilterOverlayControls(pxOverlay,xOffset);
		}
		if(iTab == TOOLS_GENERALOPTIONS_FILE)
		{
			Tools_SaveAsura::AddOverlayControls(pxOverlay,xOffset);
		}


		Tools_System::AddOverlayControls(pxOverlay,xOffset,iTab);
		Tools_Render_Options::AddOverlayControls(pxOverlay, xOffset, iTab);

		if(iTab == TOOLS_GENERALOPTIONS_MOVEMENT)
		{
			Tools_Movement::AddOverlayControls(pxOverlay,xOffset);
		}


		if(xOffset.y > fTabsEndY) fTabsEndY = xOffset.y;
	}



	//End of tabs
	xOffset.y = fTabsEndY + 1.0f;
	pxOverlay->PopCurrentParentDialog();
	pxTabsDialog->SetSelectedTabPointer(&s_uSelectedTab);

	pxOverlay->m_uID = TOOLS_OVERLAY_GENERALOPTIONS;
	Asura_Overlay_System::SubmitNewOverlay(pxOverlay);

}


void Tools_GeneralOptions::LoadSettingsFromIniFile()
{
	TransferModeData(TOOLSMODEDATAFILE_LOAD);
}

void Tools_GeneralOptions::SaveSettingsToIniFile()
{
	TransferModeData(TOOLSMODEDATAFILE_SAVE);
}

void Tools_GeneralOptions::TransferModeData(int iLoadOrSave)
{
	//(Most of the movement options are saved to a text ini file instead... just keeping the overlay
	//position here for now , since it isn't intended to be edited by hand).
	
	//open the file
	Tools_ModeDataFile xFile("GeneralOptions.dat",iLoadOrSave);

	//tranfer the data
	//(none of these should be removed , and new data should go on the end, otherwise
	//the files will get screwed up)
	if( false == xFile.GetError())
	{
		xFile.Transfer(s_xOptionsOverlayPosition);
		//shoving this here since I don't really want to create a new file for it, and general options
		//seems vaguely suitable for general stuff.
		xFile.Transfer(Tools_AsrFileManager::s_xOverlayPosition);
		xFile.Transfer(Tools_AsrFileManager::s_xUserSetTreeSize);
	}
}

void Tools_GeneralOptions::SetSelectedOptionTab ( const Tools_GeneralOptions_Tab eTab )
{
	s_uSelectedTab = static_cast<u_int>( eTab );
}

Tools_GeneralOptions_Tab Tools_GeneralOptions::GetSelectedOptionTab()
{
	return static_cast<Tools_GeneralOptions_Tab>( s_uSelectedTab );
}

//eof
