package org.springblade.third.zongheng.handler;

import com.alibaba.fastjson.JSONObject;
import org.springblade.common.constant.CommonConstant;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.estate.dto.AccessRecordDTO;
import org.springblade.estate.entity.AccessRecord;
import org.springblade.estate.feign.IAccessRecordClient;
import org.springblade.third.zongheng.beans.FaceCompareInfo;
import org.springblade.utils.OssUtil;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class FaceCompareEventHandler implements IDeviceEndEventHandler{

	public static OssUtil ossUtil;
	public static IAccessRecordClient accessRecordClient;

	static {
		ossUtil = SpringUtil.getBean(OssUtil.class);
		accessRecordClient = SpringUtil.getBean(IAccessRecordClient.class);
	}

	@Override
	public R handle(JSONObject eventInfo, List<MultipartFile> file) {
		for(MultipartFile multipartFile : file){
			if(multipartFile.getOriginalFilename().startsWith("bg_")) { //比对图
				R<BladeFile> bladeFileR = ossUtil.uploadFile(multipartFile);
				eventInfo.put("img", bladeFileR.getData().getName());
				break;
			}
		}
		String serialNum = eventInfo.getString("SerialNum");
		String faceCompareInfoStr = eventInfo.getJSONObject("FaceCompareInfo").toJSONString();
		FaceCompareInfo faceCompareInfo = JSONObject.parseObject(faceCompareInfoStr, FaceCompareInfo.class);
		FaceCompareInfo.SnapObj snapObj = faceCompareInfo.getSnap().get(0);
		FaceCompareInfo.MatchOfSnapObj match = snapObj.getMatch();
		FaceCompareInfo.TemperatureOfSnapObj temperatureOfSnapObj = snapObj.getTemperature();
		String userId = match.getId();
		String userName = match.getName();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd'T'HHmmss+08");
		Date openDate = null;
		try {
			openDate = sdf.parse(eventInfo.getString("LocalTime"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		this.createAccessRecord(serialNum, openDate, userId, userName, eventInfo.getString("img"), temperatureOfSnapObj.getTemp());
		return R.status(true);
	}

	private R createAccessRecord(String serialNum, Date openTime, String userId, String userName, String faceUrl, String temperature){
		AccessRecordDTO accessRecord = new AccessRecordDTO();
		accessRecord.setUserId(userId);
		accessRecord.setUserName(userName);
		accessRecord.setSerialNum(serialNum);
		accessRecord.setOpenTime(openTime);
		accessRecord.setOpenType(1);
		accessRecord.setFaceUrl(faceUrl);
		accessRecord.setTemperature(StringUtil.isNoneBlank(temperature) ? new BigDecimal(temperature).compareTo(BigDecimal.ZERO) == 1 ? temperature : null : null);
		if(userId.startsWith(CommonConstant.UserFlag.ENTERPRISE_STAFF_FLAG.getValue())){
			accessRecord.setUserType(CommonConstant.UserFlag.ENTERPRISE_STAFF_FLAG.getValue());
		}else if(userId.startsWith(CommonConstant.UserFlag.ADMIN_FLAG.getValue())){
			accessRecord.setUserType(CommonConstant.UserFlag.ADMIN_FLAG.getValue());
		}
		else if(userId.startsWith(CommonConstant.UserFlag.GUEST_FLAG.getValue())){
			accessRecord.setUserType(CommonConstant.UserFlag.GUEST_FLAG.getValue());
		}
		return accessRecordClient.generateAccessRecord(accessRecord);
	}
}
