package com.iwunu.nbiot.lock.util;

import com.iwunu.nbiot.common.util.Base64;
import com.iwunu.nbiot.common.util.ByteTools;
import com.iwunu.nbiot.common.util.DateUtil;
import com.iwunu.nbiot.common.util.HexUtils;
import com.iwunu.nbiot.common.util.MapToObjUtil;

import java.util.HashMap;
import java.util.Map;


/**
 * @author kinfeng
 * @date 2022/04/14 10:09 AM
 * @desc 上传可变字段解析成map或者指定对象
 */
public class MessageMapParse {
	 private static Integer typeLength = 2;//1个字节指令类型
	 private static Integer randomLength = 4;//2个字节随机数
	 private static Integer serverTypeLength =4;//2个字节服务类型
	 private static Integer timeLength = 8;//4个字节时间戳
	 private static Integer countLength = 4;//2个字节的长度
	 private static Integer endLength = 4;//2个字节结束字符
	 
	 /**
	  * 获取服务类型
	  * @param message
	  * @return
	  */
	 public static  String getServerType(String message){
		 String serverType = message.substring(add(typeLength,randomLength),add(typeLength,randomLength,serverTypeLength));
		 return serverType;
	 }
	 /**
	  * 获取下发类型
	  * @param message
	  * @return
	  */
	 public static  String getCmdType(String message){
		 String cmdType = message.substring(0,typeLength);
		 return ReportDataKeyUtil.REPORT_TYPE.get(cmdType);
	 }
	 /**
	  * 获取下发类型
	  * @param message
	  * @return
	  */
	 public static  String getMessageCmdType(String message){
		 String data = getData(message);
		 String cmdType = data.substring(0,typeLength);
		 return ReportDataKeyUtil.REPORT_TYPE.get(cmdType);
	 }
	 /**
	  * 获取当前上报时间
	  * @param message
	  * @return
	  */
	 public static  String getTime(String message){
		 String time = message.substring(add(typeLength,randomLength,serverTypeLength),add(typeLength,randomLength,serverTypeLength,timeLength));
		 Long date = Long.parseLong(time,16);
		 return DateUtil.stampToDate(date);
	 }	
	 public static  String getStampTime(String message){
		 String time = message.substring(add(typeLength,randomLength,serverTypeLength),add(typeLength,randomLength,serverTypeLength,timeLength));
		 return time;
	 }	
	 /**
	  * 获取当前上报时间
	  * @param message
	  * @return
	  */
	 public static  String getRandom(String message){
		 String random = message.substring(typeLength,add(typeLength,randomLength));
		 return random;
	 }	
	 /**
	  * 获取数据
	  * @param message
	  * @return
	  */
	 public static  String getData(String message){
		 String data = message.substring(add(typeLength,randomLength,serverTypeLength,timeLength,countLength),sub(message.length(),endLength));
		 return data;
	 }
    /**
     * 转成map
     * @param messageLength
     * @param message
     * @return
     */
    public static Map<String,Object> parseMessage(Integer messageLength, String message){
    	if(messageLength==0){ return null ;}
        try{
            Map<String,Object> map = new HashMap<>();
            Integer index =0 ;
            while (index<messageLength){
                String flag = message.substring(index,index+=2).toLowerCase();
                String length = message.substring(index,index+=2);
                String data = message.substring(index,index+=Integer.parseInt(length,16)*2);//todo 和硬件对接长度
                if(flag.equals("19")){
                    map.put(ReportDataKeyUtil.REPORT_TYPE.get(flag),ByteTools.hexStringToString(data));//data为16进制字符串
                }
                else{
                    map.put(ReportDataKeyUtil.REPORT_TYPE.get(flag),Long.valueOf(data,16));//data转为10进制
                }
            }
           
            return map;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
    public static Map<String,Object> parseImageMessage(Integer messageLength, String message){
        try{
            if(messageLength==0){
                return null ;
            }
            Map<String,Object> map = new HashMap<>();
            byte[] array = Base64.decode(message) ;
            message = HexUtils.encode(array);
            Integer index =0 ;
            while (index<messageLength*2){
                String flag = message.substring(index,index+=2).toLowerCase();
                String length = message.substring(index,index+=4);
                String data = message.substring(index,index+=Integer.parseInt(length,16)*2);//todo 和硬件对接长度
                if(Long.valueOf(flag,16)==5L || Long.valueOf(flag,16)==19L){
                    map.put(ReportDataKeyUtil.REPORT_TYPE.get(flag),data);//data为16进制字符串
                }else{
                    map.put(ReportDataKeyUtil.REPORT_TYPE.get(flag),Long.valueOf(data,16));//data转为10进制
                }
            }
            return map;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 转成指定对象
     * @param messageLength
     * @param message
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T parseMessage(Integer messageLength, String message,Class clazz){
        return MapToObjUtil.returnObj(parseMessage(messageLength,message),clazz);
    }
    
    public static Integer add(Integer count ,Integer... numbers) {
    	for(Integer number:numbers) {
    		count += number;
    	}
    	return count;
    }
    public static Integer sub(Integer count ,Integer... numbers) {
    	for(Integer number:numbers) {
    		count -= number;
    	}
    	return count;
    }

}
