#include "../script_component.hpp"

if (!hasInterface) exitWith {};

params [
	["_subjectID", "", [""]],
	["_recordTitle", "", [""]],
	["_recordText", "", [""]],
	["_recordIcon", "", [""]]
];

// Check if all parameters are provided
if (
	count _subjectID isEqualTo 0 ||
	count _recordTitle isEqualTo 0 ||
	count _recordText isEqualTo 0
) exitWith {
	[LEVEL_ERROR, QUOTE(COMPONENT), "Not all parameters provided"] call EFUNC(common,log);
};

// Check if already created
private _subjectRecords = GVAR(diaryRecords) getOrDefault [_subjectID, createHashMap, true];
private _existingRecord = _subjectRecords getOrDefault [_recordTitle, diaryRecordNull, true];

if (!isNull _existingRecord) then {
	player setDiaryRecordText [[_subjectID, _existingRecord], [_recordTitle, _recordText, _recordIcon]];
	
	[
		LEVEL_DEBUG,
		QUOTE(COMPONENT),
		format ["Updated diary record: %1", _recordTitle],
		[
			["subjectID", _subjectID],
			["recordTitle", _recordTitle]
		]
	] call EFUNC(common,log);
} else {
	private _new = player createDiaryRecord [
		_subjectID, 
		[
			_recordTitle, 
			_recordText,
			_recordIcon
		]
	];
	_subjectRecords set [_recordTitle, _new];
	GVAR(diaryRecords) set [_subjectID, _subjectRecords];

	[
		LEVEL_DEBUG,
		QUOTE(COMPONENT),
		format ["Created diary record: %1", _recordTitle],
		[
			["subjectID", _subjectID],
			["recordTitle", _recordTitle]
		]
	] call EFUNC(common,log);
};