package net.wicp.tams.app.duckula.controller.bean.constant;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import net.wicp.tams.common.constant.dic.intf.IEnumCombobox;

/***
 * 组织单位类型
 * 
 * @author andy
 * 
 */
public enum ResAuthType implements IEnumCombobox {
	OPERATE("操作资源", "icon-position", null, false, null),

	DATA("数据资源", "icon-group", null, false, null),

	MODULE("模块", "icon-dept", OPERATE, false, null),

	DIR("目录", "icon-org", OPERATE, true, new ResAuthType[] { MODULE }),

	URL("地址", "icon-dept", OPERATE, true, new ResAuthType[] { MODULE, DIR });

	private final String desc;
	private final String icon;

	private final ResAuthType parent;// 资源的父类型

	private final boolean isEdit;// 所定义的类型能否被修改，true，可以被修改，false:不能被修改

	private final ResAuthType[] parentTree;// 资源可以放在哪个类型的资源下面，如URL只能放在DIR目录下。

	private ResAuthType(String desc, String icon, ResAuthType parent, boolean isEdit, ResAuthType[] parentTree) {
		this.desc = desc;
		this.parent = parent;
		this.icon = icon;
		this.isEdit = isEdit;
		this.parentTree = parentTree;
	}

	/***
	 * 当前类型的资源能否被拖拽到另一资源类型下面
	 * 
	 * @param parent 要拖拽的目标资源类型
	 * @return
	 */
	public boolean canDrag(ResAuthType parent) {
		if (ArrayUtils.isEmpty(parentTree)) {
			return false;
		}
		return ArrayUtils.contains(this.parentTree, parent);
	}

	/****
	 * 通过名字返回类型，默认返回 ORG
	 * 
	 * @param name
	 * @return
	 */
	public static ResAuthType findByName(String name) {
		if (StringUtils.isEmpty(name)) {
			return URL;
		}
		for (ResAuthType ele : ResAuthType.values()) {
			if (ele.name().equals(name)) {
				return ele;
			}
		}
		return URL;
	}

	public String getDesc() {
		return desc;
	}

	public String getIcon() {
		return icon;
	}

	@Override
	public String getName() {
		return this.name();
	}

	@Override
	public String getDesc_en() {
		return this.desc;
	}

	@Override
	public String getDesc_zh() {
		return this.desc;
	}

	public ResAuthType getParent() {
		return parent;
	}

	public boolean isEdit() {
		return isEdit;
	}

}
