package com.jpsoft.shinestar.modules.hikvision.callback;

import com.jpsoft.ehome.callback.MsgNoticeCallBack;
import com.jpsoft.ehome.config.EHomeParamConfig;
import com.jpsoft.shinestar.config.OSSConfig;
import com.jpsoft.shinestar.modules.base.entity.DeviceInfo;
import com.jpsoft.shinestar.modules.base.entity.PersonInfo;
import com.jpsoft.shinestar.modules.base.service.DeviceInfoService;
import com.jpsoft.shinestar.modules.base.service.PersonDeviceFilterLogService;
import com.jpsoft.shinestar.modules.base.service.PersonDeviceLogService;
import com.jpsoft.shinestar.modules.common.utils.OSSUtil;
import com.jpsoft.shinestar.modules.uniview.dto.PersonNotification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.InputStream;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Component
public class MsgNoticeCallBackImpl implements MsgNoticeCallBack {
    @Autowired
    private DeviceInfoService deviceInfoService;

    @Autowired
    private OSSConfig ossConfig;

    @Autowired
    private EHomeParamConfig eHomeParamConfig;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    private PersonDeviceLogService personDeviceLogService;

    @Autowired
    private PersonDeviceFilterLogService personDeviceFilterLogService;

    @Value("${spring.profiles.active}")
    private String profile;

    @Override
    public void notice(String deviceNo, String cardNo, String temperatureStr, String faceImageUrl,String recordDate) {

        try {
            Long personId = Long.valueOf(cardNo);
            BigDecimal temperature = new BigDecimal(temperatureStr);

            DeviceInfo deviceInfo = deviceInfoService.getByDeviceNo(deviceNo);

            if (deviceInfo!=null){
                deviceInfo.setIsOnline(true);
                deviceInfo.setHeartbeatUpdateTime(new Date());

                deviceInfoService.update(deviceInfo);
            }

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

            PersonInfo personInfo = new PersonInfo();
            personInfo.setId(personId);

            try {
                String imageUrl = faceImageUrl;

                //荆楚云docker容器中无法访问服务器外网IP
                if("production".equals(profile)){
                    imageUrl = faceImageUrl.replace(eHomeParamConfig.getRemoteIP(),"127.0.0.1");
                }

                //将KMS地址转为oss地址
                URL url = new URL(imageUrl);

                //创建连接
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setConnectTimeout(5000);

                InputStream faceImageInput = conn.getInputStream();

                //downloadFile?id=0998897E339E52B43BBCDDF515776D
//                String fileName = faceImageUrl.substring(faceImageUrl.lastIndexOf("/") + 1);
                String ossUrl = OSSUtil.upload(ossConfig, "hikvision", "1.jpg", faceImageInput);
                faceImageUrl = ossUrl;

                faceImageInput.close();
            }
            catch (Exception ossEx){
                log.error(ossEx.getMessage());
            }

            int matchStatus = 10;
            String matchMsg = "";

            if (personId==0){
                matchStatus = 2;
                matchMsg = "访客";
            }

            Date recordTime = sdf.parse(recordDate);

            //记录人员记录
            String personDeviceLogId = personDeviceLogService.insertLog(deviceNo,personInfo,temperature,faceImageUrl,recordTime,matchStatus,matchMsg,personId.intValue());

            //记录人员过滤记录
            personDeviceFilterLogService.insertFilterLog(deviceNo,personInfo,temperature,faceImageUrl,recordTime,matchStatus,matchMsg,personId.intValue(),null,null);

            //人员打卡记录上报后加入队列以便后续处理
            PersonNotification personNotification = new PersonNotification();

            personNotification.setPersonId(personId);
            personNotification.setDeviceNo(deviceNo);
            personNotification.setTemperature(temperature);
            personNotification.setRecordTime(recordTime);
            personNotification.setPersonDeviceLogId(personDeviceLogId);

            rabbitTemplate.convertAndSend("personNotificationQueue", personNotification);

            //访客记录表添加进厂时间
            rabbitTemplate.convertAndSend("personGuestRecordQueue",personNotification);
        }
        catch (Exception ex){
            log.error(ex.getMessage());
        }
    }
}
