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

import com.google.gson.annotations.Expose;
import com.persagy.adm.diagram.core.model.base.Container;
import com.persagy.adm.diagram.core.model.base.XY;
import com.persagy.adm.diagram.core.model.template.DiagramTemplate;

import java.util.*;

/**
 * 系统图
 * @author zhaoyk
 */
public class Diagram {

	@Expose(serialize = false)
	private String id;

	/**
	 * 系统图名称
	 */
	@Expose(serialize = false)
	private String name;

	/**
	 * 描述信息
	 */
	@Expose(serialize = false)
	private String remark;

	/**
	 * 系统图类型编码
	 */
	@Expose(serialize = false)
	private String type;

	/**
	 * 引用的模板id
	 */
	@Expose(serialize = false)
	private String templateId;

	/**
	 * 所属的系统类型
	 */
	@Expose(serialize = false)
	private String system;

	/**
	 * 项目id
	 */
	@Expose(serialize = false)
	private String projectId;

	/**
	 * 对应的系统实例id
	 */
	@Expose(serialize = false)
	private String systemId;

	/**
	 * 集团编码
	 */
	@Expose(serialize = false)
	private String groupCode;

	/**
	 * 其他字段信息
	 */
	@Expose(serialize = false)
	private Map<String, Object> extraProps;

	/**
	 * 节点列表
	 */
	@Expose
	private List<DiagramNode> nodes = new ArrayList<>();

	/**
	 * 连线列表
	 */
	@Expose
	private List lines = new ArrayList();

	//运行时
	private DiagramTemplate template;


	public void init(){
		if (template != null)
			template.init();

		nodes.sort(new Comparator<DiagramNode>() {
			@Override
			public int compare(DiagramNode o1, DiagramNode o2) {
				return o1.getLayoutIndex() - o2.getLayoutIndex();
			}
		});
		for (DiagramNode node : nodes) {
			Container con = template.getContainerById(node.getContainerId());
			if (con != null)
				con.addComp(node);
		}
	}

	public void layout(XY location){
		this.layout(location, false);
	}

	public void layout(XY location, boolean absoluteLocation){
		if(template != null) {
			//TODO
			template.layout(location);

			if(absoluteLocation) {
				for (DiagramNode node : nodes) {
					node.setLocation(node.locationToRoot());
					if(node instanceof EquipmentNode) {
						EquipmentNode en = (EquipmentNode) node;
						if(en.getAnchorLocations() != null) {
							for(XY point : en.getAnchorLocations().values()){
								point.x += en.getLocation().x;
								point.y += en.getLocation().y;
							}
						}
						if(en.getLabel() != null) {
							XY point = en.getLabel().getLocation();
							point.x += en.getLocation().x;
							point.y += en.getLocation().y;
						}
					}
				}
			}
		}
	}

	public List<String> getObjIds(){
		List<String> ids = new ArrayList<>();
		for(DiagramNode node : nodes) {
			if(EquipmentNode.TYPE.equals(node.getCompType()))
				ids.add(((EquipmentNode) node).getObjId());
		}
		return ids;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getTemplateId() {
		return templateId;
	}

	public void setTemplateId(String templateId) {
		this.templateId = templateId;
	}

	public List<DiagramNode> getNodes() {
		return nodes;
	}

	public void setNodes(List<DiagramNode> nodes) {
		this.nodes = nodes;
	}

	public List getLines() {
		return lines;
	}

	public void setLines(List lines) {
		this.lines = lines;
	}

	public String getSystem() {
		return system;
	}

	public void setSystem(String system) {
		this.system = system;
	}

	public String getProjectId() {
		return projectId;
	}

	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}

	public String getSystemId() {
		return systemId;
	}

	public void setSystemId(String systemId) {
		this.systemId = systemId;
	}

	public String getGroupCode() {
		return groupCode;
	}

	public void setGroupCode(String groupCode) {
		this.groupCode = groupCode;
	}

	public Object getExtraProp(String propName) {
		return extraProps != null ? extraProps.get(propName) : null;
	}

	public void setExtraProp(String propName, Object propValue) {
		if(extraProps == null)
			extraProps = new HashMap<>();

		if(propValue != null)
			extraProps.put(propName, propValue);
		else
			extraProps.remove(propName);
	}

	public DiagramTemplate getTemplate() {
		return template;
	}

	public void setTemplate(DiagramTemplate template) {
		this.template = template;
	}

}
