#include "tc_util.h"
#include "chint_Handler.h"
// #include <easylogging++.h>
// #include "util.h"


int chint_set_prop_1(EPM_action_message_t msg)
{
	// LINFO << "  chint_set_prop    begin";
	tag_t root_task, *att, *ref;
	TC_argument_list_t * arguments = msg.arguments;
	int arg_cnt = TC_number_of_arguments(arguments);

	map<string, string> val_map1;
	map<string, string> val_map2;
	int n_cnt, n_ref;
	EPM_ask_root_task(msg.task, &root_task);
	EPM_ask_attachments(root_task, EPM_target_attachment, &n_cnt, &att);
	//AOM_ask_value_tags(root_task,"root_reference_attachments",&n_ref,&ref);
	EPM_ask_attachments(root_task, EPM_reference_attachment, &n_ref, &ref);
	map<string, map<string, string> > prop_map;
	for (auto i = 0; i < arg_cnt; i++)
	{
		int index_key1;
		char *temp_key, *temp_val;
		ITKCALL(ITK_ask_argument_named_value(TC_next_argument(arguments), &temp_key, &temp_val));
		printf("key:%s | value:%s\r\n",temp_key,temp_val);
		string temp_k(temp_key);
		string temp_v(temp_val);
		index_key1 = temp_k.find('.');
		if (index_key1 == -1)
			continue;
		string type = temp_k.substr(0,index_key1);
		string prop = temp_k.substr(index_key1+1);
		printf("类型:%s 属性名:%s 属性值:%s\r\n", type, prop, temp_v);
		prop_map[type][prop] = temp_v;

	}
	POM_AM__set_application_bypass(true);
	for (auto i = 0; i < n_cnt; i++)
	{
		for (auto it = prop_map.begin(); it != prop_map.end(); it++)
		{
			string type = it->first;
			// item_ask_type

			if (isTypeOf(att[i], type.c_str()))
			{
				map<string, string> prop_vals = it->second;
				ITKCALL(AOM_lock(att[i]));
				for (auto it2 = prop_vals.begin(); it2 != prop_vals.end(); it2++)
				{
					ITKCALL(AOM_set_value_string(att[i], it2->first.c_str(), it2->second.c_str()));
				}
				ITKCALL(AOM_save(att[i]));
				ITKCALL(AOM_unlock(att[i]));
				break;
			}
		}
	}
	POM_AM__set_application_bypass(false);
	// LINFO << "  chint_set_prop    end";

	return 0;
}

