//////////////////////////////////////////////////////////////////////////////
//
// AVP_ClientEntity_InteractBatteryReceptacle.h
//
// Created by AJK 25/11/2008
//
// Copyright (c) 2008 Rebellion. All rights reserved.
//
//////////////////////////////////////////////////////////////////////////////

#include "Core/Asura.h"

#include "Entity/PhysicalObject/Interact/ToolReceptacle/BatteryReceptacle/AVP_ClientEntity_InteractBatteryReceptacle.h"

//ASURA
#include "Animation/Asura_AnimationAttachment.h"
#include "Animation/Asura_AnimationController_Animation.h"
#include "Blueprint/Asura_Blueprint_System.h"
#include "Entity/PhysicalObject/Asura_ClientEntity_PhysicalObject.h"
#include "Network/Asura_Message.h"


// AVP
#include "Entity/AVP_Entity_Classification.h"
#include "Entity/PhysicalObject/Actor/AVP_Actor_Messages.h"
#include "Entity/PhysicalObject/Actor/AVP_ServerEntity_Actor.h"
#include "Entity/PhysicalObject/Interact/ToolReceptacle/BatteryReceptacle/AVP_Messages_InteractBatteryReceptacle.h"
#include "Entity/AVP_Messages.h"
#include "Inventory/AVP_Inventory_HashIDs.h"
#include "Inventory/AVP_Inventory_Item.h"
#include "Inventory/AVP_Inventory_ItemTypes.h"
#include "Inventory/AVP_Inventory_System.h"
#include "Actor/AVP_Actor_AnimStateData.h"
#include "Moves/AVP_Move_Interaction.h"
#include "Moves/Vertebrates/Human/AVP_Human_MovesInteraction.h"

Asura_Hashed_List<AVP_ClientEntity_InteractBatteryReceptacle> AVP_ClientEntity_InteractBatteryReceptacle::s_xList;

AVP_ClientEntity_InteractBatteryReceptacle::AVP_ClientEntity_InteractBatteryReceptacle()
: m_uMarineInsertBatteryHUDAnimID( ASURA_HASH_ID_UNSET )
, m_uMarineInsertBatteryLetGoHUDAnimID( ASURA_HASH_ID_UNSET )
, m_uMarineRemoveBatteryHUDAnimID( ASURA_HASH_ID_UNSET )
, m_uMarineRemoveGrabBatteryHUDAnimID( ASURA_HASH_ID_UNSET )
, m_uMarineInsertBatteryTPAnimID( ASURA_HASH_ID_UNSET )
, m_uMarineInsertBatteryLetGoTPAnimID( ASURA_HASH_ID_UNSET )
, m_uMarineRemoveBatteryTPAnimID( ASURA_HASH_ID_UNSET )
, m_uMarineRemoveGrabBatteryTPAnimID( ASURA_HASH_ID_UNSET )
, m_uAlienDestroyBatteryHUDAnimID( ASURA_HASH_ID_UNSET )
, m_uAlienDestroyBatteryTPAnimID( ASURA_HASH_ID_UNSET )
, m_uPredatorUseBatteryHUDAnimID( ASURA_HASH_ID_UNSET )
, m_uPredatorUseBatteryTPAnimID( ASURA_HASH_ID_UNSET )
, m_uBatteryDestroyedAnimID( ASURA_HASH_ID_UNSET )
, m_uBatteryOnInsertAnimID( ASURA_HASH_ID_UNSET )
, m_uBatteryOnRemoveAnimID( ASURA_HASH_ID_UNSET )
{
	Classification = AVP_ENTITYCLASS_INTERACT_BATTERY_RECEPTACLE;

	s_xList.AddToList( this );
}

AVP_ClientEntity_InteractBatteryReceptacle::~AVP_ClientEntity_InteractBatteryReceptacle() 
{
	s_xList.RemoveFromList( this );
}

void AVP_ClientEntity_InteractBatteryReceptacle::Create( const Asura_Message* pxMsg )
{
	Asura_Assert( pxMsg->GetMsgID() == ASURA_MSG_CLIENTCREATE, "" );
	Asura_Assert( pxMsg->GetEntityClassification() == AVP_ENTITYCLASS_INTERACT_BATTERY_RECEPTACLE, "" );

	const AVP_Message_InteractBatteryReceptacle_ClientCreate* pxData = static_cast< const AVP_Message_InteractBatteryReceptacle_ClientCreate* >( pxMsg->GetData() );

	AVP_ClientEntity_InteractBatteryReceptacle* pxNew = new AVP_ClientEntity_InteractBatteryReceptacle();
	pxNew->ProcessClientCreate(  pxData, pxMsg->GetSentTime(), true );
}

void AVP_ClientEntity_InteractBatteryReceptacle::ProcessClientCreate( const AVP_Message_InteractBatteryReceptacle_ClientCreate* pxData, float fServerTime, bool bProcessPosAndOri )
{
	PARENT::ProcessClientCreate(pxData, fServerTime, bProcessPosAndOri);
}

