package com.persagy.adm.diagram.manage;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 树形结构对象
 * @author zhaoyk
 */
@Data
public class Folder {

	public static final String TYPE_MAJ = "major";

	public static final String TYPE_SYS = "system";

	public static final String TYPE_TYPE = "type";

	private final String objType = "folder";

	private String type;

	private String id;

	private String name;

	private List<Object> children = new ArrayList<>();

	private List elements;

	public static Folder getFolder(String code, String type, List<Folder> folders, List<ObjectNode> dictInfoList){
		Folder target = null;
		for(Folder f: folders){
			if(f.getId().equals(code)) {
				target = f;
				break;
			}
		}
		if (target == null) {
			target = new Folder();
			target.setId(code);
			target.setType(type);

			if(Folder.TYPE_MAJ.equals(type)) {
				target.setName(getFolderName(code, dictInfoList));
			} else if(Folder.TYPE_SYS.equals(type)) {
				target.setName(getFolderName(code, dictInfoList));
			}

			folders.add(target);
		}
		return target;
	}

	private static String getFolderName(String code, List<ObjectNode> dictInfoList){
		for(ObjectNode node : dictInfoList) {
			if(node.get("code").asText().equals(code)) {
				JsonNode nameNode = node.get("name");
				if(nameNode == null) {
					nameNode = node.get("label");
				}
				return nameNode.asText();
			}
		}
		return code;
	}

}
