// © 2024 Long Ly. All rights reserved. Any unauthorized use, reproduction, or distribution of this trademark is strictly prohibited and may result in legal action.


#include "Data/OLSMaskAnimationData.h"

void FOLSMaskAnimation::CacheAdditiveCurves()
{
#if WITH_EDITOR
	if (!Animation)
	{
		CurvesToModify.Reset();
		return;
	}

	MaskPoseTime = FMath::Clamp(MaskPoseTime, 0.f, Animation->GetPlayLength());

	//Set values for Arms Spaces To 0 Before Filling them from the Weights.
	MaskingWeights.RightArmWeights.ArmLocalSpaceBlend = 0.f;
	MaskingWeights.RightArmWeights.ArmMeshSpaceBlend = 0.f;
	MaskingWeights.LeftArmWeights.ArmLocalSpaceBlend = 0.f;
	MaskingWeights.LeftArmWeights.ArmMeshSpaceBlend = 0.f;

	//Fill arm spaces values with weights
	if (MaskingWeights.RightArmWeights.ArmAdditiveMode == EOLSAdditiveMode::ELocalSpace)
	{
		MaskingWeights.RightArmWeights.ArmLocalSpaceBlend = 1.f;
	}
	else
	{
		MaskingWeights.RightArmWeights.ArmMeshSpaceBlend = 1.f;
	}

	if (MaskingWeights.LeftArmWeights.ArmAdditiveMode == EOLSAdditiveMode::ELocalSpace)
	{
		MaskingWeights.LeftArmWeights.ArmLocalSpaceBlend = 1.f;
	}
	else
	{
		MaskingWeights.LeftArmWeights.ArmMeshSpaceBlend = 1.f;
	}
	
	CurvesToModify.Reset();
	
	// Legs
	CurvesToModify.Add(MaskLegsMeshSpaceCurveName, 1);
	CurvesToModify.Add(MaskLegsLocalSpaceCurveName, 0);
	CurvesToModify.Add(MaskLegsCurveName, MaskingWeights.LegsWeights.MaskWeight);
	CurvesToModify.Add(MaskLegsAddCurveName, MaskingWeights.LegsWeights.MaskAdditive);
	Weights.LegsWeights.OverrideAlpha = MaskingWeights.LegsWeights.MaskWeight;
	Weights.LegsWeights.AdditiveAlpha = MaskingWeights.LegsWeights.MaskAdditive;
	
	// Pelvis
	CurvesToModify.Add(MaskPelvisMeshSpaceCurveName, 1);
	CurvesToModify.Add(MaskPelvisLocalSpaceCurveName, 0);
	CurvesToModify.Add(MaskPelvisCurveName, MaskingWeights.PelvisWeights.MaskWeight);
	CurvesToModify.Add(MaskPelvisAddCurveName, MaskingWeights.PelvisWeights.MaskAdditive);
	Weights.PelvisWeights.OverrideAlpha = MaskingWeights.PelvisWeights.MaskWeight;
	Weights.PelvisWeights.AdditiveAlpha = MaskingWeights.PelvisWeights.MaskAdditive;
	
	// Spine
	CurvesToModify.Add(MaskSpineMeshSpaceCurveName, 1);
	CurvesToModify.Add(MaskSpineLocalSpaceCurveName, 0);
	CurvesToModify.Add(MaskSpineCurveName, MaskingWeights.SpineWeights.MaskWeight);
	CurvesToModify.Add(MaskSpineAddCurveName, MaskingWeights.SpineWeights.MaskAdditive);
	Weights.SpineWeights.OverrideAlpha = MaskingWeights.SpineWeights.MaskWeight;
	Weights.SpineWeights.AdditiveAlpha = MaskingWeights.SpineWeights.MaskAdditive;
	
	// Head
	CurvesToModify.Add(MaskHeadMeshSpaceCurveName, 1);
	CurvesToModify.Add(MaskHeadLocalSpaceCurveName, 0);
	CurvesToModify.Add(MaskHeadCurveName, MaskingWeights.HeadWeights.MaskWeight);
	CurvesToModify.Add(MaskHeadAddCurveName, MaskingWeights.HeadWeights.MaskAdditive);
	Weights.HeadWeights.OverrideAlpha = MaskingWeights.HeadWeights.MaskWeight;
	Weights.HeadWeights.AdditiveAlpha = MaskingWeights.HeadWeights.MaskAdditive;
	
	// Right Arm
	CurvesToModify.Add(MaskRightArmCurveName, MaskingWeights.RightArmWeights.MaskWeights.MaskWeight);
	CurvesToModify.Add(RightArmMeshSpaceCurveName, MaskingWeights.RightArmWeights.ArmMeshSpaceBlend);
	CurvesToModify.Add(RightArmLocalSpaceCurveName, MaskingWeights.RightArmWeights.ArmLocalSpaceBlend);
	CurvesToModify.Add(MaskRightArmAddCurveName, MaskingWeights.RightArmWeights.MaskWeights.MaskAdditive);
	Weights.RightArmWeights.OverrideAlpha = MaskingWeights.RightArmWeights.MaskWeights.MaskWeight;
	Weights.RightArmWeights.AdditiveAlpha = MaskingWeights.RightArmWeights.MaskWeights.MaskAdditive;
	Weights.RightArmWeights.LocalSpaceBlendAlpha = MaskingWeights.RightArmWeights.ArmLocalSpaceBlend;
	Weights.RightArmWeights.MeshSpaceBlendAlpha = MaskingWeights.RightArmWeights.ArmMeshSpaceBlend;
	
	// Left Arm
	CurvesToModify.Add(LeftArmMeshSpaceCurveName, MaskingWeights.LeftArmWeights.ArmMeshSpaceBlend);
	CurvesToModify.Add(LeftArmLocalSpaceCurveName, MaskingWeights.LeftArmWeights.ArmLocalSpaceBlend);
	CurvesToModify.Add(MaskLeftArmCurveName, MaskingWeights.LeftArmWeights.MaskWeights.MaskWeight);
	CurvesToModify.Add(MaskLeftArmAddCurveName, MaskingWeights.LeftArmWeights.MaskWeights.MaskAdditive);
	Weights.LeftArmWeights.OverrideAlpha = MaskingWeights.LeftArmWeights.MaskWeights.MaskWeight;
	Weights.LeftArmWeights.AdditiveAlpha = MaskingWeights.LeftArmWeights.MaskWeights.MaskAdditive;
	Weights.LeftArmWeights.LocalSpaceBlendAlpha = MaskingWeights.LeftArmWeights.ArmLocalSpaceBlend;
	Weights.LeftArmWeights.MeshSpaceBlendAlpha = MaskingWeights.LeftArmWeights.ArmMeshSpaceBlend;

	// Right Hand
	CurvesToModify.Add(RightHandMeshSpaceCurveName, 0);
	CurvesToModify.Add(RightHandLocalSpaceCurveName, 1);
	CurvesToModify.Add(RightHandCurveName, MaskingWeights.RightHandWeights.MaskWeight);
	CurvesToModify.Add(MaskRightHandAddCurveName, MaskingWeights.RightHandWeights.MaskAdditive);
	Weights.RightHandWeights.OverrideAlpha = MaskingWeights.RightHandWeights.MaskWeight;
	Weights.RightHandWeights.AdditiveAlpha = MaskingWeights.RightHandWeights.MaskAdditive;
	Weights.RightHandWeights.LocalSpaceBlendAlpha = 1.f;
	Weights.RightHandWeights.MeshSpaceBlendAlpha = 0.f;
	
	// Left Hand
	CurvesToModify.Add(LeftHandMeshSpaceCurveName, 0);
	CurvesToModify.Add(LeftHandLocalSpaceCurveName, 1);
	CurvesToModify.Add(LeftHandCurveName, MaskingWeights.LeftHandWeights.MaskWeight);
	CurvesToModify.Add(MaskLeftHandAddCurveName, MaskingWeights.LeftHandWeights.MaskAdditive);
	Weights.LeftHandWeights.OverrideAlpha = MaskingWeights.LeftHandWeights.MaskWeight;
	Weights.LeftHandWeights.AdditiveAlpha = MaskingWeights.LeftHandWeights.MaskAdditive;
	Weights.LeftHandWeights.LocalSpaceBlendAlpha = 1.f;
	Weights.LeftHandWeights.MeshSpaceBlendAlpha = 0.f;

#endif
}

