package cn.net.connor.std.processedit.meopviewfx.bean;

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

import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentBOMLine;
import com.teamcenter.rac.kernel.TCComponentItem;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCComponentItemType;

import cn.net.connor.std.aifrcp.util.KUtil;
import javafx.beans.property.SimpleStringProperty;

public class KTableRowBackup20220509 {

	public static final String BOMLINE = "BOMLINE";
	public static final String ITEM = "ITEM";
	public static final String ITEMMASTER = "ITEMMASTER";
	public static final String REV = "REV";
	public static final String REVMASTER = "REVMASTER";

	private TCComponentBOMLine bomline;
	private TCComponentItem item;
	private TCComponentItemRevision rev;
	private TCComponent itemMaster;
	private TCComponent revMaster;
	private boolean showWebView = true;

	public KTableRowBackup20220509(TCComponentBOMLine bomline, TCComponentItem item, TCComponentItemRevision rev, TCComponent itemMaster, TCComponent revMaster) {
		this.bomline = bomline;
		this.item = item;
		this.rev = rev;
		this.itemMaster = itemMaster;
		this.revMaster = revMaster;
	}
	
	public Map<PropConfig, SimpleStringProperty> stringProps = new HashMap<PropConfig, SimpleStringProperty>();
	protected Map<PropConfig, String> savedValues = new HashMap<PropConfig, String>();

	public TCComponentBOMLine getBomLine() {
		return this.bomline;
	}
	
	public void setValueByPropName(String config, String value) throws Exception{
		if(KUtil.isEmpty(config)) {
			return;
		}
		System.out.println("Set Row Value ["+config+"] = ["+value+"]");
		for (PropConfig propConfig : stringProps.keySet()) {
			if(config.equalsIgnoreCase(propConfig.propName)) {
				stringProps.get(propConfig).setValue(value);
			}
		}
		System.out.println(">> config not found");
	}
	
	public boolean parseConfig(PropConfig config) throws Exception {
		if (config == null) {
			return true;
		}
		String value = getTCValue(config.propLoc, config.propName);
		stringProps.put(config, new SimpleStringProperty(value));
		savedValues.put(config, value);
		if (!KUtil.isEmpty(config.filterValue)) {
			// && !config.filterValue.equals(value)
			// 20200722 split filter
			String[] filters = config.filterValue.split(";");
			boolean checkOK = false;
			for (String string : filters) {
				if (string.equals(value)) {
					checkOK = true;
					break;
				}
			}
			return checkOK;
		}
		return true;
	}

	public String getTCValue(String proploc, String propName) throws Exception {
		TCComponent comp = getTCComponent(proploc);
		if (comp == null) {
			return "";
		}
		return comp.getStringProperty(propName);
	}

	public TCComponent getTCComponent(String proploc) {
		if (KUtil.isEmpty(proploc)) {
			return bomline;
		}
		proploc = proploc.toUpperCase();
		switch (proploc) {
		case BOMLINE:
			return bomline;
		case ITEM:
			return item;
		case REV:
			return rev;
		case ITEMMASTER:
			return itemMaster;
		case REVMASTER:
			return revMaster;
		default:
			System.out.println(">> Prop location error: " + proploc);
			break;
		}
		return null;
	}

	private boolean loadTCValues(Map<String, String> bomlinePropMap, Map<String, String> itemPropMap, Map<String, String> revPropMap, Map<String, String> itemMasterPropMap,
			Map<String, String> revMasterPropMap) {
		boolean modified = false;
		for (PropConfig config : stringProps.keySet()) {
			if (!config.canEdit) {
				continue;
			}
			String value = stringProps.get(config).getValueSafe();
			if (savedValues.containsKey(config)) {
				String savedValue = savedValues.get(config);
				if (value.equals(savedValue)) {
					continue;
				}
			}
			modified = true;
			String propName = config.propName;
			String proploc = config.propLoc.toUpperCase();
			System.out.println(">> " + proploc + "." + propName + " = " + value);
			switch (proploc) {
			case BOMLINE:
				bomlinePropMap.put(propName, value);
				break;
			case ITEM:
				itemPropMap.put(propName, value);
				if ("object_name".equals(propName)) {
					revPropMap.put(propName, value);
				}
				break;
			case REV:
				revPropMap.put(propName, value);
				if ("object_name".equals(propName)) {
					itemPropMap.put(propName, value);
				}
				break;
			case ITEMMASTER:
				itemMasterPropMap.put(propName, value);
				break;
			case REVMASTER:
				revMasterPropMap.put(propName, value);
				break;
			default:
				System.out.println(">> Prop location error: " + proploc);
				break;
			}
		}
		return modified;
	}

	public void saveTCData(TCComponentBOMLine parentLine, TCComponentItemType itemType) throws Exception {
		if (bomline == null) {
			if (itemType != null && parentLine != null) {
				System.out.println("Create New Row...");
				item = itemType.create(itemType.getNewID(), itemType.getNewRev(null), itemType.getName(), "-", "", null);
				System.out.println(">> new item: " + item);
				rev = item.getLatestItemRevision();
				itemMaster = item.getRelatedComponent("IMAN_master_form");
				revMaster = rev.getRelatedComponent("IMAN_master_form_rev");
				bomline = parentLine.add(item, rev, null, false, "");
			}
		}
		if (bomline == null) {
			return;
		}
		System.out.println("Update: " + bomline);
		Map<String, String> revPropMap = new HashMap<>();
		Map<String, String> revMasterPropMap = new HashMap<>();
		Map<String, String> itemPropMap = new HashMap<>();
		Map<String, String> itemMasterPropMap = new HashMap<>();
		Map<String, String> bomlinePropMap = new HashMap<>();
		if (!loadTCValues(bomlinePropMap, itemPropMap, revPropMap, itemMasterPropMap, revMasterPropMap)) {
			return;
		}
		if (itemPropMap.size() > 0) {
			item.setProperties(itemPropMap);
		}
		if (revPropMap.size() > 0) {
			rev.setProperties(revPropMap);
		}
		if (itemMasterPropMap.size() > 0) {
			itemMaster.setProperties(itemMasterPropMap);
		}
		if (revMasterPropMap.size() > 0) {
			revMaster.setProperties(revMasterPropMap);
		}
		if (bomlinePropMap.size() > 0) {
			bomline.setProperties(bomlinePropMap);
		}
		bomline.refresh();
	}

	public void removeLine() throws Exception {
		if (bomline != null) {
			System.out.println(">> Remove: " + bomline);
			bomline.cut();
		}
	}

	public void showWebView(boolean showWebView) {
		this.showWebView = showWebView;
	}

	public boolean showWebView() {
		return showWebView;
	}

}
