{
	Purpose: Export the selected form values to a JSON array that PapyrusUtils can read.
	Game: The Elder Scrolls V: Skyrim
	Author: Kaxat
	Version: 1.3
	
	Hotkey: Ctrl+Shift+J
}

unit UserScript;

var 
	tJsonItems, tJsonComments: TwbFastStringList;
	sHasKeywordEdid, sJsonArrayKey: String;
	bOutputComments: Boolean;

uses
  StrUtils;


function Initialize: integer;
begin
	bOutputComments := 1;
	sHasKeywordEdid := '';
	sJsonArrayKey := 'yourArray';
	tJsonItems := TStringList.Create;
	tJsonComments := TStringList.Create;
	
	AddMessage('--------------------------------------');
	
	if not (sHasKeywordEdid = '') then begin
		
		AddMessage('Finding all items with the keyword: ' + sHasKeywordEdid);
		AddMessage('--------------------------------------');
	
	end;
	
	Result := 0;
end;

// The form ID relative to the plugin i.e. 00000801
function GetLocalID(e: IInterface): Integer;
begin
	Result := FileFormIDtoLoadOrderFormID(FileByIndex(0), FormID(e));
end;

// Plugin file that created this record
function GetPluginFile(e: IInterface): String;
begin
	Result := GetFileName(GetFile(MasterOrSelf(e)));
end;

// Json Form string formatted for PapyrusUtils version 3.3
function GetJSONFormString(e: IInterface): String;
begin
	Result := '"0x' + IntToHex(GetLocalID(e),1) + '|' + GetPluginFile(e) + '"';
end;


procedure OutputJSONArray(bTrailingComma: Boolean);
var
	i: int;
	sRow: String;
begin
	AddMessage('"' + sJsonArrayKey + '" : [ ');
	for i := 0 to (tJsonItems.Count - 1) do begin
		if i < (tJsonItems.Count - 1) then
			sRow := '	' + tJsonItems[i] + ','
		else
			sRow := '	' + tJsonItems[i];
			
		if bOutputComments then sRow := sRow + '	/* ' + tJsonComments[i] + ' */';

		AddMessage(sRow);
	end;
			
	if (bTrailingComma = 1) then
		AddMessage('],')
	else
		AddMessage(']');
	
end;


function Process(e: IInterface): integer;
begin

	if HasKeyword(e, sHasKeywordEdid) then begin
		tJsonItems.Add(GetJSONFormString(e));
		tJsonComments.Add(GetEditValue(ElementByPath(e,'EDID')) + ' Dec.' + IntToStr(GetLocalID(e)));
	end;
	
end;


function HasKeyword(e: IInterface; KeywordEdid: String): Boolean;
var
	i: Integer;
	tKeyword, tKeywords: IInterface;
	sKeywordString: String;
begin

	if KeywordEdid = '' then begin
		Result := 1;
		exit;
	end;
	
	tKeywords := ElementByPath(e, 'KWDA');
	
	for i := 0 to ElementCount(tKeywords) - 1 do begin
		tKeyword := LinksTo(ElementByIndex(tKeywords, i));
		sKeywordString := GetEditValue(ElementByPath(tKeyword, 'EDID'));
		
		if sKeywordString = KeywordEdid then begin
			Result := 1;
			exit;
		end;
	
	end;
	
	
	Result := 0;
	exit;

end;


function Finalize: integer;
var
	i: Integer;
	sOutputDir, sOutputTsvFile, sOutputJsonFile: String;
begin
	
	if (tJsonItems.Count > 0) then begin
		OutputJSONArray(1);
	end
	else begin
		AddMessage('Found 0 items.');
	end;
	
	AddMessage('--------------------------------------');

	tJsonItems.Free;
	tJsonComments.Free;
	
	Result := 0;
end;

end.
