#include "PlayerCinematic.h"
#include "NakedDesireCharacter.h"
#include "Kismet/GameplayStatics.h"
#include "NakedDesire/Clothing/ClothingItem.h"
#include "NakedDesire/Clothing/ClothingItemInstance.h"
#include "NakedDesire/Clothing/ClothingManager.h"


// Sets default values
APlayerCinematic::APlayerCinematic()
{
	PrimaryActorTick.bCanEverTick = false;
	
	NipplesMeshComponent = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Nipples"));
	NipplesMeshComponent->SetupAttachment(GetMesh());
	AnalMeshComponent = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Anal"));
	AnalMeshComponent->SetupAttachment(GetMesh());
	VaginaMeshComponent = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Vagina"));
	VaginaMeshComponent->SetupAttachment(GetMesh());
	HeadMeshComponent = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Head"));
	HeadMeshComponent->SetupAttachment(GetMesh());
	NeckMeshComponent = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Neck"));
	NeckMeshComponent->SetupAttachment(GetMesh());
	FaceMeshComponent = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Face"));
	FaceMeshComponent->SetupAttachment(GetMesh());
	EyesMeshComponent = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Eyes"));
	EyesMeshComponent->SetupAttachment(GetMesh());
	BodyMeshComponent = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Body"));
	BodyMeshComponent->SetupAttachment(GetMesh());
	TopMeshComponent = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Top"));
	TopMeshComponent->SetupAttachment(GetMesh());
	BottomMeshComponent = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Bottom"));
	BottomMeshComponent->SetupAttachment(GetMesh());
	BraMeshComponent = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Bra"));
	BraMeshComponent->SetupAttachment(GetMesh());
	PantiesMeshComponent = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Panties"));
	PantiesMeshComponent->SetupAttachment(GetMesh());
	SocksMeshComponent = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Socks"));
	SocksMeshComponent->SetupAttachment(GetMesh());
	ShoesMeshComponent = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Shoes"));
	ShoesMeshComponent->SetupAttachment(GetMesh());
}

void APlayerCinematic::BeginPlay()
{
	Super::BeginPlay();

	Player = Cast<ANakedDesireCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
	if (!Player)
	{
		return;
	}

	Player->ClothingManager->OnClothingEquip.AddUniqueDynamic(this, &APlayerCinematic::OnClothingEquip);
	Player->ClothingManager->OnClothingUnequip.AddUniqueDynamic(this, &APlayerCinematic::OnClothingUnequip);

	for (const UClothingItemInstance* ClothingItemInstance : Player->ClothingManager->GetEquippedClothing())
	{
		EquipClothing(ClothingItemInstance);
	}
}

void APlayerCinematic::OnClothingEquip(const UClothingItemInstance* ClothingItemInstance)
{
	EquipClothing(ClothingItemInstance);
}

void APlayerCinematic::OnClothingUnequip(const UClothingItemInstance* ClothingItemInstance)
{
	UnequipClothing(ClothingItemInstance);
}

USkeletalMeshComponent* APlayerCinematic::GetMeshByType(const EClothingSlotType SlotType) const
{
	switch (SlotType)
	{
	case EClothingSlotType::Nipples:
		return NipplesMeshComponent;
	case EClothingSlotType::Anal:
		return AnalMeshComponent;
	case EClothingSlotType::Vagina:
		return VaginaMeshComponent;
	case EClothingSlotType::Head:
		return HeadMeshComponent;
	case EClothingSlotType::Neck:
		return NeckMeshComponent;
	case EClothingSlotType::Face:
		return FaceMeshComponent;
	case EClothingSlotType::Eyes:
		return EyesMeshComponent;
	case EClothingSlotType::Body:
		return BodyMeshComponent;
	case EClothingSlotType::Top:
		return TopMeshComponent;
	case EClothingSlotType::Bottom:
		return BottomMeshComponent;
	case EClothingSlotType::Bra:
		return BraMeshComponent;
	case EClothingSlotType::Panties:
		return PantiesMeshComponent;
	case EClothingSlotType::Socks:
		return SocksMeshComponent;
	case EClothingSlotType::Shoes:
		return ShoesMeshComponent;
	default:
		return NipplesMeshComponent;
	}
}

void APlayerCinematic::EquipClothing(const UClothingItemInstance* ClothingItemInstance)
{
	USkeletalMeshComponent* MeshComponent = GetMeshByType(ClothingItemInstance->GetClothingItem()->SlotType);
	MeshComponent->SetSkeletalMesh(ClothingItemInstance->GetClothingItem()->SkeletalMesh);
	if (ClothingItemInstance->GetClothingItem()->UseLeaderPose)
	{
		MeshComponent->SetLeaderPoseComponent(GetMesh());
	}

	if (!ClothingItemInstance->GetClothingItem()->Materials.IsEmpty())
	{
		for (const TPair<FName, UMaterialInstance*>& Material : ClothingItemInstance->GetClothingItem()->Materials)
		{
			MeshComponent->SetMaterialByName(Material.Key, Material.Value);
		}
	}
}

void APlayerCinematic::UnequipClothing(const UClothingItemInstance* ClothingItemInstance)
{
	USkeletalMeshComponent* MeshComponent = GetMeshByType(ClothingItemInstance->GetClothingItem()->SlotType);
	MeshComponent->SetSkeletalMesh(nullptr);
	MeshComponent->SetLeaderPoseComponent(nullptr);
}
