#include "epm_handler_common.h"
#include <tc\tc_macros.h>
#include <tc\emh.h>
#include <tc\preferences.h>
#include <property\propdesc.h>
#include <epm\epm.h>
#include <epm\epm_toolkit_tc_utils.h>
#include <tccore\item.h>
#include <tccore\grmtype.h>
#include <tccore\grm.h>
#include <tccore\imantype.h>
#include <sa\am.h>
#include <sa\sa.h>
#include <tccore\aom.h>
#include <tccore\aom_prop.h>
#include <property\prop_errors.h>
#include <tccore\workspaceobject.h>
#include <qry\qry.h>
#include <bom\bom_attr.h>
#include <bom\bom.h>
#include <epm\signoff.h>
#include <pom\pom\pom.h>
#include <pom\pom\pom_errors.h>
#include <fclasses\tc_date.h>
#include <epm\cr.h>
#include <cfm\cfm.h>
#include <sa\am.h>
#include "tc/envelope.h"
#include <stdio.h>
#include "ae/dataset.h"
#include <ctype.h>
#include <string.h>
#include <string>
#include <iostream>
#include <iterator>
#include <map>
#include <vector>
#include "ce/ce.h"
#include <time.h>
#include <windows.h>  
#include <iostream>  
#include <sql.h>  
#include <sqlext.h>  
#include "string"
#include "sstream"
#include <sa/tcfile.h>
#include <tccore/aom.h>
#include <sa/tcfile_cache.h>
#include <form/form.h>
#include "ctime"
#include "tchar.h"
#include <iostream>  
#include <string>  
#include <fstream>

using namespace std;
extern "C" int POM_AM__set_application_bypass(logical bypass);

int is_begin_with(const char * str1, char *str2) {
	if (str1 == NULL || str2 == NULL)
		return -1;
	int len1 = strlen(str1);
	int len2 = strlen(str2);
	if ((len1 < len2) || (len1 == 0 || len2 == 0))
		return -1;
	char *p = str2;
	int i = 0;
	while (*p != '\0') {
		if (*p != str1[i])
			return 0;
		p++;
		i++;
	}
	return 1;
}

bool isTypeOf(tag_t objtag, const char * type_name) {
	tag_t type = NULLTAG;
	TCTYPE_ask_object_type(objtag, &type);
	tag_t item_type = NULLTAG;
	TCTYPE_find_type(type_name, "", &item_type);
	bool is_type = false;
	if (item_type != NULLTAG) {
		logical isok = FALSE;
		TCTYPE_is_type_of(type, item_type, &isok);
		if (isok) {
			is_type = true;
		}
	}
	return is_type;
}

int jd_check_99_bomyt(EPM_rule_message_t msg) {
	POM_AM__set_application_bypass(true);

	int ifail = EPM_nogo, att_cnt;
	tag_t root_task, *attachments;
	EPM_ask_root_task(msg.task, &root_task);
	EPM_ask_attachments(root_task, EPM_target_attachment, &att_cnt, &attachments);

	for (int i = 0;i < att_cnt;i++) {
		char *name;
		AOM_ask_value_string(attachments[i], "object_string", &name);
		printf("name=====>%s\n", name);
		if (is_begin_with(name, "99")) {
			tag_t form = NULLTAG;
			if (isTypeOf(attachments[i], "ItemRevision")) {
				AOM_ask_value_tag(attachments[i], "IMAN_master_form_rev", &form);
			} else if (isTypeOf(attachments[i], "Item")) {
				tag_t *revs;
				int rev_cnt;
				AOM_ask_value_tags(attachments[i], "revision_list", &rev_cnt, &revs);
				AOM_ask_value_tag(revs[rev_cnt - 1], "IMAN_master_form_rev", &form);
			}
			if (form != NULLTAG) {
				char *prop;
				AOM_ask_value_string(form, "jd2_BOMyt", &prop);
				if (tc_strcmp(prop, "5") == 0) {
					ifail = EPM_go;
				}
			}
		} else {
			ifail = EPM_go;
		}
	}

	POM_AM__set_application_bypass(false);
	return ifail;
}