void AVP_ClientEntity_InteractBatteryReceptacle::OnAlienInteraction(const Asura_Hash_ID uAnim)
{
	if ( uAnim != ASURA_HASH_ID_UNSET )
	{
		// Play animation
		Asura_Animation* pxAnimation = GetShapeInstance().GetAnimation();
		if ( pxAnimation )
		{
			Asura_AnimationBlend* pxBlend = new Asura_AnimationBlend_AnimatedTween( 0.0f, false );
			Asura_AnimationController_Animation* pxAACA = pxAnimation->CreateControllerAnimation( AAC_FLAG_ACTIVE | AAC_FLAG_ALLOW_DESTROY_FROM_MESSAGE, 0, uAnim, pxBlend );
			if ( pxAACA )
			{
				pxAACA->Play();
			}
			else
			{
				delete pxBlend;
			}
		}
	}
}

void AVP_ClientEntity_InteractBatteryReceptacle::GetMarineInsertBatteryAnimHashes( Asura_Hash_ID& uHUDAnimHash, Asura_Hash_ID& uTPAnimHash ) const
{
	uHUDAnimHash = m_uMarineInsertBatteryHUDAnimID;
	uTPAnimHash  = m_uMarineInsertBatteryTPAnimID;
}

void AVP_ClientEntity_InteractBatteryReceptacle::GetMarineInsertBatteryLetGoAnimHashes( Asura_Hash_ID& uHUDAnimHash, Asura_Hash_ID& uTPAnimHash ) const
{
	uHUDAnimHash = m_uMarineInsertBatteryLetGoHUDAnimID;
	uTPAnimHash  = m_uMarineInsertBatteryLetGoTPAnimID;
}

void AVP_ClientEntity_InteractBatteryReceptacle::GetMarineRemoveBatteryAnimHashes( Asura_Hash_ID& uHUDAnimHash, Asura_Hash_ID& uTPAnimHash ) const
{
	uHUDAnimHash = m_uMarineRemoveBatteryHUDAnimID;
	uTPAnimHash  = m_uMarineRemoveBatteryTPAnimID;
}

void AVP_ClientEntity_InteractBatteryReceptacle::GetMarineRemoveGrabBatteryAnimHashes( Asura_Hash_ID& uHUDAnimHash, Asura_Hash_ID& uTPAnimHash ) const
{
	uHUDAnimHash = m_uMarineRemoveGrabBatteryHUDAnimID;
	uTPAnimHash  = m_uMarineRemoveGrabBatteryTPAnimID;
}

void AVP_ClientEntity_InteractBatteryReceptacle::GetAlienDestroyBatteryAnimHashes( Asura_Hash_ID& uHUDAnimHash, Asura_Hash_ID& uTPAnimHash ) const
{
	uHUDAnimHash = m_uAlienDestroyBatteryHUDAnimID;
	uTPAnimHash  = m_uAlienDestroyBatteryTPAnimID;
}

void AVP_ClientEntity_InteractBatteryReceptacle::GetPredatorUseBatteryAnimHashes( Asura_Hash_ID& uHUDAnimHash, Asura_Hash_ID& uTPAnimHash ) const
{
	uHUDAnimHash = m_uPredatorUseBatteryHUDAnimID;
	uTPAnimHash  = m_uPredatorUseBatteryTPAnimID;
}

