#pragma once
#include "Engine/EngineTypes.h"
#include "MantisSettings.generated.h"

USTRUCT()
struct FBatchBinding
{
	GENERATED_BODY()

	/* Initialize the struct using a string from the config file. */
	FBatchBinding( FString& InString )
	{
		FString CleanString = InString.Mid( 1, InString.Len() - 2 );

		TArray<FString> VariableStrings;
		CleanString.ParseIntoArray( VariableStrings, TEXT(",") );

		TMap<FString, FString> Variables;
		for ( int32 i = 0; i < VariableStrings.Num(); i++ )
		{
			TArray<FString> VariableString;
			VariableStrings[i].ParseIntoArray( VariableString, TEXT( "=" ) );
			if ( VariableString.Num() != 2 ) continue;
			Variables.Add( VariableString[0], VariableString[1].TrimQuotes() );
		}

		FString* FieldIDValue = Variables.Find( "FieldID" );
		if ( FieldIDValue != nullptr ) FieldID = *FieldIDValue;
		FString* BatchPathValue = Variables.Find( "BatchPath" );
		if ( BatchPathValue != nullptr ) BatchPath = *BatchPathValue;
	}

	/* Default constructor. */
	FBatchBinding() {}

	/*
	 * The ID of the field.
	 * e.g. category_id
	*/
	UPROPERTY( Config, EditAnywhere )
	FString FieldID;

	/*
	 * The path to the batch script. This is can be a relative path from the project folder (where the .uproject is located) or an absolute path.
	 * Relative e.g. /Scripts/GetCategory.bat
	 * Absolute e.g. C:/Perforce/Scripts/GetCategory.bat
	*/
	UPROPERTY( Config, EditAnywhere )
	FString BatchPath;
};

UCLASS(config=Engine)
class UMantisSettings : public UObject
{
	GENERATED_UCLASS_BODY()
public:
	/* 
	 * This is the root url of the Mantis.
	 * e.g. if the login url is: http://bugs.yourdomain.com/login_page.php
	 * this would be http://bugs.youdomain.com/
	*/
	UPROPERTY( Config, EditAnywhere, Category = General )
	FString HostURL;

	/*
	* A list of batch scrips you want to use to load data for certain fields in the crash report.
	*/
	UPROPERTY( Config, EditAnywhere, Category = Advanced )
	TArray<FBatchBinding> ScriptBindings;
};