//////////////////////////////////////////////////////////////////////////////
//
// AVP_Pickup.h
//
// (AJK - 29.08.2008) Created
//
// Copyright (c) 2008 Rebellion. All rights reserved.
//
//////////////////////////////////////////////////////////////////////////////

#include "Core/Asura.h"

// Own header
#include "Entity/PhysicalObject/Pickup/AVP_Pickup_Messages.h"

// Engine
#include "Network/Asura_Message.h"

#ifdef ASURA_SUPPORT_NETMSG_SYSTEM
#include "Network/Asura_NetMsg.h"
#endif //#ifdef ASURA_SUPPORT_NETMSG_SYSTEM

// AVP
#include "Entity/AVP_Entity_Classification.h"
#include "Entity/PhysicalObject/Pickup/AVP_ClientEntity_Pickup.h"
#include "Entity/PhysicalObject/Pickup/AVP_ServerEntity_Pickup.h"

//
// Creation message
//
void AVP_Message_Pickup_ClientCreate::FillPickup(const AVP_ServerEntity_Pickup* pxPickup) 
{
    m_uPickupBlueprintHash   = pxPickup->GetPickupBlueprintHash();
	m_uDialogueBlueprintHash = pxPickup->GetDialogueBlueprintHashID();
    m_bHidden                = pxPickup->IsHidden();
	m_bEnabled               = pxPickup->IsEnabled();
	
	FillCreatePhysicalObject(pxPickup);
}

void AVP_Message_Pickup_ClientCreate::ExtractPickup(AVP_ClientEntity_Pickup* pxPickup) const
{
	pxPickup->SetPickupBlueprintHash(m_uPickupBlueprintHash);
	pxPickup->SetDialogueBlueprintHashID(m_uDialogueBlueprintHash);
	pxPickup->SetInvisibleFlag(m_bHidden);
	pxPickup->SetEnabed(m_bEnabled);
}

//
// Update message
//
void AVP_Message_Pickup_ClientUpdate::FillPickup(const AVP_ServerEntity_Pickup* pxPickup) 
{
	m_bHidden  = pxPickup->IsHidden();
	m_bEnabled = pxPickup->IsEnabled();
	FillPhysicalObject(pxPickup);
}

void AVP_Message_Pickup_ClientUpdate::ExtractPickup(AVP_ClientEntity_Pickup* pxPickup) const
{
	pxPickup->SetInvisibleFlag(m_bHidden);
	pxPickup->SetEnabed(m_bEnabled);
}

#ifdef ASURA_SUPPORT_NETMSG_SYSTEM
void AVP_Message_Pickup_ClientUpdate::RegisterFields(u_short usMsgID, u_short usEntityClassification)
{
	Asura_Assert(usMsgID == ASURA_MSG_CLIENTUPDATE, "invalid msg ID");
	Asura_Assert(!Asura_NetMsg::FindNetMsgObject(usMsgID, usEntityClassification), "already exists");
	Asura_Assert(usEntityClassification == AVP_ENTITYCLASS_PICKUP, "unexpected classification");

	u_char ucNumFields = GetNumFields();

	Asura_NetMsg* pxNetMsg = new Asura_NetMsg(usMsgID, usEntityClassification, ucNumFields, sizeof(AVP_Message_Pickup_ClientUpdate));
	RegisterFields(pxNetMsg);
	pxNetMsg->EndRegistration();
}

void AVP_Message_Pickup_ClientUpdate::RegisterFields(class Asura_NetMsg* pxNetMsg)
{
	AVP_Message_Pickup_ClientUpdate xDummy;
	PARENT::RegisterFields(pxNetMsg);

	Asura_NetMsg_RegisterField(pxNetMsg, xDummy, Asura_FieldType_uchar, m_bHidden, 1, Asura_Threshold_AnyChange, 0.0f);
	Asura_NetMsg_RegisterField(pxNetMsg, xDummy, Asura_FieldType_uchar, m_bEnabled, 1, Asura_Threshold_AnyChange, 0.0f);

}
#endif//#ifdef ASURA_SUPPORT_NETMSG_SYSTEM

//
// Collection message
//
void AVP_Message_Pickup_Collect::FillInfo   ( Asura_Guid uPickupGuid, Asura_Hash_ID  uPickupBlueprintHash, int iQuantity,  bool bSetAsCurrent, int iSecondaryQuantity)
{
	m_uPickupGuid			= uPickupGuid;
	m_uPickupBlueprintHash  = uPickupBlueprintHash;
	m_iQuantity             = iQuantity;
	m_iSecondaryQuantity	= iSecondaryQuantity;
	m_bSetAsCurrent         = bSetAsCurrent;
}
void AVP_Message_Pickup_Collect::ExtractInfo( Asura_Guid& uPickupGuid, Asura_Hash_ID& uPickupBlueprintHash, int& iQuantity,  bool& bSetAsCurrent, int* piSecondaryQuantity) const
{ 
	uPickupGuid			 = m_uPickupGuid;
	uPickupBlueprintHash = m_uPickupBlueprintHash; 
    iQuantity            = m_iQuantity;
	if(piSecondaryQuantity)
	{
		*piSecondaryQuantity = m_iSecondaryQuantity;
	}
	bSetAsCurrent        = m_bSetAsCurrent;
}