#include "handlers.h"

int NHL_SignOff_Handler(EPM_action_message_t msg){
	
	printf("*******************************************************************************\n");
	printf("*                      NHL_SignOff_Handler is comming £¡                      *\n");
	printf("*******************************************************************************\n");

	int customError = ITK_ok, n_args = 0, attachcount = 0;
	char *trans_args_value = NULL, *para_flag = NULL, *para_value = NULL, *approver_attr = NULL, *approvedate_attr = NULL, *object_type = NULL;
	tag_t job_tag = NULLTAG, root_task_tag = NULLTAG, *attaches = NULL, form_tag = NULLTAG;

	n_args = TC_number_of_arguments(msg.arguments);
	TC_init_argument_list(msg.arguments);
	for (int ix = 0; ix < n_args; ix++)
	{
		trans_args_value = TC_next_argument(msg.arguments);

		ITKCALL(ITK_ask_argument_named_value(trans_args_value, &para_flag, &para_value));

		if (tc_strcmp(para_flag, "approver_attr") == 0)
		{
			approver_attr = (char*)MEM_alloc((tc_strlen(para_value) + 1) * sizeof(char*));
			tc_strcpy(approver_attr, para_value);
		}
		else if (tc_strcmp(para_flag, "approvedate_attr") == 0)
		{
			approvedate_attr = (char*)MEM_alloc((strlen(para_value) + 1) * sizeof(char*));
			tc_strcpy(approvedate_attr, para_value);
		}

		if (para_flag) { MEM_free(para_flag); para_flag = NULL; }
		if (para_value) { MEM_free(para_value); para_value = NULL; }
	}
	//²éÑ¯job
	ITKCALL(EPM_ask_job(msg.task, &job_tag));
	//²éÑ¯rootTask	
	ITKCALL(EPM_ask_root_task(job_tag, &root_task_tag));

	ITKCALL(EPM_ask_attachments(root_task_tag, EPM_target_attachment, &attachcount, &attaches));
	char *form_type = "";
	for (int ix = 0; ix < attachcount; ix++)
	{
		ITKCALL(WSOM_ask_object_type2(attaches[ix], &object_type));
		if (tc_strcmp(object_type, form_type) == 0)
		{
			form_tag = attaches[ix];
			break;
		}
	}
	if (attaches) { MEM_free(attaches); attaches = NULL; }

	return ITK_ok;
}