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

#include "NakedDesireGameMode.h"
#include "Kismet/GameplayStatics.h"
#include "NakedDesire/Clothing/ClothingItemDefinition.h"
#include "NakedDesire/Clothing/ClothingItemInstance.h"
#include "NakedDesire/Interactables/ItemPickup.h"
#include "UObject/ConstructorHelpers.h"
#include "NakedDesire/Interactables/Wardrobe.h"
#include "NakedDesire/MissionBuilder/MissionsConfig.h"
#include "NakedDesire/MissionBuilder/MissionsManager.h"
#include "NakedDesire/Player/NakedDesireCharacter.h"
#include "NakedDesire/SaveGame/GlobalSaveGameData.h"
#include "NakedDesire/SaveGame/ItemSaveRecord.h"
#include "NakedDesire/SaveGame/SaveSubsystem.h"

void ANakedDesireGameMode::RestartGame()
{
	UGameplayStatics::OpenLevel(this, "City");
}

AWardrobe* ANakedDesireGameMode::GetWardrobe() const
{
	return Wardrobe;
}

void ANakedDesireGameMode::BuyItem(UClothingItemInstance* ClothingItemInstance)
{
	USaveSubsystem* SaveSubsystem = UGameplayStatics::GetGameInstance(GetWorld())->GetSubsystem<USaveSubsystem>();
	UGlobalSaveGameData* SaveGame = SaveSubsystem->GetCurrentSave();
	if (!SaveGame)
	{
		UE_LOG(LogTemp, Error, TEXT("ANakedDesireGameMode::BuyItem Couldn't load save game"));
		return;
	}

	if (SaveGame->Money < ClothingItemInstance->GetClothingItemDefinition()->BasePrice)
		return;

	SaveGame->Money -= ClothingItemInstance->GetClothingItemDefinition()->BasePrice;
	Wardrobe->AddItem(ClothingItemInstance);
}

void ANakedDesireGameMode::OnHourChanged(int32 Hour)
{
	USaveSubsystem* SaveSubsystem = UGameplayStatics::GetGameInstance(GetWorld())->GetSubsystem<USaveSubsystem>();
	UGlobalSaveGameData* SaveGame = SaveSubsystem->GetCurrentSave();
	if (!SaveGame)
	{
		UE_LOG(LogTemp, Error, TEXT("ANakedDesireGameMode::BuyItem Couldn't load save game"));
		return;
	}
	
	if (Hour == 4)
	{
		SaveGame->DaysPassed++;
		RefreshDailyMissions();
	}
}

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

	if (AActor* FoundActor = UGameplayStatics::GetActorOfClass(GetWorld(), AWardrobe::StaticClass()))
	{
		if (AWardrobe* WardrobeActor = Cast<AWardrobe>(FoundActor))
		{
			Wardrobe = WardrobeActor;
		}
	}
	
	USaveSubsystem* SaveSubsystem = UGameplayStatics::GetGameInstance(GetWorld())->GetSubsystem<USaveSubsystem>();
	for (const FItemSaveRecord& Item : SaveSubsystem->GetCurrentSave()->GetWorldItems())
	{
		UClothingItemInstance* NewItemInstance = Cast<UClothingItemInstance>(UItemInstance::CreateFromRecord(this, Item));
		if (!NewItemInstance)
			continue;

		AItemPickup* NewItemPickup = GetWorld()->SpawnActor<AItemPickup>(ItemPickupClass, Item.WorldTransform);
		NewItemPickup->SetItem(NewItemInstance);
	}
}

void ANakedDesireGameMode::RefreshDailyMissions()
{
	const ANakedDesireCharacter* Player = Cast<ANakedDesireCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
	if (!Player)
		return;
	
	USaveSubsystem* SaveSubsystem = UGameplayStatics::GetGameInstance(GetWorld())->GetSubsystem<USaveSubsystem>();
	
	int ClampedIndex = FMath::Clamp(SaveSubsystem->GetCurrentSave()->DaysPassed , 0, MissionsConfig->DailyMissions.Num() - 1);
	Player->MissionsManager->RefreshDailyMissions(MissionsConfig->DailyMissions[ClampedIndex].Missions);
}
