package com.cim.idm.utils;

import org.jdom.Document;
import org.jdom.Element;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.*;

public class DocumentUtils {
	

	public static Document jsonToTibcoDocument(String methodName, JSONObject jsonObject) {
		Element message = new Element("MESSAGE");
		Document document = new Document(message);
		Element header = message.addContent("HEADER");
		Element body = message.addContent("BODY");
		
		Element messagename = header.addContent("MESSAGENAME");
		messagename.setText(methodName);
		
		jsonToElement(body,jsonObject);
		return document;
	}
	
	public static void jsonToElement(Element body, JSONObject jsonObject) {
		Iterator<String> iterator = jsonObject.keySet().iterator();
		while(iterator.hasNext()) {
			String key = iterator.next();
			Object jsonChild = jsonObject.get(key);
			if(jsonChild.getClass().equals(JSONObject.class)) {
				Element child = new Element(key.toUpperCase());
				body.addContent(child);
				jsonToElement(child,(JSONObject)jsonChild);
			}else if(JSONArray.class.equals(jsonChild.getClass())) {
				Element child = new Element(key.toUpperCase());
				body.addContent(child);
				for(Object o : (JSONArray)jsonChild) {
					if(o instanceof JSONObject) {
						Element data = new Element("DATA");
						child.addContent(data);
						jsonToElement(data,(JSONObject)o);
					}else {
						Element child2 = new Element((key+"child").toUpperCase());
						child2.setText(o.toString());
						child.addContent(child2);
					}
				}
				
			}else {
				Element child = new Element(key.toUpperCase());
				body.addContent(child);
				child.setText(jsonChild.toString());
			}
		}
	}

	public static void jsonToElementV2(Element parentElement, JSONObject jsonObject) {
		for (String key : jsonObject.keySet()) {
			Object value = jsonObject.get(key);

			if (value instanceof JSONObject) {
				Element child = new Element(key.toUpperCase());
				parentElement.addContent(child);
				jsonToElementV2(child, (JSONObject) value);
			} else if (value instanceof JSONArray) {
				JSONArray jsonArray = (JSONArray) value;
				for (int i = 0; i < jsonArray.length(); i++) {
					Element child = new Element(key.toUpperCase());
					parentElement.addContent(child);
					JSONObject arrayObject = jsonArray.getJSONObject(i);
					jsonToElementV2(child, arrayObject);
				}
			} else {
				Element child = new Element(key.toUpperCase());
				parentElement.addContent(child);
				child.setText(value.toString());
			}
		}
	}

	
	/**B19将sap下发消息转成我们这边的大写（在用）
	 * 20231026
	 * @param body
	 * @param jsonObject
	 */
	public static void jsonToElement2(Element body, com.alibaba.fastjson.JSONObject jsonObject) {
		Iterator<String> iterator = jsonObject.keySet().iterator();
		while(iterator.hasNext()) {
			String key = iterator.next();
			Object jsonChild = jsonObject.get(key);
			if(jsonChild.getClass().equals(com.alibaba.fastjson.JSONObject.class)) {
				if ("detail".equals(key)) {
					Element child = new Element(key.toUpperCase());
					Element data = new Element("DATA");
					child.addContent(data);
					body.addContent(child);
					jsonToElement2(data,(com.alibaba.fastjson.JSONObject)jsonChild);
				} else {
					Element child = new Element(key.toUpperCase());
					body.addContent(child);
					jsonToElement2(child,(com.alibaba.fastjson.JSONObject)jsonChild);
				}
				//		        Element child = new Element(key.toUpperCase());
				//		        body.addContent(child);
				//		        jsonToElement2(child,(com.alibaba.fastjson.JSONObject)jsonChild);
			}else if(com.alibaba.fastjson.JSONArray.class.equals(jsonChild.getClass())) {
				Element child = new Element(key.toUpperCase());
				body.addContent(child);
				for(Object o : (com.alibaba.fastjson.JSONArray)jsonChild) {
					if(o instanceof com.alibaba.fastjson.JSONObject && !"detail".equals(key)) {
						Element data = new Element("DATAS");
						child.addContent(data);
						jsonToElement2(data,(com.alibaba.fastjson.JSONObject)o);
					} else if (o instanceof com.alibaba.fastjson.JSONObject && "detail".equals(key)) {
						Element data = new Element("DATA");
						child.addContent(data);
						jsonToElement2(data,(com.alibaba.fastjson.JSONObject)o);
					}else {
						Element child2 = new Element((key+"child").toUpperCase());
						child2.setText(o.toString());
						child.addContent(child2);
					}
				}

			}else {
				Element child = new Element(key.toUpperCase());
				body.addContent(child);
				child.setText(jsonChild.toString());
			}
		}
	}

	public static JSONObject CNMDocumentToJson(Document document) {
		Element root = document.getRootElement();
		Element returnnode = root.getChild("Return");
		Element returncode = returnnode.getChild("RETURNCODE");
		Element returnmsg = returnnode.getChild("RETURNMESSAGE");
		Map<String, Object> map = new HashMap<>();		
		if(returncode.getText().equals("0")) {
			Element data = root.getChild("Body");
			//Element data = body.getChild("DATALIST");
			if(data==null||data.getContent().isEmpty()) {				
				map.put("status", Constant.RESPONSE_STATUS_SUCCESS);
				map.put("RETURNCODE","0" );
				map.put("RETURNMSG", "无数据返回");
			}else {
				documentToMap(data, map);
				map.put("RETURNCODE","0" );
				map.put("status", Constant.RESPONSE_STATUS_SUCCESS);
				map.put("RETURNMSG", returnmsg.getText());
			}			
			JSONObject jsonObject = new JSONObject(map);
			return jsonObject;
			
		}else {
			map.put("status", Constant.RESPONSE_STATUS_FAIL);
			map.put("RETURNCODE", returncode.getText());
			map.put("RETURNMSG", returnmsg.getText());
			JSONObject jsonObject = new JSONObject(map);
			return jsonObject;
		}
	}
	