void AVP_ClientEntity_InteractBatteryReceptacle::InitialiseFromBlueprint( const Asura_Blueprint& xBlueprint )
{
	PARENT::InitialiseFromBlueprint( xBlueprint );

	Asura_Value xValue;

	if ( xBlueprint.GetParameterValueByHashID( 0x00D62FB3, xValue ) )	//Asura_GetHashID( "Anim.Interaction.PlugBattery.Marine.HUD"); 
	{
		m_uMarineInsertBatteryHUDAnimID = xValue.GetHashIDValue();
	}
	if ( xBlueprint.GetParameterValueByHashID( 0x210F2C40, xValue ) )	//Asura_GetHashID( "Anim.Interaction.PlugBattery.Marine.TP"); 
	{
		m_uMarineInsertBatteryTPAnimID = xValue.GetHashIDValue();
	}
	if ( xBlueprint.GetParameterValueByHashID( 0x9EE56D96, xValue ) )	//Asura_GetHashID( "Anim.Interaction.LetGoBattery.Marine.HUD"); 
	{
		m_uMarineInsertBatteryLetGoHUDAnimID = xValue.GetHashIDValue();
	}
	if ( xBlueprint.GetParameterValueByHashID( 0x80FF25FD, xValue ) )	//Asura_GetHashID( "Anim.Interaction.LetGoBattery.Marine.TP"); 
	{
		m_uMarineInsertBatteryLetGoTPAnimID = xValue.GetHashIDValue();
	}
	if ( xBlueprint.GetParameterValueByHashID( 0x42EFA34C, xValue ) )	//Asura_GetHashID( "Anim.Interaction.UnplugBattery.Marine.HUD"); 
	{
		m_uMarineRemoveBatteryHUDAnimID = xValue.GetHashIDValue();
	}
	if ( xBlueprint.GetParameterValueByHashID( 0xD09C6187, xValue ) )	//Asura_GetHashID( "Anim.Interaction.UnplugBattery.Marine.TP"); 
	{
		m_uMarineRemoveBatteryTPAnimID = xValue.GetHashIDValue();
	}
	if ( xBlueprint.GetParameterValueByHashID( 0x74BFB8F1, xValue ) )	//Asura_GetHashID( "Anim.Interaction.GrabBattery.Marine.HUD"); 
	{
		m_uMarineRemoveGrabBatteryHUDAnimID = xValue.GetHashIDValue();
	}
	if ( xBlueprint.GetParameterValueByHashID( 0x35508342, xValue ) )	//Asura_GetHashID( "Anim.Interaction.GrabBattery.Marine.TP"); 
	{
		m_uMarineRemoveGrabBatteryTPAnimID = xValue.GetHashIDValue();
	}
	if ( xBlueprint.GetParameterValueByHashID( 0xC214880E, xValue ) )	//Asura_GetHashID( "Anim.Interaction.DestroyBattery.Alien.HUD"); 
	{
		m_uAlienDestroyBatteryHUDAnimID = xValue.GetHashIDValue();
	}
	if ( xBlueprint.GetParameterValueByHashID( 0x16C6DC85, xValue ) )	//Asura_GetHashID( "Anim.Interaction.DestroyBattery.Alien.TP"); 
	{
		m_uAlienDestroyBatteryTPAnimID = xValue.GetHashIDValue();
	}
	if ( xBlueprint.GetParameterValueByHashID( 0xE45A4420, xValue ) )	//Asura_GetHashID( "Anim.Interaction.HealthFromBattery.Predator.HUD"); 
	{
		m_uPredatorUseBatteryHUDAnimID = xValue.GetHashIDValue();
	}
	if ( xBlueprint.GetParameterValueByHashID( 0x51B05633, xValue ) )	//Asura_GetHashID( "Anim.Interaction.HealthFromBattery.Predator.TP"); 
	{
		m_uPredatorUseBatteryTPAnimID = xValue.GetHashIDValue();
	}
	if ( xBlueprint.GetParameterValueByHashID( 0x33AA5F7F, xValue ) )	//Asura_GetHashID( "Anim.Interaction.Battery.Destroyed"); 
	{
		m_uBatteryDestroyedAnimID   = xValue.GetHashIDValue();
	}
	if ( xBlueprint.GetParameterValueByHashID( 0x47BC05F6, xValue ) )	//Asura_GetHashID( "BatteryOnInsertAnim"); 
	{
		m_uBatteryOnInsertAnimID = xValue.GetHashIDValue();
	}
	if ( xBlueprint.GetParameterValueByHashID( 0x4FA05E41, xValue ) )	//Asura_GetHashID( "BatteryOnRemoveAnim"); 
	{
		m_uBatteryOnRemoveAnimID = xValue.GetHashIDValue();
	}
}

bool AVP_ClientEntity_InteractBatteryReceptacle::IsActorUsingRightInteractionItem(const AVP_ClientEntity_Actor& xActor) const
{
	if(m_bToolInside) return true;        // Battery inside

	if(xActor.GetInventorySystem())
	{
		const AVP_Inventory_Item* pxCurrentWeapon = xActor.GetInventorySystem()->GetCurrentItem(uAVP_INVENTORY_GROUPHASH_WEAPON);
		return (pxCurrentWeapon && pxCurrentWeapon->GetItemType() == AVP_INVENTORYITEMTYPE_BATTERY);
	}
	return false;

}

Asura_Guid AVP_ClientEntity_InteractBatteryReceptacle::GetToolGuidFromActor (const AVP_ServerEntity_Actor& xActor) const
{
	return xActor.GetPickupGuid();
};

Asura_Hash_ID AVP_ClientEntity_InteractBatteryReceptacle::GetToolAnimRelatedToMove(Asura_Hash_ID uMoveHash)
{
	if ( uMoveHash == AVP_Human_Move_Interaction_PlugBattery::GetStaticHash())
	{
		return m_uBatteryOnInsertAnimID;
	}
	else if ( uMoveHash == AVP_Human_Move_Interaction_GrabBattery::GetStaticHash())
	{
		return m_uBatteryOnRemoveAnimID;
	}
	else if ( uMoveHash == AVP_Human_Move_Interaction_LetGoBattery::GetStaticHash())
	{
		return m_uPickupRestAnim;
	}
	else
	{
		return m_uPickupRestAnim;
	}
}

