#include "util.h"
int bs_sap_material(EPM_action_message_t msg)
{
	printf("start sap material\n");
	int ifail = ITK_ok, att_count;
	tag_t rootTask, *attachments, relation_type;
	char *obj_type;
	int pref_cnt;
	char **pref_vals;
	EPM_ask_root_task(msg.task, &rootTask);
	map<int, string> class_map;

	EPM_ask_attachments(rootTask, EPM_target_attachment, &att_count, &attachments);
	TC_argument_list_t * arguments = msg.arguments;
	int arg_cnt = TC_number_of_arguments(arguments), status = 0;
	POM_AM__set_application_bypass(true);
	map<string, string> paras,rel_map;
	map<string, string> prop_map;

	vector<string> rel_vec;
	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;
	}
	string d0 = "D00", d1 = "D0";
	for(int i=0;i<67;i++)
	{
		if(i>8)
		{
			
			class_map[1000+i] =  d1 + to_string(i + 1);
		}else
		{
			class_map[1000 + i] = d0 + to_string(i + 1);


		}
	}
	PREF_ask_char_values("connor_sap", &pref_cnt, &pref_vals);
	split(pref_vals[4], ",", &rel_vec);
	for(int i=0;i<rel_vec.size();i++)
	{
		vector<string> temp_vec;
		split(rel_vec[i], "=", &temp_vec);
		if(temp_vec.size()==2)
		{
			rel_map[temp_vec[0]] = temp_vec[1];
		}
	}
	split(pref_vals[5], ",", &rel_vec);

	for (int i = 0; i<rel_vec.size(); i++)
	{
		vector<string> temp_vec;
		split(rel_vec[i], "=", &temp_vec);
		if (temp_vec.size() == 2)
		{
			prop_map[temp_vec[0]] = temp_vec[1];
		}
	}
	GRM_find_relation_type("IMAN_master_form", &relation_type);
	printf("create class\n");
	Testclass ^test = gcnew Testclass();
	printf("create success\n");
	String^ url = convert_to_cstring(pref_vals[0]);
	String^ user = convert_to_cstring(pref_vals[2]);
	String^ password = convert_to_cstring(pref_vals[3]);
	// prop_map["MTART"] = "jd2_wllx";
	// prop_map["MATNR"] = "item_id";
	// prop_map["MAKTX"] = "jd2_wlms";
	// prop_map["MEINS"] = "jd2_dldw";
	// prop_map["MATKL"] = "jd2_wlz";
	// prop_map["PRDHA"] = "jd2_cpcc";
	// prop_map["EXTWG"] = "jd2_wbwlz";
	// prop_map["MSTAE"] = "release_status_list";
	// prop_map["VOLUM"] = "jd2_tj";
	// prop_map["VOLEH"] = "jd2_tjdw";
	// prop_map["BISMT"] = "jd2_jwlh";
	// prop_map["ZEINR"] = "jd2_th";
	// prop_map["BRGEW"] = "jd2_mz";
	// prop_map["NTGEW"] = "jd2_jz";
	// prop_map["GEWEI"] = "jd2_zldw";
	// prop_map["CLASS"] = "jd2_wllx";
	vector<string> vec{ "jd2_wllx","item_id","jd2_wlms","jd2_jldw","jd2_wlz","jd2_cppc","jd2_wbwlz","release_status_list","jd2_tj","jd2_tjdw","jd2_jwlh","jd2_th","jd2_mz","jd2_jz","jd2_zldw" };
	printf("start111\n");
	for (int i = 0; i < att_count; i++)
	{
		if (isTypeOf(attachments[i], "ItemRevision"))
		{
			int rel_cnt;
			tag_t *rels;
			GRM_list_secondary_objects_only(attachments[i], relation_type, &rel_cnt, &rels);
			System::Collections::Generic::Dictionary<String^, String^> ^dic = gcnew System::Collections::Generic::Dictionary<String^, String^>();
			for (auto j = prop_map.begin(); j != prop_map.end(); j++)
			{
				char *val="";
				string temp_val;
				if (j->second == "item_id")
				{
					AOM_UIF_ask_value(attachments[i], j->second.c_str(), &val);
					temp_val = val;
				}
				else if (j->second == "release_status_list") {
					int status_cnt;
					tag_t *rel_status;
					AOM_ask_value_tags(attachments[i], j->second.c_str(), &status_cnt, &rel_status);
					if (status_cnt)
						AOM_UIF_ask_value(rel_status[status_cnt - 1], "object_string", &val);
					temp_val = rel_map[val];
				}
				else
				{
					AOM_UIF_ask_value(rels[0], j->second.c_str(), &val);
					temp_val = val;
				}
				
				dic[convert_to_cstring(j->first.c_str())] = convert_to_cstring(temp_val.c_str());
			}
			tag_t class_obj;
			ICS_ask_classification_object(attachments[i], &class_obj);
			if (class_obj)
			{
				char ***vals;
				int cnt, *ids,*val_count;
				ICS_ico_ask_attributes(class_obj, &cnt, &ids, &val_count, &vals);
				for(int j=0;j<cnt;j++)
				{
					printf("id:%d\n", ids[j]);
					if(class_map.find(ids[j])!=class_map.end())
					{
						if(val_count[j])
							dic[convert_to_cstring(class_map[ids[j]].c_str())] = convert_to_cstring(vals[j][0]);
					}
				}
			}
			dic["MBRSH"] = "M";
			dic["SPART"] = "00";
			dic["KLART"] = "001";
			string result = ConvertToString(test->send_material(url, user, password, dic));
			printf("result:%s\n", result.c_str());


		}
	}

	POM_AM__set_application_bypass(false);

	return 0;
}


