#include "util.h"

void judge_tag(tag_t tag,int index,vector<string> &vec,stringstream &name)
{
	if(index>=vec.size()-1)
	{
		char *val,*type;
		AOM_ask_value_string(tag, "object_type", &type);
		printf("vec type:%s last type:%s\n",vec[index].c_str(), type);
		if (vec[index] != type)
			return;
		int rel_cnt;
		tag_t *rels;
		AOM_ask_value_tags(tag, "release_status_list", &rel_cnt, &rels);
		if (rel_cnt == 0)
		{
			AOM_ask_value_string(tag, "object_string", &val);
			name << val << ",";
		}
		return;
	}
	if(vec[index]=="Latest")
	{
		tag_t rev;
		ITEM_ask_latest_rev(tag, &rev);
		if(rev)
			judge_tag(rev, index + 1, vec, name);
		return;
	}
	int tag_cnt;
	tag_t *tags;
	AOM_ask_value_tags(tag, vec[index].c_str(), &tag_cnt, &tags);
	for(auto i=0;i<tag_cnt;i++)
	{
		judge_tag(tags[i], index + 1, vec, name);
	}

}


int bs_test_release_check(EPM_action_message_t msg)
{
	int ifail = ITK_ok, att_count, tag_cnt, rel_cnt;
	tag_t rootTask, *attachments, win, top, *tags, tag,*rels;
	char *val;
	printf("begin bs_test_release_check\n");
	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;
	
	stringstream name;
	// map<string, string> paras;
	map<string, vector<string>> judge_map;
	POM_AM__set_application_bypass(true);
	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> temp;
		split(temp_val, ".", &temp);
		if(temp.size())
			judge_map[temp[0]] = temp;
	}
	
	
	for (int i = 0; i<att_count; i++)
	{
		bool pass = true;
		char *type;
		AOM_ask_value_string(attachments[i], "object_type", &type);
		printf("type:%s\n", type);
		if(judge_map.find(type)!=judge_map.end())
		{
			judge_tag(attachments[i], 1, judge_map[type], name);
		}
	}
	if (name.str().length()) {
		printf("has error\n");
	
		ifail = 1;
		name <<"未发布，请先审核发布！";
		EMH_store_error_s1(EMH_severity_warning, EMH_PROP_error_base, name.str().c_str());
	}
	POM_AM__set_application_bypass(false);

	printf("end bs_test_release_check123\n");

	return ifail;
}

