// Fill out your copyright notice in the Description page of Project Settings.


#include "Widget/AIGCWindow.h"

#include "AIGC.h"
#include "Definations.h"
#include "JsonObjectConverter.h"
#include "WebSocketHandler.h"
#include "WebSocketManager.h"
#define LOCTEXT_NAMESPACE "FAIGCModule"


void SAIGCWindow::Construct(const FArguments& InArgs)
{
	ChildSlot[
		SNew(SVerticalBox)
		+ SVerticalBox::Slot()
		   .AutoHeight()
		   .Padding(10)
		   [
			   SAssignNew(ServerIP, SConfigItemWidget)
			   .Title(LOCTEXT("Server", "服务器ip"))
			   .HintText(LOCTEXT("HintServer", "请输入服务器ip"))
			   .DefaultText(LOCTEXT("InputServer", "127.0.0.1"))
		   ]
		+ SVerticalBox::Slot()
		   .AutoHeight()
		   .Padding(10)
		   [
			   SAssignNew(DataName, SConfigItemWidget)
			   .Title(LOCTEXT("DataName", "配表名称"))
			   .HintText(LOCTEXT("HintDataName", "请输入配表名称"))
		   ]
		+ SVerticalBox::Slot()
			   .AutoHeight()
			   .Padding(10)
			   [
				   SAssignNew(NPCName1, SConfigItemWidget)
				   .Title(LOCTEXT("Name1", "名称1"))
				   .HintText(LOCTEXT("HintName", "请输入名称"))
			   ]
		+ SVerticalBox::Slot()
		   .AutoHeight()
		   .Padding(10)
		   [
			   SAssignNew(NPCJob1, SConfigItemWidget)
			   .Title(LOCTEXT("Job1", "职业1"))
			   .HintText(LOCTEXT("HintJon", "请输入职业"))
		   ]
		+ SVerticalBox::Slot()
		   .AutoHeight()
		   .Padding(10)
		   [
			   SAssignNew(NPCName2, SConfigItemWidget)
			   .Title(LOCTEXT("Name2", "名称2"))
			   .HintText(LOCTEXT("HintName", "请输入名称"))
		   ]
		
		+ SVerticalBox::Slot()
		   .AutoHeight()
		   .Padding(10)
		   [
			   SAssignNew(NPCJob2, SConfigItemWidget)
			   .Title(LOCTEXT("Job2", "职业2"))
			   .HintText(LOCTEXT("HintJon", "请输入职业"))
		   ]
		+ SVerticalBox::Slot()
		   .AutoHeight()
		   .Padding(10)
		   [
			   SAssignNew(DialogScene, SConfigItemWidget)
			   .Title(LOCTEXT("DialogScene", "对话场景"))
			   .HintText(LOCTEXT("HintDialogScene", "请输入对话场景"))
		   ]
		+ SVerticalBox::Slot()
				   .AutoHeight()
				   .Padding(10)
			[
				SAssignNew(GenerateButton, SButton)
			   .Text(LOCTEXT("GenerateChat", "生成对话"))
			   .OnClicked_Lambda([this](){ OnAIGenerateClicked(); return FReply::Handled(); })
			]

		
		];

	// 注册绘制后回调
	RegisterActiveTimer(0.f, 
		FWidgetActiveTimerDelegate::CreateSP(
			this, 
			&SAIGCWindow::OnPostPaint
		)
	);
	
}

EActiveTimerReturnType SAIGCWindow::OnPostPaint(double X, float Arg)
{
	WebSocketManager = NewObject<UWebSocketManager>();
	WebSocketManager->InitWebSocket(*ServerIP->GetInputText());
	WebSocketManager->OnConnectDelegate.AddLambda([this](bool bSuccess)
	{
		GenerateButton->SetEnabled(bSuccess);
	});
	WebSocketManager->OnDataReceiveDelaget.AddRaw(this, &SAIGCWindow::HandleReceiveData);
	return EActiveTimerReturnType::Stop;
}


void SAIGCWindow::OnAIGenerateClicked()
{
	GenerateButton->SetEnabled(false);
	WebSocketManager->SendData(GeneratePromptJson());
}


void SAIGCWindow::HandleReceiveData(TArray<FString> AiMessages)
{
	GenerateButton->SetEnabled(true);
	FAIGCModule* ModulePtr = FModuleManager::GetModulePtr<FAIGCModule>("AIGC");
	if (ModulePtr) {
		FString DataPath = FString::Printf(TEXT("/Game/Test/%s.%s"), *DataName->GetInputText(), *DataName->GetInputText());
		for (auto message: AiMessages)
		{
			ModulePtr->CreateOrAddData(DataPath, message);
		}
	}
}

FString SAIGCWindow::GeneratePromptJson()
{
	FPrompt PromptData;
	PromptData.DialogContent = FDialogContent(DialogScene->GetInputText());
	PromptData.Persons.Add(FPersonInfo(NPCName1->GetInputText(), NPCJob1->GetInputText()));
	PromptData.Persons.Add(FPersonInfo(NPCName2->GetInputText(), NPCJob2->GetInputText()));
	// 结构体转JSON字符串
	FString OutputString;
	FJsonObjectConverter::UStructToJsonObjectString(
		FPrompt::StaticStruct(), 
		&PromptData, 
		OutputString, 
		0, 
		0
	);
	return OutputString;
}



#undef LOCTEXT_NAMESPACE
