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

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

import com.teamcenter.rac.cme.time.common.ActivityUtils;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentCfgActivityLine;
import com.teamcenter.rac.kernel.TCComponentMEActivity;
import com.teamcenter.rac.kernel.TCComponentMEActivityType;
import com.teamcenter.rac.kernel.TCComponentMECfgLine;

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

public class KTableRowMEActivity extends KTableRow {
	
	public TCComponentCfgActivityLine actline;
	private TCComponentMEActivity folder;

	public KTableRowMEActivity(TCComponentCfgActivityLine bomline,TCComponentMEActivity folder) {
		super(null,null,null,null,null);
		this.actline = bomline;
		this.folder = folder;
	}

	@Override
	public TCComponent getTCComponent(String proploc) {
		if(KUtil.isEmpty(proploc)) {
			return folder;
		}
		proploc = proploc.toUpperCase();
		switch (proploc) {
		case BOMLINE:
			return actline;
		case ITEM:
			return folder;
		default:
			System.out.println(">> Prop location error: " + proploc);
			break;
		}
		return null;
	}
	
	private boolean loadTCValues(Map<String, String> bomlinePropMap , Map<String, String> itemPropMap) {
		boolean modified = false;
		for (PropConfig config : stringProps.keySet()) {
			if(!config.canEdit) {
				continue;
			}
			String value = stringProps.get(config).getValueSafe();
			String propName = config.propName;
			String proploc = config.propLoc.toUpperCase();
			if(savedValues.containsKey(config)) {
				String savedValue = savedValues.get(config);
				if(value.equals(savedValue)) {
					continue;
				}
			}
			modified = true;
			System.out.println(">> "+proploc+"."+propName+" = "+value);
			switch(proploc) {
			case BOMLINE:
				bomlinePropMap.put(propName, value);
				break;
			case ITEM:
				itemPropMap.put(propName, value);
				break;
			default:
				System.out.println(">> Prop location error: " + proploc);
				break;
			}
		}
		return modified;
	}

	public void saveTCData(TCComponentCfgActivityLine parentLine, TCComponentMEActivityType itemType) throws Exception{
		if(actline==null) {
			if(itemType!=null&&parentLine!=null) {
				System.out.println("Create New Activity...");
				folder= itemType.createActivity("-", itemType.getName(), "", 0.0D);
				System.out.println(">> New Activity: "+folder);
				this.actline = (TCComponentCfgActivityLine) ActivityUtils.addActivity(parentLine, folder);
				System.out.println(">> New Line: "+this.actline);
			}
		}
		if(actline==null) {
			return;
		}
		System.out.println("Update: "+actline);
		Map<String, String> itemPropMap = new HashMap<>();
		Map<String, String> bomlinePropMap = new HashMap<>();
		if(!loadTCValues(bomlinePropMap, itemPropMap)) {
			System.out.println(">> No modified property");
			return;
		}
		if(itemPropMap.size()>0) {
			folder.setProperties(itemPropMap);
		}
		if(bomlinePropMap.size()>0) {
			actline.setProperties(bomlinePropMap);
		}
		actline.refresh();
	}
	
	@Override
	public void removeLine() throws Exception{
		if(actline!=null) {
			System.out.println(">> Remove Activity: "+actline);
			ActivityUtils.removeActivity((TCComponentMECfgLine)actline);
		}
	}
	
}
