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

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.teamcenter.rac.aif.AbstractAIFApplication;
import com.teamcenter.rac.aif.kernel.AIFComponentContext;
import com.teamcenter.rac.kernel.TCComponentBOMLine;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.util.Registry;

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

public class MeopNoDialogController extends KDialogController {

	public static final String PREF_MEOP_TYPE = "Connor_AVIC_SQGXH_GXLX";
	public static final String PREF_MEOP_TABLE = "Connor_AVIC_SQGXH_SXBJ";
	private String[] meopTypes;
	protected String[] tableHeaders;
	protected int[] tableHeaderWidth;
	private String[] tableProps;
	private MeopNoDialog dialog;
	private TCComponentBOMLine procLine;
	private int newRowNum = -1;
	private static final Registry REG = Registry.getRegistry(MeopNoDialogController.class);

	public MeopNoDialogController(AbstractAIFApplication app, TCComponentBOMLine procLine) {
		super();
		this.procLine = procLine;
	}

	@Override
	public boolean init() throws Exception {
		this.dialog = (MeopNoDialog) aifDialog;
		this.meopTypes = KUtil.getPrefVals(session, PREF_MEOP_TYPE);
		String[] tableConfigs = KUtil.getPrefVals(session, PREF_MEOP_TABLE);
		if (KUtil.getLen(meopTypes) == 0) {
			throw new TCException(String.format(REG.getString("prefnotconfiged.ERROR1"), PREF_MEOP_TYPE));
		}
		if (KUtil.getLen(tableConfigs) == 0) {
			throw new TCException(String.format(REG.getString("prefnotconfiged.ERROR1"), PREF_MEOP_TABLE));
		}
		int colCnt = KUtil.getLen(tableConfigs);
		tableHeaders = new String[colCnt];
		tableHeaderWidth = new int[colCnt];
		tableProps = new String[colCnt];
		for (int i = 0; i < colCnt; i++) {
			String config = tableConfigs[i];
			int ind = config.lastIndexOf('=');
			if (ind > 0) {
				tableHeaders[i] = config.substring(0, ind);
				tableProps[i] = config.substring(ind + 1);
			} else {
				tableHeaders[i] = config;
				tableProps[i] = config;
			}
			int width = 0;
			while (tableHeaders[i].length() > 0 && Character.isDigit(tableHeaders[i].charAt(0))) {
				width = width * 10 + (tableHeaders[i].charAt(0) - '0');
				tableHeaders[i] = tableHeaders[i].substring(1);
			}
			if (width < 1) {
				width = 120;
			}
			tableHeaderWidth[i] = width;
		}
		return true;
	}

	protected void readMeop() throws Exception {
		System.out.println("Load Meop: " + this.procLine+" | "+Arrays.toString(meopTypes));
		if (this.procLine == null) {
			return;
		}
		this.procLine.refresh();
		AIFComponentContext[] cc = this.procLine.getChildren();
		int len = KUtil.getLen(cc);
		for (int i = 0; i < len; i++) {
			TCComponentBOMLine cLine = (TCComponentBOMLine) cc[i].getComponent();
			String cType = cLine.getItem().getType();
			System.out.println((i+1)+". "+cLine+" -> "+cType);
			if(KUtil.inArray(cType, meopTypes)>=0) {
				dialog.tableModel.addRow(new MeopNoRow(cLine).getRowData(tableProps));
			}
		}
	}

	public void insert() throws Exception {
		int selectedRow = dialog.table.getSelectedRow();
		System.out.println("Insert to: "+selectedRow);
		if(selectedRow<0) {
			newRowNum = dialog.table.getRowCount();
			dialog.tableModel.addRow(new Object[] {});
		}else {
			newRowNum = selectedRow;
			dialog.tableModel.insertRow(newRowNum, new Object[] {});
		}
		System.out.println("New RowNum: "+this.newRowNum);
	}

	public Map<String, String> getInsertedData(){
		System.out.println("New RowNum: "+this.newRowNum);
		if(this.newRowNum<0) {
			return null;
		}
		Map<String, String> res = new HashMap<String, String>();
		int colCnt = KUtil.getLen(tableProps);
		for(int i=0; i<colCnt; i++) {
			String value = (String) dialog.table.getValueAt(this.newRowNum, i);
			if(value==null) {
				value = "";
			}
			res.put(tableProps[i], value);
		}
		return res;
	}

}