	public static JSONObject tibcoDocumentToJson(Document document) {
		Element root = document.getRootElement();
		
		Element returnnode = root.getChild("Return");
		Element returncode = returnnode.getChild("RETURNCODE");
		Element returnmsg = returnnode.getChild("RETURNMESSAGE");
		if(returncode.getText().equals("0")) {
			Element body = root.getChild("Body");
			Element data = body.getChild("DATALIST");
			
			Map<String, Object> map = new HashMap<>();			
			if(data.getContent().isEmpty()) {				
				map.put("status", Constant.RESPONSE_STATUS_FAIL);
				map.put("RETURNCODE","0" );
				map.put("DATA", "404");
				map.put("RETURNMSG", "未找到数据");
			}else {
				documentToMap(data, map);
				map.put("TOTAL", body.getChildText("TOTAL"));
				map.put("RETURNCODE","0" );
				map.put("status", Constant.RESPONSE_STATUS_SUCCESS);
			}
			
			JSONObject jsonObject = new JSONObject(map);
			return jsonObject;
		}else {
			JSONObject jsonObject = new JSONObject();
			jsonObject.append("RETURNCODE", returncode.getText());
			jsonObject.append("RETURNMSG", returnmsg.getText());
			return jsonObject;
		}
		
		
	}
	
	
	public static void documentToMap(Element ret, Map<String, Object> map) {
		List<Element> elementList = ret.getChildren();
		for(Element child : elementList) {
			if(child.getChildren().size() == 0) {
				if(map.containsKey(child.getName())) {
					Object o = map.get(child.getName());
					if(o instanceof List) {
						List<String> list = (List<String>)o;
						list.add(child.getText());
					}else {
						List<String> list = new LinkedList<>();
						list.add(o.toString());
						list.add(child.getText());
						map.put(child.getName(), list);
					}
				}else {
//					List<String> list = new LinkedList<>();
//					list.add(child.getText());
					map.put(child.getName(), child.getText());
				}
			}else {
				if(map.containsKey(child.getName())) {
					List<Map<String, Object>> list = (List<Map<String,Object>>)map.get(child.getName());
					Map<String, Object> childMap = new HashMap<>();
					documentToMap(child, childMap);
					list.add(childMap);
				}else {
					List<Map<String, Object>> list = new LinkedList<>();
					Map<String, Object> childMap = new HashMap<>();
					documentToMap(child, childMap);
					list.add(childMap);
					map.put(child.getName(), list);
				}
			}
		}
	}
	
	public static void jsonToElementMDC(Element body, JSONObject jsonObject) {
		Iterator<String> iterator = jsonObject.keySet().iterator();
		while(iterator.hasNext()) {
			String key = iterator.next();
			Object jsonChild = jsonObject.get(key);
			if(jsonChild.getClass().equals(JSONObject.class)) {
				Element child = new Element(key.toUpperCase());
				body.addContent(child);
				jsonToElement(child,(JSONObject)jsonChild);
			}else if(JSONArray.class.equals(jsonChild.getClass())) {
				Element child = new Element(key.toUpperCase());
				body.addContent(child);
				for(Object o : (JSONArray)jsonChild) {
					if(o instanceof JSONObject) {
						Element data = new Element("MATERIALPACKING");
						child.addContent(data);
						jsonToElement(data,(JSONObject)o);
					}else {
						Element child2 = new Element((key+"child").toUpperCase());
						child2.setText(o.toString());
						child.addContent(child2);
					}
				}
				
			}else {
				Element child = new Element(key.toUpperCase());
				body.addContent(child);
				child.setText(jsonChild.toString());
			}
		}
	}
	public static void jsonToElementPMS(Element body, JSONObject jsonObject) {
		Iterator<String> iterator = jsonObject.keySet().iterator();
		while(iterator.hasNext()) {
			String key = iterator.next();
			Object jsonChild = jsonObject.get(key);
			if(jsonChild.getClass().equals(JSONObject.class)) {
				Element child = new Element(key.toUpperCase());
				body.addContent(child);
				jsonToElement(child,(JSONObject)jsonChild);
			}else if(JSONArray.class.equals(jsonChild.getClass())) {
				Element child = new Element(key.toUpperCase());
				body.addContent(child);
				for(Object o : (JSONArray)jsonChild) {
					if(o instanceof JSONObject) {
						Element data = new Element("MATERIALSHIPREQUESTDETAILFORSI");
						child.addContent(data);
						jsonToElement(data,(JSONObject)o);
					}else {
						Element child2 = new Element((key+"child").toUpperCase());
						child2.setText(o.toString());
						child.addContent(child2);
					}
				}
				
			}else {
				Element child = new Element(key.toUpperCase());
				body.addContent(child);
				child.setText(jsonChild.toString());
			}
		}
	}
}
