package org.springblade.third.zongheng.handler;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springblade.common.constant.CommonConstant;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.estate.dto.AuthorizeRecordsDTO;
import org.springblade.estate.entity.AuthorizeRecords;
import org.springblade.estate.feign.IAuthorizeRecordsClient;
import org.springblade.estate.feign.IUserAccessPermissionsClient;
import org.springblade.estate.vo.AuthorizeRecordsVO;
import org.springblade.third.constants.ZHConstant;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Objects;

public class FrmFacePictureHandler implements IDeviceEndianCommandHandler{

	private static IAuthorizeRecordsClient authorizeRecordsClient;
	private static IUserAccessPermissionsClient userAccessPermissionsClient;

	static {
		authorizeRecordsClient = SpringUtil.getBean(IAuthorizeRecordsClient.class);
		userAccessPermissionsClient = SpringUtil.getBean(IUserAccessPermissionsClient.class);
	}
	@Override
	public R handle(JSONObject responseData) {
		String serialNum = responseData.getString("SerialNum");
		String authReqNo = responseData.getString("CommandSeq");
		List<AuthorizeRecords> list = authorizeRecordsClient.getAuthRecord(serialNum, authReqNo, null);
		if(list.size() > 0){
			AuthorizeRecords authorizeRecords = list.get(0);
			authorizeRecords.setAuthRespData(responseData.toString());
			//更新用户人脸权限
			AuthorizeRecordsDTO authorizeRecordsDTO = Objects.requireNonNull(BeanUtil.copy(authorizeRecords, AuthorizeRecordsDTO.class));
			authorizeRecordsDTO.setPermissions(3); //刷脸开门权限
			if(authorizeRecords.getAuthCmd().equals(ZHConstant.I8H.add_user_face.getDesc())){
				try{
					JSONObject data = responseData.getJSONObject("Data");
					JSONArray resultList = data.getJSONArray("ResultList");
					JSONObject resultData = resultList.getJSONObject(0);
					Integer result = resultData.getInteger("Result");
					if(result == 0){
						authorizeRecordsDTO.setAuthStatus(CommonConstant.AuthStatus.SUCCESS.getValue());
						authorizeRecordsDTO.setAuthRespMsg("人脸新增入库成功");
					}else{
						authorizeRecordsDTO.setAuthStatus(CommonConstant.AuthStatus.FAIL.getValue());
						switch (result){
							case 2:
								authorizeRecordsDTO.setAuthRespMsg("人脸库已满");
								break;
							case 3:
								authorizeRecordsDTO.setAuthRespMsg("人脸分组不存在");
								break;
							case 4:
								authorizeRecordsDTO.setAuthRespMsg("人脸 ID 冲突");
								break;
							case 5:
								authorizeRecordsDTO.setAuthRespMsg("图片太大，要求（1920*1080）");
								break;
							case 6:
								authorizeRecordsDTO.setAuthRespMsg("未检测到人脸");
								break;
							case 7:
								authorizeRecordsDTO.setAuthRespMsg("模板格式错误");
								break;
							default:
								authorizeRecordsDTO.setAuthRespMsg("未知人脸入库异常");
						}
					}
				}catch (Exception e){
					e.printStackTrace();
					authorizeRecordsDTO.setAuthStatus(CommonConstant.AuthStatus.FAIL.getValue());
					authorizeRecordsDTO.setAuthRespMsg("未知人脸更新异常");
					return R.status(true);
				}
				userAccessPermissionsClient.generateUserAccessPermissions(authorizeRecordsDTO);
			}else if(authorizeRecords.getAuthCmd().equals(ZHConstant.I8H.update_user_face.getDesc())){
				try{
					JSONObject data = responseData.getJSONObject("Data");
					JSONArray resultList = data.getJSONArray("ResultList");
					JSONObject resultData = resultList.getJSONObject(0);
					Integer result = resultData.getInteger("Result");
					if(result == 0){
						authorizeRecordsDTO.setAuthRespMsg("人脸更新入库成功");
						authorizeRecordsDTO.setAuthStatus(CommonConstant.AuthStatus.SUCCESS.getValue());
					}else{
						authorizeRecordsDTO.setAuthStatus(CommonConstant.AuthStatus.FAIL.getValue());
						switch (result){
							case 2:
								authorizeRecordsDTO.setAuthRespMsg("人脸分组不存在");
								break;
							case 3:
								authorizeRecordsDTO.setAuthRespMsg("人脸 ID 冲突");
								break;
							default:
								authorizeRecordsDTO.setAuthRespMsg("未知人脸更新异常");
						}
					}
				}catch (Exception e){
					e.printStackTrace();
					authorizeRecordsDTO.setAuthStatus(CommonConstant.AuthStatus.FAIL.getValue());
					authorizeRecordsDTO.setAuthRespMsg("未知人脸更新异常");
					return R.status(true);
				}
				userAccessPermissionsClient.generateUserAccessPermissions(authorizeRecordsDTO);
			}else if(authorizeRecords.getAuthCmd().equals(ZHConstant.I8H.delete_user_face.getDesc())){
				Integer result = responseData.getInteger("Result");
				if(result == 0){
					authorizeRecordsDTO.setAuthStatus(CommonConstant.AuthStatus.SUCCESS.getValue());
					authorizeRecordsDTO.setAuthRespMsg("人脸删除成功");
				}else{
					authorizeRecordsDTO.setAuthStatus(CommonConstant.AuthStatus.FAIL.getValue());
					authorizeRecordsDTO.setAuthRespMsg("未知删除人脸异常");
				}
				userAccessPermissionsClient.deleteUserAccessPermissions(authorizeRecordsDTO);
			}
			return R.status(authorizeRecordsClient.update(authorizeRecords));
		}
		return R.status(true);
	}
}