void get_bom(System::Collections::Generic::List<System::Collections::Generic::Dictionary<String^, String^>^> ^a_list,tag_t line,char *werks,char *stlan)
{
	int line_cnt;
	tag_t *lines;
	BOM_line_ask_child_lines(line, &line_cnt, &lines);
	char *id;
	AOM_UIF_ask_value(line, "bl_item_item_id", &id);
	for (int j = 0; j < line_cnt; j++)
	{
		System::Collections::Generic::Dictionary<String^, String^> ^dic = gcnew System::Collections::Generic::Dictionary<String^, String^>();
		dic["MATNR"] = convert_to_cstring(id);
		dic["WERKS"] = convert_to_cstring(werks);
		dic["STLAN"] = convert_to_cstring(stlan);
		dic["BMENG"] = "1";
		dic["STLST"] = "1";
		dic["POSTP"] = "L";
		char *val;
		AOM_UIF_ask_value(lines[j], "bl_sequence_no", &val);
		dic["POSNR"] = convert_to_cstring(val);
		AOM_UIF_ask_value(lines[j], "bl_child_id", &val);
		dic["IDNRK"] = convert_to_cstring(val);

		AOM_UIF_ask_value(lines[j], "bl_quantity", &val);
		dic["MENGE"] = convert_to_cstring(val);

		AOM_UIF_ask_value(lines[j], "bl_uom", &val);
		dic["MEINS"] = convert_to_cstring(val);
		a_list->Add(dic);
		get_bom(a_list, lines[j], werks, stlan);
	}
}

int bs_sap_bom(EPM_action_message_t msg)
{
	printf("start sap bom\n");
	int ifail = ITK_ok, att_count;
	tag_t rootTask, *attachments, relation_type, win;
	char *obj_type;
	int pref_cnt;
	char **pref_vals;
	EPM_ask_root_task(msg.task, &rootTask);

	EPM_ask_attachments(rootTask, EPM_target_attachment, &att_count, &attachments);
	TC_argument_list_t * arguments = msg.arguments;
	int arg_cnt = TC_number_of_arguments(arguments), status = 0;
	POM_AM__set_application_bypass(true);
	map<string, string> paras;

	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;
	}
	PREF_ask_char_values("connor_sap", &pref_cnt, &pref_vals);
	GRM_find_relation_type("IMAN_master_form", &relation_type);
	Testclass ^test = gcnew Testclass();
	String^ url = convert_to_cstring(pref_vals[1]);
	String^ user = convert_to_cstring(pref_vals[2]);
	String^ password = convert_to_cstring(pref_vals[3]);

	BOM_create_window(&win);
	for (int i = 0; i < att_count; i++)
	{
		if (isTypeOf(attachments[i], "PSBOMViewRevision"))
		{
			printf("execute\n");
			int rel_cnt, line_cnt;
			System::Collections::Generic::List<System::Collections::Generic::Dictionary<String^, String^>^> ^a_list = gcnew System::Collections::Generic::List<System::Collections::Generic::Dictionary<String^, String^>^>();
			tag_t top_line, *lines, rev, *rels;
			char *id, *werks, *stlan;
			BOM_set_window_top_line_bvr(win, attachments[i], &top_line);
			AOM_ask_value_tag(top_line, "bl_line_object", &rev);
			GRM_list_secondary_objects_only(rev, relation_type, &rel_cnt, &rels);
			AOM_ask_value_string(rev, "item_id", &id);
			AOM_ask_value_string(rels[0], "jd2_gc", &werks);
			AOM_ask_value_string(rels[0], "jd2_BOMyt", &stlan);
			if (tc_strlen(werks)==0||tc_strlen(stlan)==0)
				continue;
			
			get_bom(a_list, top_line, werks, stlan);
			string result = ConvertToString(test->send_bom(url, user, password, a_list));
			printf("result:%s\n", result.c_str());

		}
	}
	BOM_close_window(win);
	POM_AM__set_application_bypass(false);

	return 0;
}