void FOLSGaitMasks::CacheAdditiveCurves()
{
#if WITH_EDITOR
	ForwardMask.CacheAdditiveCurves();
	RightMask.CacheAdditiveCurves();
	LeftMask.CacheAdditiveCurves();
	BackwardMask.CacheAdditiveCurves();
#endif
}

void FOLSStanceMaskSets::CacheAdditiveCurves()
{
#if WITH_EDITOR
	for (TPair<FGameplayTag, FOLSGaitMasks>& pair : GaitMasks)
	{
		pair.Value.CacheAdditiveCurves();
	}

	IdleMask.CacheAdditiveCurves();
	AimMask.CacheAdditiveCurves();
#endif
}

const FOLSGaitMasks* FOLSStanceMaskSets::SelectGaitMasksByGait(const FGameplayTag& gait)
{
	return GaitMasks.Find(gait);
}

const FOLSMaskAnimation& FOLSStanceMaskSets::GetIdleMask() const
{
	return IdleMask;
}

const FOLSMaskAnimation& FOLSStanceMaskSets::GetAimMask() const
{
	return AimMask;
}

void FOLSStanceMaskSets::GetAimBlendInOut(float& outBlendIn, float& outBlendOut) const
{
	outBlendIn = AimBlendInTime;
	outBlendOut = AimBlendOutTime;
}