/**
 * Copyright 2022-2025 Theai, Inc. dba Inworld AI
 */

#include "Graph/RuntimeData/InworldGraphRuntimeData_EmotionState.h"

#include "Graph/InworldGraph.h"

UInworldGraphRuntimeData_EmotionState::UInworldGraphRuntimeData_EmotionState()
{
	Key = "Emotion";
}

FInworldDataHandle UInworldGraphRuntimeData_EmotionState::GetData()
{
	return FInworldData_Struct(GetEmotion());
}

FInworldEmotion UInworldGraphRuntimeData_EmotionState::GetEmotion() const
{
	return Emotion;
}

EInworldEmotionLabel UInworldGraphRuntimeData_EmotionState::GetEmotionLabel() const
{
	return Emotion.Label;
}

FString UInworldGraphRuntimeData_EmotionState::GetEmotionLabelString() const
{
	return Emotion.GetLabelString();
}

void UInworldGraphRuntimeData_EmotionState::SetEmotionLabel(const EInworldEmotionLabel& InEmotionLabel)
{
	Emotion.Label = InEmotionLabel;
}

void UInworldGraphRuntimeData_EmotionState::UpdateEmotion(const FString& TextResponse)
{
	TOptional<EInworldEmotionLabel> OptionalEmotion = ParseEmotionResponse(TextResponse);
	if (OptionalEmotion.IsSet())
	{
		Emotion.Label = OptionalEmotion.GetValue();
		UE_LOG(LogInworldGraph, Log, TEXT("Emotion state updated: %s"), *GetEmotionLabelString());
	}
}

TOptional<EInworldEmotionLabel> UInworldGraphRuntimeData_EmotionState::ParseEmotionResponse(const FString& TextResponse)
{
	if (TextResponse.Equals(TEXT("Neutral"), ESearchCase::IgnoreCase))
	{
		return EInworldEmotionLabel::NEUTRAL;
	}
	else if (TextResponse.Equals(TEXT("Contempt"), ESearchCase::IgnoreCase))
	{
		return EInworldEmotionLabel::CONTEMPT;
	}
	else if (TextResponse.Equals(TEXT("Belligerence"), ESearchCase::IgnoreCase))
	{
		return EInworldEmotionLabel::BELLIGERENCE;
	}
	else if (TextResponse.Equals(TEXT("Domineering"), ESearchCase::IgnoreCase))
	{
		return EInworldEmotionLabel::DOMINEERING;
	}
	else if (TextResponse.Equals(TEXT("Criticism"), ESearchCase::IgnoreCase))
	{
		return EInworldEmotionLabel::CRITICISM;
	}
	else if (TextResponse.Equals(TEXT("Anger"), ESearchCase::IgnoreCase))
	{
		return EInworldEmotionLabel::ANGER;
	}
	else if (TextResponse.Equals(TEXT("Tension"), ESearchCase::IgnoreCase))
	{
		return EInworldEmotionLabel::TENSION;
	}
	else if (TextResponse.Equals(TEXT("Tense Humor"), ESearchCase::IgnoreCase))
	{
		return EInworldEmotionLabel::TENSE_HUMOR;
	}
	else if (TextResponse.Equals(TEXT("Defensiveness"), ESearchCase::IgnoreCase))
	{
		return EInworldEmotionLabel::DEFENSIVENESS;
	}
	else if (TextResponse.Equals(TEXT("Whining"), ESearchCase::IgnoreCase))
	{
		return EInworldEmotionLabel::WHINING;
	}
	else if (TextResponse.Equals(TEXT("Sadness"), ESearchCase::IgnoreCase))
	{
		return EInworldEmotionLabel::SADNESS;
	}
	else if (TextResponse.Equals(TEXT("Stonewalling"), ESearchCase::IgnoreCase))
	{
		return EInworldEmotionLabel::STONEWALLING;
	}
	else if (TextResponse.Equals(TEXT("Interest"), ESearchCase::IgnoreCase))
	{
		return EInworldEmotionLabel::INTEREST;
	}
	else if (TextResponse.Equals(TEXT("Validation"), ESearchCase::IgnoreCase))
	{
		return EInworldEmotionLabel::VALIDATION;
	}
	else if (TextResponse.Equals(TEXT("Humor"), ESearchCase::IgnoreCase))
	{
		return EInworldEmotionLabel::HUMOR;
	}
	else if (TextResponse.Equals(TEXT("Affection"), ESearchCase::IgnoreCase))
	{
		return EInworldEmotionLabel::AFFECTION;
	}
	else if (TextResponse.Equals(TEXT("Surprise"), ESearchCase::IgnoreCase))
	{
		return EInworldEmotionLabel::SURPRISE;
	}
	else if (TextResponse.Equals(TEXT("Joy"), ESearchCase::IgnoreCase))
	{
		return EInworldEmotionLabel::JOY;
	}
	else if (TextResponse.Equals(TEXT("Disgust"), ESearchCase::IgnoreCase))
	{
		return EInworldEmotionLabel::DISGUST;
	}

	UE_LOG(LogInworldGraph, Warning,
		TEXT("UInworldGraphRuntimeData_EmotionState::ParseEmotionResponse - "
			 "Failed to parse emotion response value for: %s"),
		*TextResponse);
	return TOptional<EInworldEmotionLabel>();
}
