package jt809.handle;

import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.json.JSONUtil;
import jt809.common.util.CommonUtils;
import  jt809.packet.JT809Packet0x1202;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.dromara.system.domain.GpsData;
import org.dromara.util.CamelDataUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;

/**
 * @Author: Xiuming Lee
 * @Date: 2019/9/23 16:04
 * @Version 1.0
 * @Describe:
 */
@Import(cn.hutool.extra.spring.SpringUtil.class)
public class JT809Packet0x1202Handle extends SimpleChannelInboundHandler<JT809Packet0x1202> {
    private static Logger log = LoggerFactory.getLogger(JT809Packet0x1202Handle.class);


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, JT809Packet0x1202 msg) {
//        log.info("车辆定位信息：{}", msg.toString());
        if (msg.getVec1() > 0 || msg.getVec2() > 0) {
            GpsData gpsData = new GpsData();
            gpsData.setDevicecode(convertToPinyin(msg.getVehicleNo().trim()));
            gpsData.setLat((double) (msg.getLat()/1000000.0));
            gpsData.setLng((double) (msg.getLon()/1000000.0));
            gpsData.setAlt(0.0);
            gpsData.setSpeed((double) (msg.getVec1() > 0?msg.getVec1():msg.getVec2()));

            final  CamelDataUtil camelDataUtil = SpringUtil.getBean("camelDataUtil");
            camelDataUtil.processGpsData(gpsData);
        }



    }


    public static String convertToPinyin(String chinese)  {
        try {
            StringBuilder pinyin = new StringBuilder();
            char[] chars = chinese.toCharArray();
            HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
            format.setToneType(net.sourceforge.pinyin4j.format.HanyuPinyinToneType.WITHOUT_TONE);

            for (char c : chars) {
                String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);

                if (pinyinArray != null&& pinyinArray.length>0) {
                    pinyin.append(pinyinArray[0]);
                } else {
                    pinyin.append(c);
                }
            }
            return pinyin.toString().trim();
        }catch (Exception e){
           System.out.println(e.getMessage());
        }
        return "car_";

    }


//    public static void main(String[] args) {
//       System.out.println((double) 26956477/100000);
//    }
}
