
#include "OrderTranslationManager.h"

#include "FayeDeveloperSettings.h"
#include "SUDSDialogue.h"
#include "SUDSLibrary.h"
#include "Faye/Data/OrderSettingsDataAsset.h"
#include "Faye/Data/Filters/ItemFilters.h"
#include "Faye/GameObjects/NpcPawn.h"

USUDSDialogue* UOrderTranslationManager::GetCoffeeOrderDialogue(UCoffeeFilter* InFilter, ANpcPawn* Npc)
{
	if (!InFilter ||!Npc) return nullptr;
	
	const UFayeDeveloperSettings* DeveloperSettings = GetDefault<UFayeDeveloperSettings>();
	auto ToFlag = [](ECoffeeFilterFlags Flag) -> uint8
	{
		return 1 << static_cast<uint8>(Flag);
	};
	
	uint8 ChosenFlag = ToFlag(ECoffeeFilterFlags::SizeAndType);
	
	if (InFilter->bIced)
	{
		ChosenFlag |= ToFlag(ECoffeeFilterFlags::Iced);
	}
	
	if (InFilter->CoffeeAddition != ECoffeeAddition::None)
	{
		ChosenFlag |= ToFlag(ECoffeeFilterFlags::CoffeeAddition);
	}
	
	if (InFilter->bWhippedCream)
	{
		ChosenFlag |= ToFlag(ECoffeeFilterFlags::WhippedCream);
	}
	
	const UOrderSettingsDataAsset* DataAsset = Npc->GetCustomOrderSettings()? Npc->GetCustomOrderSettings() : DeveloperSettings->GetOrderSettingDataAsset();
	const FCoffeeFilterFlagsSet* CoffeeFlagSet = DataAsset->FilterSettings.FindByPredicate([ChosenFlag](const FCoffeeFilterFlagsSet& FlagSet)
	{
		return FlagSet.Flags == ChosenFlag;
	});
	ensure(CoffeeFlagSet);
	
	USUDSScript* ChosenScript = CoffeeFlagSet->ScriptsArray[FMath::RandRange(0, CoffeeFlagSet->ScriptsArray.Num() - 1)];
	ensure(ChosenScript);
	
	USUDSDialogue* Dialogue = USUDSLibrary::CreateDialogueWithParticipant(Npc, ChosenScript, Npc, true);
	ensure(Dialogue);
	
	const FText* CoffeeTypeText = DeveloperSettings->CoffeeTranslations.CoffeeTypeTranslations.Find(InFilter->CoffeeType);
	ensure(CoffeeTypeText);
	
	const FText* CoffeeSizeText = DeveloperSettings->CoffeeTranslations.CoffeeSizeTranslations.Find(InFilter->CoffeeSize);
	ensure(CoffeeSizeText);
	
	Dialogue->SetVariableText("CoffeeType", *CoffeeTypeText);
	Dialogue->SetVariableText("CoffeeSize", *CoffeeSizeText);
	
	return Dialogue;
}
