package org.dromara.web.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.dromara.common.core.service.OssService;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.system.domain.bo.TblEventBo;
import org.dromara.system.domain.vo.SysOssUploadVo;
import org.dromara.system.domain.vo.SysOssVo;
import org.dromara.system.service.ISysOssService;
import org.dromara.system.service.ITblEventService;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class KafkaMessageConsumer {
    public void consumerMessage(List<ConsumerRecord<String, Object>> message) {
        System.out.println("收到消息：" + message);
        message.forEach(consumerRecord ->{

            if(consumerRecord.topic().equals("zs_objects_r2p3")){
                    ThreadUtil.execAsync(() -> {
                        try{JSONObject msg =  JSONUtil.parseObj( consumerRecord.value());
                            if(msg.getInt("MsgType")!=1001) return;
                            JSONArray list = msg.getJSONArray("ChannelEvtInfo");
                            for (int i = 0; i < list.size(); i++) {
                                JSONObject item = list.getJSONObject(i);
                                JSONArray envlist = item.getJSONArray("Evt_List");
                                for (int j = 0; j < envlist.size(); j++) {
                                    JSONObject envitem = envlist.getJSONObject(j);
                                    envitem.set("from","源驶科技");
                                    envitem.set("lx", getEventName(envitem.getInt("EvtType")));
                                    TblEventBo tblEventBo = new TblEventBo();
                                    tblEventBo.setCreateTime(DateUtil.date(msg.getLong("Timestamp")));
                                    tblEventBo.setExt2(JSONUtil.toJsonStr(envitem));
                                    List<String> urls = new ArrayList<>();
                                    //下载图片
                                    String path =envitem.getStr("EventImagePath");
                                    String path2 =envitem.getStr("EventImagePath2");
                                    String path3 =envitem.getStr("EventImagePath3");
                                    String path4 =envitem.getStr("EventImagePath4");
                                    String path5 =envitem.getStr("EventVideoPath");
                                    if(!StrUtil.isEmptyIfStr( path)){
                                        path = downloadImage(path);
                                        urls.add( path);
                                    }
                                    if(!StrUtil.isEmptyIfStr( path2)){
                                        path2 = downloadImage(path2);
                                        urls.add( path2);
                                    }
                                    if(!StrUtil.isEmptyIfStr( path3)){
                                        path3 = downloadImage(path3);
                                        urls.add( path3);
                                    }
                                    if(!StrUtil.isEmptyIfStr( path4)){
                                        path4 = downloadImage(path4);
                                        urls.add( path4);
                                    }
                                    if(!StrUtil.isEmptyIfStr( path5)){
                                        path5 = downloadImage(path5);
                                        urls.add( path5);
                                    }
                                    tblEventBo.setExt1(JSONUtil.toJsonStr(urls));
                                    tblEventBo.setAddr(msg.getStr("DevNo"));
                                    SpringUtils.getBean(ITblEventService.class).insertByBo(tblEventBo);
                                }
                            }

                        }catch (Exception e){
                            e.printStackTrace();
                        }

                    });

            }
        });

        //消息确认
//        ack.acknowledge();
    }

    private String downloadImage(String path){
        if(!FileUtil.exist("./temp/")){
            FileUtil.mkdir("./temp/");
        }
        File file = HttpUtil.downloadFileFromUrl(path, FileUtil.file("./temp/"));
        SysOssVo oss = SpringUtils.getBean(ISysOssService.class).upload(file);
        return oss.getFileName();
    }


    /**
     * 根据传入的十进制事件码返回对应的事件名称
     *
     * @param code 事件码（十进制整数）
     * @return 对应的事件名称，若未匹配到则返回 null
     */
    public static String getEventName(int code) {
        switch (code) {
            case 2: return "慢速";
            case 4: return "超速";
            case 8: return "压线";
            case 16: return "异常变道";
            case 17: return "周界防范";
            case 64: return "进入非法区域";
            case 65: return "机动车进入非法区域";
            case 66: return "非机动车进入非法区域";
            case 128: return "非法停止";
            case 129: return "机动车非法停止";
            case 130: return "非机动车非法停止";
            case 256: return "非法逆行";
            case 257: return "机动车逆行";
            case 258: return "非机动车逆行";
            case 259: return "行人非法逆行";
            case 512: return "抛洒物";
            case 1024: return "拥堵";
            case 2048: return "行人";
            case 2304: return "人员攀爬";
            case 2305: return "翻越栏杆";
            case 2306: return "人员聚集";
            case 2307: return "人员离岗";
            case 2308: return "徘徊检测";
            case 2309: return "人员滞留";
            case 2310: return "人员睡岗";
            case 2311: return "未穿工服检测";
            case 2312: return "未戴安全帽";
            case 2313: return "抽烟";
            case 2314: return "打电话";
            case 2315: return "跌倒";
            case 2316: return "陌生人检测";
            case 2317: return "火焰检测";
            case 2318: return "有色气体检测";
            case 2319: return "占用消防通道";
            case 2320: return "人员过少";
            case 2321: return "打架";
            case 2322: return "玩手机";
            case 2323: return "下蹲";
            case 2324: return "奔跑";
            case 2325: return "未戴口罩";
            case 2326: return "电动车入";
            case 2327: return "电梯开门";
            case 2328: return "落水";
            case 2329: return "渣土车未盖";
            case 2330: return "小动物";
            case 2331: return "未穿反光衣";
            case 2332: return "短袖";
            case 2333: return "长袖";
            case 2334: return "灭火器移位";
            case 2335: return "遗留物";
            case 2336: return "占道经营";
            case 2337: return "垃圾堆积";
            case 2338: return "垃圾满溢";
            case 2339: return "道路积水";
            case 2340: return "裸土未盖";
            case 2341: return "垃圾桶起火";
            case 2342: return "焚烧垃圾";
            case 2343: return "焚烧秸秆";
            case 2344: return "街道垃圾识别";
            case 2345: return "违规店外经营";
            case 2346: return "违规非法摆摊";
            case 2347: return "违规撑伞";
            case 2348: return "人员越界";
            case 2349: return "重点人员检测";
            case 2350: return "老鼠识别";
            case 2351: return "电梯超员";
            case 4096: return "大货车禁行";
            case 4097: return "危化品车辆";
            case 4102: return "大货车慢行";
            case 4103: return "大货车超速";
            case 8192: return "应急车道占道";
            case 16384: return "非法走机动车道";
            case 16386: return "外卖/快递车闯入";
            case 32768: return "违法上下客";
            case 32769: return "未礼让行人";
            case 32770: return "区域人数检测";
            case 65536: return "施工";
            case 131072: return "交通事故";
            case 262144: return "违法通行";
            case 262145: return "违法直行";
            case 262146: return "违法右转";
            case 262147: return "违法左转";
            case 262148: return "违法掉头";
            case 262149: return "违法压线";
            case 524288: return "排队超限";
            case 524289: return "机动车驶离";
            case 1048576: return "机动车危险驾驶";
            case 2097152: return "非机动车未戴头盔驾驶";
            case 2097153: return "非机动车载人";
            case 4194304: return "排队溢出";
            case 8388608: return "高温";
            case 134217729: return "行人闯红灯";
            case 134217730: return "非机动车闯红灯";
            default: return null; // 可选：返回默认值如 "未知事件"
        }
    }

}
