package cn.net.connor.std.processedit.util;

import java.text.DecimalFormat;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;

import com.teamcenter.rac.kernel.TCComponentBOMLine;
import com.teamcenter.rac.kernel.TCComponentBOMWindow;
import com.teamcenter.rac.kernel.TCComponentBOPWindowType;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCSession;

import cn.net.connor.std.aifrcp.util.KUtil;

public class ProcessUtil {

	public static final String PREF_DB_CONN = "Connor_DBConn_Oracle_TC";
	public static DecimalFormat DF = new DecimalFormat("#.##########################################");
	public static final String PREF_MEOP_ITEMTYPE = "Connor_AVIC_MEOP_Types";
	public static final String PREF_MEPROCESS_ITEMTYPE = "Connor_AVIC_MEProcess_Types";

	public static boolean isMEOP(TCSession session, String itemTypeName) throws Exception {
		if (KUtil.isEmpty(itemTypeName)) {
			return false;
		}
		String[] types = KUtil.getPrefVals(session, PREF_MEOP_ITEMTYPE);
		return KUtil.inArray(itemTypeName, types) >= 0;
	}

	public static boolean isMEProcess(TCSession session, String itemTypeName) throws Exception {
		if (KUtil.isEmpty(itemTypeName)) {
			return false;
		}
		String[] types = KUtil.getPrefVals(session, PREF_MEPROCESS_ITEMTYPE);
		return KUtil.inArray(itemTypeName, types) >= 0;
	}

	public static void errorSWT(Shell shell, String message, String title) {
		showDialog(shell, message, title, SWT.ICON_ERROR);
	}

	public static void showDialog(Shell shell, String message, String title, int type) {
		org.eclipse.swt.widgets.MessageBox dialog = new org.eclipse.swt.widgets.MessageBox(shell, SWT.OK | type);
		dialog.setText(title);
		dialog.setMessage(message);
		dialog.open();
	}

	@SuppressWarnings("deprecation")
	public static void switchToPrecise(TCSession session, TCComponentItemRevision rev) throws Exception {
		// 切换到精确bom
		System.out.println("Switch to Precise: " + rev);
		TCComponentBOPWindowType bopWindowType = (TCComponentBOPWindowType) session.getTypeComponent("BOPWindow");
		TCComponentBOMWindow bopWindow = bopWindowType.create(null);
		TCComponentBOMLine bopLine = bopWindow.setWindowTopLine(rev.getItem(), rev, null, null);
		bopWindow.lock();
		try {
			bopLine.setPrecision(true);
			bopWindow.save();
		} finally {
			bopWindow.unlock();
			bopWindow.close();
		}
	}

}
