#pragma once
#include "util.h"
static void replace_dataset_named_reference(tag_t dataset_tag, tag_t old_file,
	const char *new_file_path, char *ref_name)
{
	IMF_file_data_p_t file_data;
	int index = 0;
	ITKCALL(IMF_get_file_access(old_file, 0, &file_data));

	tag_t new_file_tag = NULLTAG;
	AOM_lock(old_file);

	ITKCALL(IMF_replace_file_and_get_new_tag(old_file, new_file_path,
		TRUE, &new_file_tag));

	AOM_lock(dataset_tag);

	ITKCALL(AE_replace_dataset_named_ref2(dataset_tag, old_file, ref_name,
		AE_PART_OF, new_file_tag));
	ITKCALL(AE_save_myself(dataset_tag));
	AOM_unlock(dataset_tag);
	ITKCALL(IMF_release_file_access(&file_data));
	//	ITKCALL2(AOM_lock_for_delete(old_file));
	//	ITKCALL2(AOM_delete(old_file));
}

int bs_sign_cad(EPM_action_message_t msg)
{
	int ifail = ITK_ok, att_count,tag_cnt,form_cnt;
	tag_t rootTask, *attachments, *tags,ref,*forms;
	TC_argument_list_t * arguments = msg.arguments;
	char *name,*val;
	int arg_cnt = TC_number_of_arguments(arguments), status = 0;
	POM_AM__set_application_bypass(true);
	map<string, string> paras;
	string tempdir = getenv("temp");
	for (auto i = 0; i<arg_cnt; i++)
	{
		char *temp_key, *temp_val;
		ITK_ask_argument_named_value(TC_next_argument(arguments), &temp_key, &temp_val);
		paras[temp_key] = temp_val;
	}
	vector<string> prop_vec;
	map<string, string> prop_map;
	split(paras["prop"], ",", &prop_vec);
	for(int i=0;i<prop_vec.size();i++)
	{
		vector<string> temp_vec;
		split(prop_vec[i], "=", &temp_vec);
		if (temp_vec.size() == 2)
			prop_map[temp_vec[0]] = temp_vec[1];
	}
	EPM_ask_root_task(msg.task, &rootTask);
	Testclass ^test = gcnew Testclass();
	EPM_ask_attachments(rootTask, EPM_target_attachment, &att_count, &attachments);
	AE_reference_type_t ae_ref;
	POM_AM__set_application_bypass(true);
	for(int i=0;i<att_count;i++)
	{
		if(isTypeOf(attachments[i],paras["type"].c_str()))
		{
			printf("get item\n");
			AOM_ask_value_tags(attachments[i], "TC_Attaches", &tag_cnt, &tags);
			AOM_ask_value_tags(attachments[i], "IMAN_master_form_rev", &form_cnt, &forms);

			for(int j=0;j<tag_cnt;j++)
			{
				AE_ask_dataset_named_ref2(tags[j], "JD2_dwg", &ae_ref, &ref);
				if(ref)
				{
					IMF_ask_original_file_name2(ref, &name);
					stringstream path;
					path << tempdir <<"\\"<< newGUID() << name;
					printf("path:%s\n", path.str().c_str());
					IMF_export_file(ref, path.str().c_str());
					System::Collections::Generic::Dictionary<String^, String^> ^dic = gcnew System::Collections::Generic::Dictionary<String^, String^>();
					for(auto k=prop_map.begin();k!=prop_map.end();k++)
					{
						AOM_UIF_ask_value(forms[0], k->second.c_str(), &val);
						dic[convert_to_cstring(k->first.c_str())] = convert_to_cstring(val);
					}
					string result= ConvertToString(test->signCad(convert_to_cstring(path.str().c_str()), convert_to_cstring(paras["title"].c_str()), dic));
					if(result.length())
					{
						replace_dataset_named_reference(tags[j], ref, result.c_str(), "JD2_dwg");
					}
				}
			}
		}
	}
	POM_AM__set_application_bypass(false);

	return 0;
}

