package com.kingdee.shr.customer.gtiit.helper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.google.common.collect.Maps;
import com.kingdee.bos.Context;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.web.dynamic.model.FieldInfo;
import com.kingdee.shr.base.syssetting.web.dynamic.model.UIViewInfo;
import com.kingdee.shr.base.syssetting.web.dynamic.util.UIViewUtil;

public class CmpDisplayViewAnalyzing {
	public static List<FieldInfo> getAdjSalaryApplyListFields(Context ctx, String display) throws SHRWebException {
		UIViewInfo uiViewInfo = UIViewUtil.getUIViewInfoByUIPK(ctx, display);
		if (uiViewInfo == null)
			return null;
		String content = uiViewInfo.getContent();
		List<FieldInfo> fields = null;
		try {
			fields = parseFieldByUIViewContent(content);
		} catch (DocumentException e) {
			throw new SHRWebException(e);
		}
		return fields;
	}

	public static List<FieldInfo> parseFieldByUIViewContent(String content) throws DocumentException {
		List<FieldInfo> fields = new ArrayList<>();
		Document doc = DocumentHelper.parseText(content);
		Element root = doc.getRootElement();
		Element body = root.element("body");
		List<Element> elements = body.elements("field");
		if (elements == null || elements.size() == 0)
			return fields;
		fields = parseFields(elements);
		return fields;
	}

	public static List<FieldInfo> parseFields(List<Element> elements) {
		List<FieldInfo> fields = new ArrayList<>();
		for (Element element : elements) {
			FieldInfo field = new FieldInfo();
			field.setName(element.attributeValue("name"));
			field.setAlias(element.attributeValue("label"));
			field.setType(element.attributeValue("type"));
			Map<String, String> attributes = new HashMap<>();
			attributes.put("required", element.attributeValue("required"));
			attributes.put("frozen", element.attributeValue("frozen"));
			attributes.put("filter", element.attributeValue("filter"));
			field.setAttributes(attributes);
			fields.add(field);
		}
		return fields;
	}

	public static Map<String, Map<String, Boolean>> complementDefaultField(Context ctx, String display) throws SHRWebException {
		List<FieldInfo> listFields = getAdjSalaryApplyListFields(ctx, display);
		if (listFields == null)
			return null;
		Map<String, Map<String, Boolean>> attrMap = new LinkedHashMap<>();
		for (FieldInfo listField : listFields) {
			String type = listField.getType();
			Map<String, String> attributes = listField.getAttributes();
			Map<String, Boolean> valueMap = Maps.newHashMap();
			if (type != null && type.equals("hidden"))
				valueMap.put("hidden", Boolean.TRUE);
			if ("true".equalsIgnoreCase(attributes.get("frozen")))
				valueMap.put("frozen", Boolean.TRUE);
			if ("true".equalsIgnoreCase(attributes.get("required")))
				valueMap.put("required", Boolean.TRUE);
			attrMap.put(listField.getName(), valueMap);
		}
		return attrMap;
	}

}
