// © 2025 Naked People Team. All Rights Reserved.


#include "CensorshipComponent.h"

#include "Components/StaticMeshComponent.h"
#include "NakedDesire/Clothing/BodyPart.h"
#include "NakedDesire/Clothing/ClothingManager.h"
#include "NakedDesire/Global/Constants.h"
#include "NakedDesire/Global/NakedDesireUserSettings.h"

UCensorshipComponent::UCensorshipComponent()
{
	PrimaryComponentTick.bCanEverTick = false;
}

void UCensorshipComponent::Initialize(UClothingManager* InClothingManager,
	UStaticMeshComponent* InBoobL, UStaticMeshComponent* InBoobR,
	UStaticMeshComponent* InVagina, UStaticMeshComponent* InAnal)
{
	ClothingManager = InClothingManager;
	BoobLCensorship = InBoobL;
	BoobRCensorship = InBoobR;
	VaginaCensorship = InVagina;
	AnalCensorship = InAnal;

	if (ClothingManager)
	{
		ClothingManager->OnClothingEquip.AddUniqueDynamic(this, &UCensorshipComponent::HandleClothingChanged);
		ClothingManager->OnClothingUnequip.AddUniqueDynamic(this, &UCensorshipComponent::HandleClothingChanged);
	}

	if (UNakedDesireUserSettings* Settings = UNakedDesireUserSettings::GetNakedDesireUserSettings())
	{
		Settings->OnSettingsChanged.AddUniqueDynamic(this, &UCensorshipComponent::HandleSettingsChanged);
	}

	RefreshCensorship();
}

void UCensorshipComponent::RefreshCensorship()
{
	if (!ClothingManager)
		return;

	const bool bCensor = ShouldCensor();

	SetBodyPartCensored(EBodyPart::Boobs, bCensor && ClothingManager->IsBodyPartExposed(EBodyPart::Boobs));
	SetBodyPartCensored(EBodyPart::Genitals, bCensor && ClothingManager->IsBodyPartExposed(EBodyPart::Genitals));
	SetBodyPartCensored(EBodyPart::Ass, bCensor && ClothingManager->IsBodyPartExposed(EBodyPart::Ass));
}

void UCensorshipComponent::HandleClothingChanged(UClothingItemInstance* ClothingItemInstance)
{
	RefreshCensorship();
}

void UCensorshipComponent::HandleSettingsChanged(UNakedDesireUserSettings* Settings)
{
	RefreshCensorship();
}

bool UCensorshipComponent::ShouldCensor() const
{
	if (IS_DEMO)
		return true;

	const UNakedDesireUserSettings* Settings = UNakedDesireUserSettings::GetNakedDesireUserSettings();
	return Settings && Settings->GetIsCensorshipEnabled();
}

void UCensorshipComponent::SetBodyPartCensored(EBodyPart BodyPart, bool bCensored)
{
	switch (BodyPart)
	{
	case EBodyPart::Boobs:
		if (BoobLCensorship)
			BoobLCensorship->SetVisibility(bCensored);
		if (BoobRCensorship)
			BoobRCensorship->SetVisibility(bCensored);
		break;
	case EBodyPart::Genitals:
		if (VaginaCensorship)
			VaginaCensorship->SetVisibility(bCensored);
		break;
	case EBodyPart::Ass:
		if (AnalCensorship)
			AnalCensorship->SetVisibility(bCensored);
		break;
	default:
		break;
	}
}