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


#include "LovenseSupportActor.h"
#include <LovenseTypes.h>
#include <LovenseManager.h>
#include <LovenseIntegration.h>

// Sets default values
ALovenseSupportActor::ALovenseSupportActor()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void ALovenseSupportActor::BeginPlay()
{
	Super::BeginPlay();
	///Script/Engine.DataTable'/LovenseIntegration/DataTables/LovenseToySupport.LovenseToySupport'
	LovenseSupportDataTable = Cast<UDataTable>(StaticLoadObject(UDataTable::StaticClass(), nullptr, TEXT("/Script/Engine.DataTable'/LovenseIntegration/DataTables/LovenseToySupport.LovenseToySupport'")));
	FString contextString = TEXT("GET");
	TArray<FName> _rowNames = LovenseSupportDataTable->GetRowNames();
	for (auto _data : _rowNames) {
		FSupportStruct* support = LovenseSupportDataTable->FindRow<FSupportStruct>(_data, contextString, false);
			TArray<ELovenseCommandType> supportCommands;
			TSharedPtr<FJsonObject> jsonObject = MakeShareable(new FJsonObject());
			TSharedRef<TJsonReader<TCHAR>> jsonReader = TJsonReaderFactory<TCHAR>::Create(support->func);
			FJsonSerializer::Deserialize(jsonReader, jsonObject);

				if (jsonObject->HasField(TEXT("v"))) {
					bool enable = jsonObject->GetBoolField(TEXT("v"));
					if (enable) {
						supportCommands.Add(ELovenseCommandType::VIBRATE);
					}
				}
				if (jsonObject->HasField(TEXT("v1"))) {
					bool enable = jsonObject->GetBoolField(TEXT("v1"));
					if (enable) {
						supportCommands.Add(ELovenseCommandType::VIBRATE1);
					}
				}
				if (jsonObject->HasField(TEXT("v2"))) {
					bool enable = jsonObject->GetBoolField(TEXT("v2"));
					if (enable) {
						supportCommands.Add(ELovenseCommandType::VIBRATE2);
					}
				}
				if (jsonObject->HasField(TEXT("v3"))) {
					bool enable = jsonObject->GetBoolField(TEXT("v3"));
					if (enable) {
						supportCommands.Add(ELovenseCommandType::VIBRATE3);
					}
				}
				if (jsonObject->HasField(TEXT("r"))) {
					bool enable = jsonObject->GetBoolField(TEXT("r"));
					if (enable) {
						supportCommands.Add(ELovenseCommandType::ROTATE);
					}
				}
				if (jsonObject->HasField(TEXT("p"))) {
					bool enable = jsonObject->GetBoolField(TEXT("p"));
					if (enable) {
						supportCommands.Add(ELovenseCommandType::PUMP);
					}
				}
				if (jsonObject->HasField(TEXT("s"))) {
					bool enable = jsonObject->GetBoolField(TEXT("s"));
					if (enable) {
						supportCommands.Add(ELovenseCommandType::SUCTION);
					}
				}
				if (jsonObject->HasField(TEXT("t"))) {
					bool enable = jsonObject->GetBoolField(TEXT("t"));
					if (enable) {
						supportCommands.Add(ELovenseCommandType::THRUST);
					}
				}
				if (jsonObject->HasField(TEXT("d"))) {
					bool enable = jsonObject->GetBoolField(TEXT("d"));
					if (enable) {
						supportCommands.Add(ELovenseCommandType::DEPTH);
					}
				}
				if (jsonObject->HasField(TEXT("pos"))) {
					bool enable = jsonObject->GetBoolField(TEXT("pos"));
					if (enable) {
						supportCommands.Add(ELovenseCommandType::POSITION);
					}
				}
				FString supportType = support->type.ToLower().Replace(TEXT(" "), TEXT(""));
				if (supportType != TEXT("version")) {
					if (!typeSupportMap.Contains(supportType)) {
						typeSupportMap.Add(supportType, supportCommands);
					}
				}
	}
	FLovenseIntegrationModule::Get().InitToySupport(typeSupportMap);

	
	
}

// Called every frame
void ALovenseSupportActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

