#include "util.h"

int bs_wl_check(EPM_rule_message_t msg)
{
	int ifail = EPM_go, att_count, tag_cnt,form_cnt;
	tag_t rootTask, *attachments, *tags,*forms;
	char *val;
	stringstream ss,name_ss;

	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> wl_types, judge_types,judge_vec;

	string name;
	map<string, string> paras,judge_map;
	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["wlType"], ",", &wl_types);
	split(paras["judgeType"], ",", &judge_types);
	split(paras["judgeProps"], ",", &judge_vec);
	for(int i=0;i<judge_vec.size();i++)
	{
		vector<string> temp;
		split(judge_vec[i], "=", &temp);
		if(temp.size()==2)
		{
			judge_map[temp[0]] = temp[1];
		}
	}
	
	for (int i = 0; i<att_count; i++)
	{
		bool pass=false;
		for(int j=0;j<wl_types.size();j++)
		{
			if(isTypeOf(attachments[i],wl_types[j].c_str()))
			{
				pass = true;
				printf("wl_type:%s\n", wl_types[j].c_str());
				break;
			}
		}
		if (!pass)
		{
			continue;
		}
		AOM_ask_value_tags(attachments[i], "IMAN_master_form_rev", &form_cnt, &forms);
		pass = false;

		for(auto j=judge_map.begin();j!=judge_map.end();j++)
		{
			AOM_UIF_ask_value(forms[0], j->first.c_str(), &val);
			if(j->second!=val)
			{
				pass = true;
				break;
			}
		}
		if(!pass)
		{
			continue;
		}
		AOM_ask_value_tags(attachments[i], paras["relation"].c_str(), &tag_cnt, &tags);
		printf("relation :%s\n, length:%d\n", paras["relation"].c_str(), tag_cnt);
		pass = false;
		for(int j=0;j<tag_cnt;j++)
		{
			for(int k=0;k<judge_types.size();k++)
			{
				if(isTypeOf(tags[j], judge_types[k].c_str()))
				{
					printf("judge_type:%s\n", judge_types[k].c_str());
					tag_t rev;
					ITEM_ask_latest_rev(tags[j], &rev);
					if(rev)
					{
						int rel_cnt;
						tag_t *rels;
						AOM_ask_value_tags(rev, "release_status_list", &rel_cnt, &rels);
						if(rel_cnt==0)
						{
							AOM_ask_value_string(tags[j], "object_string", &val);
							ss << val << ",";
						}
					}else
					{
						printf("dont have rev\n");
					}
					pass = true;
				}
			}
		}
		if (!pass)
		{
			AOM_ask_value_string(attachments[i], "object_string", &val);
			name_ss << val<<",";
		}
	}
	if(ss.str().length())
	{
		ss << "×îÐÂ°æÎ´·¢²¼\n";
		ifail = EPM_nogo;
	}
	if(name_ss.str().length())
	{
		ss << name_ss.str() << paras["prompt"];
		ifail = EPM_nogo;
	}
	if (ifail==EPM_nogo) {
		EMH_store_error_s1(EMH_severity_user_error, EMH_PROP_error_base, ss.str().c_str());
	}
	return ifail;
}

