package com.iwunu.nbiot.lock.util;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.iwunu.nbiot.common.util.ByteTools;
import com.iwunu.nbiot.common.util.DateUtil;
import com.iwunu.nbiot.common.util.HexUtils;

@Component

public class MessageConversionAdapter   {
	@Value("${message.outputService}")
	private String serverType;
	@Value("${message.servicetype.online}")
	private String onlineType;
	@Value("${message.servicetype.openlock}")
	private String openlockType;
	@Value("${message.servicetype.cmd}")
	private String cmdTyp;
	@Value("${message.outputService}")
	private String outputService;
	@Value("${message.multiple}")
	private Integer multiple;
	@Value("${message.openLockData}")
	private String openLockData;
	@Value("${message.end}")
	private String end;	
	private String sendType06;
	private Integer facesLength;
	private Integer offsizeLength;
	public  Integer pitNumLength;
	public  Integer sendDataLength;
	private Integer maxGroupLength;
	/**
	 ** 获取下发数据
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 * 
	 */

	public String getSendData(String data,Integer pitNum,String miniType, String startTime, String endTime) {
		StringBuilder send = new StringBuilder();
		StringBuilder sendData = new StringBuilder();
		sendData.append(HexUtils.toHex(pitNum, 2));
		if(data.length()<8) {
			sendData.append(StringUtils.leftPad(data,8,"0"));
		}else {
			sendData.append(data);
		}
		if(!startTime.equals("")) {
			startTime = DateUtil.dateToStampHex(startTime);
		}
		if(!endTime.equals("")) {
			endTime = DateUtil.dateToStampHex(endTime);
		}	
		sendData.append(startTime).append(endTime);
		send.append(miniType);
		send.append(HexUtils.toHexL(sendData.toString().length()/2+"",2)).append(sendData.toString());
		return send.toString();
	}
	/**
	 ** 获取下发数据
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 * 
	 */

	public String getSendFingerData(String data,Integer pitNum,String miniType, String startTime, String endTime) {
		StringBuilder send = new StringBuilder();
		StringBuilder sendData = new StringBuilder();
		sendData.append(HexUtils.toHex(pitNum, 2));
		if(startTime!=null) {
			startTime = DateUtil.dateToStampHex(startTime);
			sendData.append(startTime);
		}
		if(endTime!=null) {
			endTime = DateUtil.dateToStampHex(endTime);
			sendData.append(endTime);
		}
		sendData.append(data);
		send.append(miniType);
		send.append(HexUtils.toHexL(sendData.toString().length()/2+"",2)).append(sendData.toString());
		return send.toString();
	}
	/**
	 ** 获取多个下发数据
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public String getSendDatas(List<String> params,String stamptime) {
		String random =ByteTools.getRandom();
		StringBuffer sendData = new StringBuffer();
		for(String param : params) {
			JSONObject data = JSONObject.parseObject(param);
			sendData.append(getSendData(data.getString("data"),
					data.getInteger("pitNum"),data.getString("miniType"),
					data.getString("startTime"),data.getString("endTime")));
		}
		this.getSendCmdData(sendData.toString(), random,stamptime);
		return sendData.append(end).toString();
	}		
	/**
	 * 获取下发开锁数据
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public String getOpenLockData(String random) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(serverType).append(random).append(openlockType).append(DateUtil.getStamp())
		.append(HexUtils.toHex(openLockData.length()/2, 4)).append(openLockData).append(end);
		return sendData.toString();
	}
	/**
	 * 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public String getSendCmdData(String data,String random,String stamptime) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(serverType).append(random).append(cmdTyp).append(stamptime)
		.append(HexUtils.toHex(data.length()/2, 4)).append(data).append(end);
		return sendData.toString();
	}
	/**
	 * 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public String getSendFacesData(String deviceId, Integer dataLength, String data, String serviceType,
			String miniType, String random, String stamp,String offsize,Integer count) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(serverType).append(outputService).append(sendType06).append(deviceId).append(random)
				.append(serviceType).append(stamp)
				.append(HexUtils.toHex(dataLength +maxGroupLength+offsizeLength+facesLength, 4)).append(miniType)
				.append(HexUtils.toHex(dataLength+offsizeLength+facesLength, 4)).append(HexUtils.toHex(count,8))
				.append(offsize)
				.append(data)
				.append(end);
		return sendData.toString();
	}

	/**
	 * 
	 * @return
	 */
	public String getSendLength(Integer length) {
		return StringUtils.leftPad(length.toString(), 4);
	}

	/**
	 * 
	 * @param imageData
	 * @param index
	 * @return
	 */
	public String spitImageData(byte[] imageData,Integer index,Integer size) {
		if(imageData.length-index<size) {
			size = imageData.length-index;
		}
		byte[] imageHex = Arrays.copyOfRange(imageData, index,  index+size);
		return HexUtils.bytesToHexString(imageHex, size);
	}
	/**
	 * 开锁
	 * 
	 * @param status
	 * @param stamp
	 * @return
	 */
	public String getOpenLockData(String status, String stamp) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(status).append(stamp);
		return sendData.toString();

	}


}
