package com.guwan.controller.login;

import cn.hutool.core.collection.CollectionUtil;
import com.arcsoft.face.FaceInfo;
import com.arcsoft.face.enums.ExtractType;
import com.arcsoft.face.toolkit.ImageFactory;
import com.arcsoft.face.toolkit.ImageInfo;
import com.guwan.config.GlobalValue;
import com.guwan.config.MinioConfig;
import com.guwan.face.dto.FaceRecognitionResDTO;
import com.guwan.face.service.FaceEngineService;
import com.guwan.util.UUIDUtil;
import io.minio.MinioClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Base64;
import java.util.List;

@RestController
@Slf4j
public class FaceController {

    @Autowired
    private MinioConfig minioConfig;
    @Autowired
    private MinioClient minioClient;
    @Autowired
    private GlobalValue globalValue;
    @Autowired
    private FaceEngineService faceEngineService;


    @GetMapping("/11")
    public Integer test() {
        try {

            String tempOrgImageFile = "photo/t_1f952219ae6848a48fbf282d7d464623.jpg";
            // 调用statObject()来判断对象是否存在。如果不存在, statObject()抛出异常, 否则则代表对象存在。
            minioClient.statObject(minioConfig.getBucketName(), tempOrgImageFile);
            //判断人脸照片是否合格
            //1
            InputStream tempInputStream = minioClient.getObject(minioConfig.getBucketName(),
                    tempOrgImageFile);
            //----------------算法检测----------------------------------------------
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length;
            while ((length = tempInputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, length);
            }
            byte[] bytes = outputStream.toByteArray();
            outputStream.close();
            tempInputStream.close();
            //-------------------------------------------------------------------------
            ImageInfo rgbData = ImageFactory.getRGBData(bytes);
            List<FaceInfo> faceInfoList = faceEngineService.detectFaces(rgbData);
            if (CollectionUtil.isNotEmpty(faceInfoList)) {
                FaceInfo faceInfo = faceInfoList.get(0);
                FaceRecognitionResDTO faceRecognitionResDTO = new FaceRecognitionResDTO();
                faceRecognitionResDTO.setRect(faceInfo.getRect());
                byte[] featureBytes = faceEngineService.extractFaceFeature(rgbData, faceInfo, ExtractType.REGISTER);
                if (featureBytes != null) {
//                            UserRamCache.UserInfo userInfo = new UserCompareInfo();
//                            userInfo.setFaceId(faceAddReqDTO.getName());
//                            userInfo.setName(faceAddReqDTO.getName());
//                            userInfo.setFaceFeature(feature);
//                            //这边注册到内存缓存中，也可以根据业务，注册到数据库中
//                            UserRamCache.addUser(userInfo);
                    //entity.setFeature(Base64.getEncoder().encodeToString(featureBytes));
                    System.out.println(Base64.getEncoder().encodeToString(featureBytes));
                }else{
                    log.error("图片不合格,未检测到人脸");
                    return 2;
                }
            }else{
                log.error("图片不合格,未检测到人脸");
                return 2;
            }


            String orgImageFileName = "face/"+ "o_" + UUIDUtil.uuid() + ".jpg";
            //拷贝临时文件正式文件
            minioClient.copyObject(
                    minioConfig.getBucketName(),
                    orgImageFileName,
                    null,
                    null,
                    minioConfig.getBucketName(),
                    tempOrgImageFile,
                    null,
                    null);



            //删除临时文件oss
            minioClient.removeObject(minioConfig.getBucketName(), tempOrgImageFile);
            //删除本地临时文件
//                    new File(tempFaceFilePath).delete();
        } catch (Exception e) {

            e.printStackTrace();
            return 10;
        }
        return 1;
    }

}
