package com.ym.mec.collectfee.utils;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.javabean.BeanProvider;
import com.thoughtworks.xstream.converters.javabean.JavaBeanConverter;
import com.ym.mec.collectfee.entity.RequestHead;
import org.springframework.beans.factory.annotation.Value;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

public final class XStreamUtil {

	private XStreamUtil() {

	}

	public static RequestHead getOrdersHead(String body,Integer cmd,String publicKey,Integer uid,Integer encrypt){
		RequestHead head = new RequestHead();
		head.setMsgId(getRandom(7));
		head.setCmd(cmd);
		if(uid != null){
			head.setUid(uid);
		}
		if(encrypt != null){
			head.setEncrypt(encrypt);
		}
		head.setSign(XStreamUtil.getSign(head, body, publicKey));
		return head;
	}

	public static String getRandom(int length) {
		String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}

	public static String getSign(RequestHead head,String body,String userSign){
		//msgId+cmd+uid+ts+body+用户密钥/公共密钥
		StringBuffer sb = new StringBuffer(head.getMsgId());
		sb.append(head.getCmd()).append(head.getUid()).append(head.getTs()).append(body).append(userSign);
		return XStreamUtil.encryption(sb.toString()).toLowerCase();
	}

	/**
	 * 将包含xml内容的流转换成对�?
	 * 
	 * @param rootNode
	 *            根节点名�?
	 * @param rootNodeType
	 *            根节点类�?
	 * @param inputStream
	 *            �?
	 * @return 返回转换后的对象
	 */
	@SuppressWarnings("unchecked")
	public static <T> T xmlToObject(String rootNode, Class<T> rootNodeType,
			String body) {
		XStream xStream = new XStream();// 默认用xppDriver
		xStream.autodetectAnnotations(true);
		xStream.alias(rootNode, rootNodeType);// 尽可能的加上，否则会抛CannotResolveClassException异常
		xStream.ignoreUnknownElements();
		// 将xml转换成对�?
		return (T) xStream.fromXML(body);
	}

	/**
	 * 将对象转换成xml
	 * 
	 * @param rootNodeName
	 *            根节点名�?
	 * @param obj
	 *            对象�?
	 * @return
	 */
	public static String objectToXml(String rootNodeName, Object obj) {
		XStream xStream = new XStream();
		xStream.registerConverter(new JavaBeanConverter(xStream.getMapper(),
				new BeanProvider()), XStream.PRIORITY_VERY_LOW);
		xStream.alias(rootNodeName, obj.getClass());
		return xStream.toXML(obj);
	}

	public static String encryption(String body) {
		String re_md5 = new String();
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(body.getBytes());
			byte b[] = md.digest();

			int i;

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}

			re_md5 = buf.toString();

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return re_md5.toUpperCase();
	}

}
