///////////////////////////////////////////////////////////////////////////////
//
// File: Tools_ContainerFile.cpp
//
// Created by Martin G.Harris (3/6/2008)
// 
// Copyright (C) 2008 Rebellion, All Rights Reserved.
//
///////////////////////////////////////////////////////////////////////////////

#include "Core/Asura.h"
#include "Entity/Container/Tools_ContainerFile.h"

#ifdef ASURA_USE_CONTAINER_SYSTEM

// Asura
#include "Core/Asura_StringBuffer.h"
#include "Debugging/Asura_Assert.h"

// Tools
#include "Entity/Container/Tools_Container_System.h"
#include "Load&Save/Tools_SaveAsura.h"
#include "Misc/Tools_Timed_Messages.h"

///////////////////////////////////////////////////////////////////////////////
// Tools_ContainerFile
///////////////////////////////////////////////////////////////////////////////

//  Note: Should match definition in Tools_Container_System.cpp
#define szCONTAINERS_DIRECTORY    "Containers\\"

//////////////////
// SetShortName //
//////////////////
// Set the short name from the filename.
void Tools_ContainerFile::SetShortName(bool bNewFile)
{
	m_xFilename.TidyFilename();

	Asura_String xPath;
	m_xFilename.SplitFilename(&xPath, &m_xShortName, 0);
	
	// Remove "Containers\" from start of full filename.
	const char* const szDirectory = szCONTAINERS_DIRECTORY;
	if (xPath.StartsWith(szDirectory))
	{
		// File is in a sub-directory, skip first part of path, keep the rest.
		Asura_String256 xBuffer;
		xBuffer += (xPath.GetString() + strlen(szDirectory));
		xBuffer += "\\";
		xBuffer += m_xShortName.GetString();
	
		m_xShortName = xBuffer.GetString();
	}

	if (bNewFile)
	{
		// On creation save the short name (this is saved out in the chunk and will never change).
		// Allows us to rename the file without having to fix up all the data.
		m_xCreationShortName = m_xShortName;
	}
}

/////////////////
// SetModified //
/////////////////
void Tools_ContainerFile::SetModified()
{
	if (!m_bModified)
	{
		WarnIfReadOnly(true);
	}
	m_bModified = true;
}

////////////////////
// WarnIfReadOnly //
////////////////////
// If file is read-only display a warning message.
void Tools_ContainerFile::WarnIfReadOnly(bool bForceCheck /*= false*/) const
{
	// Only check a maximum of once per frame, unless bForceCheck is true.
	if (!bForceCheck && m_uCheckedWriteableFrame == Asura_Timers::GetFrameNumber())
	{
		return;
	}

	m_uCheckedWriteableFrame = Asura_Timers::GetFrameNumber();
	m_bWriteable = Tools_SaveAsura::WarnIfReadOnly(m_xFilename.GetString());
}

////////////////////////////
// RefreshWriteableStatus //
////////////////////////////
// Update writeable status, no message is displayed.
void Tools_ContainerFile::RefreshWriteableStatus() const
{
	// Only check a maximum of once per frame.
	if (m_uCheckedWriteableFrame == Asura_Timers::GetFrameNumber())
	{
		return;
	}

	m_uCheckedWriteableFrame = Asura_Timers::GetFrameNumber();
	m_bWriteable = Tools_SaveAsura::CanSaveFile(m_xFilename.GetString(), false);
}

/////////////
// CanSave //
/////////////
// Can we save this file? If not then display error message.
bool Tools_ContainerFile::CanSave()
{
	m_uCheckedWriteableFrame = Asura_Timers::GetFrameNumber();
	m_bWriteable = Tools_SaveAsura::CanSaveFile(m_xFilename.GetString(), true);
	return m_bWriteable;
}


///////////////////////////////////////////////////////////////////////////////
// Tools_ContainerGroup
///////////////////////////////////////////////////////////////////////////////
// Set the intial creation name from the filename and group name.
// This should be unique even if a number of people are working on the container files
// at the same time.
void Tools_ContainerGroup::SetCreationGroupName()
{
	Asura_Assert(!m_xGroupName.IsBlank(), "Blank container group name!");

	Asura_String2048 xBuffer;
	const Tools_ContainerFile* pxFile = Tools_Container_System::FindFileByHash(m_uFileHash);
	Asura_Assert(pxFile, "Couldn't find container file");
	if (pxFile)
	{
		Asura_Assert(!pxFile->GetCreationShortName().IsBlank(), "Blank container filename!");
		xBuffer += pxFile->GetCreationShortName().GetString();
		xBuffer += "\\";
	}
	xBuffer += m_xGroupName.GetString();

	m_xCreationGroupName = xBuffer.GetString();
}


/////////////////
// SetModified //
/////////////////
// Flag this group as having been modified.
void Tools_ContainerGroup::SetModified()
{
	Tools_Container_System::SetFileModified(m_uFileHash);
}


#endif //#ifdef ASURA_USE_CONTAINER_SYSTEM
