package com.persagy.adm.diagram.core.model.logic;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.gson.annotations.Expose;
import com.persagy.adm.diagram.core.model.EquipmentNode;
import com.persagy.adm.diagram.core.model.base.Container;
import com.persagy.adm.diagram.core.model.base.IComponent;
import com.persagy.adm.diagram.core.model.template.MainPipe;
import com.persagy.dmp.digital.entity.ObjectRelation;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 关系过滤器
 * @author zhaoyk
 */
public class RelationFilter extends DataFilter {

	@Expose
	private String relObjType; //关联对象类型

	@Expose
	private String relObjId; //关联对象id

	@Expose
	private List<String> relTypes; //关联关系类型，格式: graphCode/relCode

	/**
	 * 关系列表，运行时
	 */
	@JsonIgnore
	private List<ObjectNode> relations;

	public RelationFilter(){
		this.type = TYPE_RELATION;
	}

	@Override
	protected boolean judge(Object data, CalcContext context) {
		if(relations == null) {
			initRelations(context);
		}

		String paramDataId = getDataId(data);
		if(paramDataId != null) {
			for(ObjectNode rel : relations) {
				if(paramDataId.equals(rel.get(ObjectRelation.OBJ_FROM_HUM).asText()) || paramDataId.equals(rel.get(ObjectRelation.OBJ_TO_HUM).asText())){
					return true;
				}
			}
		}
		return false;
	}

	private void initRelations(CalcContext context) {
		List<String> dataIds = new ArrayList<>();
		Object o = context.getComp(relObjId, relObjType);
		if(o instanceof MainPipe) {
			String dataId = getDataId(((MainPipe) o).getDataObject());
			if(dataId != null) {
				dataIds.add(dataId);
			}
		} else if(o instanceof Container) {
			Container con = (Container) o;
			if(con.isEquipmentBox()) {
				for(IComponent comp : con.getChildren()) {
					if(comp instanceof EquipmentNode) {
						String dataId = getDataId(((EquipmentNode) comp).getDataObject());
						if(dataId != null) {
							dataIds.add(dataId);
						}
					}
				}
			}
		}

		if(dataIds.size() > 0) {
			List<String[]> types;
			if (relTypes != null) {
				types = relTypes.stream().map(type -> type.split("/")).collect(Collectors.toList());
			} else {
				types = new ArrayList<>();
			}
			relations = context.getDataStrategy().loadRelationsByType(types, dataIds, context.getProjectId(), context.getGroupCode());
		}

		if(relations == null){
			relations = new ArrayList<>();
		}
	}

	private String getDataId(Object obj){
		if(obj instanceof ObjectNode){
			return ((ObjectNode) obj).get("id").asText();
		}
		return null;
	}

	public String getRelObjType() {
		return relObjType;
	}

	public void setRelObjType(String relObjType) {
		this.relObjType = relObjType;
	}

	public String getRelObjId() {
		return relObjId;
	}

	public void setRelObjId(String relObjId) {
		this.relObjId = relObjId;
	}

	public List<String> getRelTypes() {
		return relTypes;
	}

	public void setRelTypes(List<String> relTypes) {
		this.relTypes = relTypes;
	}

}
