package com.mortals.xhx.module.hik.event.service.impl;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.mortals.framework.common.Rest;
import com.mortals.framework.utils.ServletUtils;
import com.mortals.xhx.base.system.upload.service.UploadService;
import com.mortals.xhx.common.code.HikEventTypeEnum;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.module.hik.event.model.req.callback.recognition.EventData;
import com.mortals.xhx.module.hik.event.model.req.callback.EventsItem;
import com.mortals.xhx.module.hik.event.model.req.callback.recognition.FaceRecognitionResult;
import com.mortals.xhx.module.hik.event.model.req.callback.recognition.Snap;
import com.mortals.xhx.module.hik.face.model.req.img.ImgReq;
import com.mortals.xhx.module.hik.face.service.IHikFaceService;
import com.mortals.xhx.module.realtime.model.RealtimeDataflowEntity;
import com.mortals.xhx.module.realtime.service.RealtimeDataflowService;
import com.mortals.xhx.utils.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.stream.Collectors;

/**
 * 陌生人员识别
 *
 * @author:
 * @date: 2023/4/17 9:27
 */
@Slf4j
public class StrangerRecoginze extends EventTypeAbstract {

    private RealtimeDataflowService dataflowService;

    private IHikFaceService faceService;

    private UploadService uploadService;

    public StrangerRecoginze(int type) {
        super(type);
        this.dataflowService = SpringUtils.getBean(RealtimeDataflowService.class);
        this.faceService = SpringUtils.getBean(IHikFaceService.class);
        this.uploadService = SpringUtils.getBean(UploadService.class);
    }


    @Override
    int getType() {
        return HikEventTypeEnum.重点人员识别事件.getValue();
    }

    @Override
    public void saveEventData(EventsItem event) {
        log.info("处理陌生人事件,event:{}", event.getEventId());
        EventData eventData = event.getData();
        //识别结果
        FaceRecognitionResult faceRecognitionResult = eventData.getFaceRecognitionResult();
        //抓拍信息
        Snap snap = faceRecognitionResult.getSnap();
        //不论识别结果 保存流量数据
        RealtimeDataflowEntity realtimeDataflowEntity = new RealtimeDataflowEntity();
        realtimeDataflowEntity.initAttrValue();
        realtimeDataflowEntity.setSiteId(1L);
        realtimeDataflowEntity.setSiteName("宜宾市民中心");
        realtimeDataflowEntity.setDetectTime(event.getHappenTime());
        String resIndexCodes = eventData.getResInfo().stream().map(i -> i.getIndexCode()).collect(Collectors.joining(","));
        realtimeDataflowEntity.setDevice(resIndexCodes);
        realtimeDataflowEntity.setPicUri(snap.getFaceUrl());
        if (!ObjectUtils.isEmpty(eventData.getResInfo())) {
            realtimeDataflowEntity.setLocation(eventData.getResInfo().get(0).getCn());
        }

        ImgReq imgReq = new ImgReq();
        imgReq.setUrl(realtimeDataflowEntity.getPicUri());
        Rest<String> rest = faceService.downloadPicture(imgReq);
        if (YesNoEnum.YES.getValue() == rest.getCode()) {
            byte[] bytes = Base64.decode(rest.getData());
            if (!ObjectUtils.isEmpty(bytes)) {
                InputStream inputStream = new ByteArrayInputStream(bytes);
                MultipartFile file = ServletUtils.getMultipartFile(inputStream, "snap.jpg");
                String filePath = uploadService.saveFileUpload(file, "file/fileupload/snap/" + DateUtil.today(), null);
                realtimeDataflowEntity.setPicture(filePath);
            }
        }

        realtimeDataflowEntity.setEventId(event.getEventId());
        realtimeDataflowEntity.setEventType(HikEventTypeEnum.陌生人员识别事件.getValue().longValue());
        realtimeDataflowEntity.setCreateUserId(1L);
        realtimeDataflowEntity.setCreateTime(new Date());
        realtimeDataflowEntity.setCreateUserName("system");
        dataflowService.save(realtimeDataflowEntity);
    }

}
