#include "util.h"

boolean judge(tag_t top,string &status,string &name)
{
	int line_cnt;
	tag_t *lines;
	char *val;
	BOM_line_ask_child_lines(top, &line_cnt, &lines);
	for (int j = 0; j < line_cnt; j++)
	{
		AOM_UIF_ask_value(lines[j], "jd2_last_release_status", &val);
		printf("status:%s\n", val);
		string temp = val;
		if (temp.length()>0&&status.find(val) != -1)
		{
			AOM_ask_value_string(lines[j], "bl_rev_object_string", &val);
			name = val;
			return false;
		}
		if (!judge(lines[j], status, name))
			return false;
	}
	return true;
}

int bs_bom_check(EPM_rule_message_t msg)
{
	int ifail = EPM_go, att_count, line_cnt;
	tag_t rootTask, *attachments, win, top, *lines;
	char *val;
	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;
	vector<string> types;
	string name;
	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;
		
	}
	split(paras["type"].c_str(),",", &types);
	printf("check status:%s\n", paras["status"].c_str());
	BOM_create_window(&win);
	for (int i = 0; i < att_count; i++)
	{
		bool pass = false;
		for (int j = 0; j < types.size(); j++)
		{
			if (isTypeOf(attachments[i], types[j].c_str()))
			{
				pass = true;
				break;
			}
		}
		if (!pass)
			continue;
		printf("begin check \n");
		BOM_set_window_top_line(win, NULLTAG, attachments[i], NULLTAG, &top);
		if(!judge(top, paras["status"],name))
		{
			ifail = EPM_nogo;
			break;
		}
		printf("end check \n");

	}
	BOM_close_window(win);
	if (ifail==EPM_nogo) {
		name += paras["prompt"];
		EMH_store_error_s1(EMH_severity_user_error, EMH_PROP_error_base, name.c_str());
	}
	return ifail;
}

