//
// Tools_StreamingConnection_Object.cpp
//
// Created by Richard Rice - 06/03/2006
// 
// Copyright (C) 2006 Rebellion, All Rights Reserved.
//

#include "Core/Asura.h"
#include "Streaming/Tools_StreamingConnection_Object.h"
#ifdef ASURA_USE_STREAMING

#include "Interface/Asura_Overlay.h"
#include "Interface/Asura_Dialog.h"
#include "Streaming/Asura_Streaming_Zone.h"
#include "Streaming/Asura_Streaming_Connection.h"

#include "Load&Save/Tools_SaveAsura.h"


static List<Tools_StreamingConnection_Object*> g_xList;

Tools_StreamingConnection_Object::Tools_StreamingConnection_Object(const char* szConnectionName,Asura_Streaming_Zone* pxZone)
:Tools_VolumeEdit_Object(szConnectionName)
{
	g_xList.add_entry(this);

	m_pxZone = pxZone;
	m_uConnectionHash = Asura_GetHashID(szConnectionName);

	//get values from connection
	Asura_Streaming_Connection* pxConnection = GetConnection();
	Asura_Assert(pxConnection,"Failed to find connection");

	m_bLowDetail = pxConnection->IsLowDetail();
	m_bUseBox = pxConnection->GetUseBoxForLoading();
	m_xBox = pxConnection->GetBoxForLoading();
	m_bConnectionModified = false;

	
	//set pointers in base class
	m_pxVolume = &m_xBox;
	m_xWorldVolume = m_xBox;
	SetDefaultPositionFromVolume();
	m_pbModifiedFlag = &m_bConnectionModified;
}

Tools_StreamingConnection_Object::~Tools_StreamingConnection_Object()
{
	g_xList.delete_entry(this);
}

Tools_StreamingConnection_Object* Tools_StreamingConnection_Object::FindByName(const char* szName)
{
	Asura_Hash_ID uHash = Asura_GetHashID(szName);

	for(LIF<Tools_StreamingConnection_Object*> xLIF(&g_xList);!xLIF.done();xLIF.next())
	{
		if(xLIF()->m_uConnectionHash == uHash)
		{
			return xLIF();
		}
	}
	return 0;
}


Asura_Streaming_Connection* Tools_StreamingConnection_Object::GetConnection()
{
	for(u_int i=0;i<m_pxZone->GetNumConnection();i++)
	{
		Asura_Streaming_Connection* pxConnection = m_pxZone->GetConnection(i);
		if(pxConnection->GetToZoneHashID() == m_uConnectionHash)
		{		
			return pxConnection;
		}
	}
	return 0;
}

void Tools_StreamingConnection_Object::Update()
{
	Tools_VolumeEdit_Object::Update();

	if(m_bConnectionModified)
	{
		//update details in connection
		Asura_Streaming_Connection* pxConnection = GetConnection();
		if(pxConnection)
		{
			pxConnection->SetLowDetail(m_bLowDetail);
			pxConnection->SetUseBoxForLoading(m_bUseBox);
			pxConnection->SetBoxForLoading(m_xBox);

			Tools_SaveAsura::SetStreamingConnectionsModified();
		}
		m_bConnectionModified = false;
	}
}

void Tools_StreamingConnection_Object::Render()
{
	if(!m_bUseBox)
	{
		Tools_Object::Render();
	}
	else
	{
		Tools_VolumeEdit_Object::Render();
	}
}

void Tools_StreamingConnection_Object::AddOverlayControls(class Asura_Overlay* pxOverlay,Asura_Vector_2& xOffset)
{


	Asura_Dialog* pxDialog = new Asura_Tickbox_Dialog("Low Detail",xOffset,&m_bLowDetail,&m_bConnectionModified);
	pxDialog->SetHelpText("If ticked then the low detail version of the zone should be loaded.");
	pxOverlay->AddDialog(pxDialog);
	xOffset.y += Asura_Dialog::DefaultSpacing();

	pxDialog = new Asura_Tickbox_Dialog("Use Box For Loading",xOffset,&m_bUseBox,&m_bConnectionModified);
	pxDialog->SetHelpText("If ticked then the bounding box edited by this object will be used for determining when the connected zone should be loaded");
	pxOverlay->AddDialog(pxDialog);
	xOffset.y += Asura_Dialog::LargeSpacing();

	Tools_VolumeEdit_Object::AddOverlayControls(pxOverlay,xOffset);
}

#endif // ASURA_USE_STREAMING