package com.persagy.adm.diagram.core.build;

import cn.hutool.core.collection.CollUtil;
import com.persagy.adm.diagram.core.model.base.IEquipHolder;

import java.util.*;

/**
 * 计算时的数据组
 * @author zhaoyk
 */
public class CalcGroup {

	private CalcGroup parent;

	private int currentIdx;

	private Map<String, Object> currentComps = new HashMap<>();

	private IEquipHolder dynSrcComp;

	public CalcGroup() {

	}

	public void setDynSrcComp(IEquipHolder dynSrcComp, Object bindData) {
		this.dynSrcComp = dynSrcComp;
		this.dynSrcComp.setMatchedData(CollUtil.newArrayList(bindData));
	}

	public IEquipHolder getDynSrcComp() {
		return dynSrcComp;
	}

	public void setCurrentIdx(int currentIdx) {
		this.currentIdx = currentIdx;
	}

	public void setParent(CalcGroup parent) {
		this.parent = parent;
	}

	public CalcGroup getParent() {
		return parent;
	}

	public Object getComp(String id) {
		CalcGroup g = this;
		while (g != null){
			Object comp = g.currentComps.get(id);
			if(comp != null) {
				return comp;
			}
			g = g.parent;
		}
		return null;
	}

	public void addComp(String id, Object comp) {
		currentComps.put(id, comp);
	}

	public List<Integer> getIdx(){
		LinkedList<Integer> list = new LinkedList<>();
		CalcGroup g = this;
		while (g != null){
			list.addFirst(g.currentIdx);
			g = g.parent;
		}
		return list;
	}

}
