package com.sptg.common.core.util.aliface;

import com.aliyun.facebody20191230.Client;
import com.aliyun.facebody20191230.models.DetectFaceRequest;
import com.aliyun.facebody20191230.models.DetectFaceResponse;
import com.aliyun.facebody20191230.models.DetectFaceResponseBody;
import com.aliyun.teaopenapi.models.Config;
import com.sptg.common.core.beans.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Component
public class FaceDetectUtil {

    private static final String accessKeyId = "LTAI5tDJtFJEVy4MWB5jWvGY";
    private static final String accessKeySecret = "xLyBVJD0RuUlJ1jQuKncnVbw3RCjyZ";

    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.facebody20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "facebody.cn-shanghai.aliyuncs.com";
        return new com.aliyun.facebody20191230.Client(config);
    }

    /*public static void main(String[] args_) throws Exception {
        java.util.List<String> args = java.util.Arrays.asList(args_);
        com.aliyun.facebody20191230.Client client = FaceDetectUtil.createClient("accessKeyId", "accessKeySecret");
        DetectFaceRequest detectFaceRequest = new DetectFaceRequest()
                .setImageURL("http://viapi-customer-pop.oss-cn-shanghai.aliyuncs.com/0c65_1397217182439218_1441c36ad01e46b8ad792d1698e66749");
        // 复制代码运行请自行打印 API 的返回值
        DetectFaceResponse detectFaceResponse = client.detectFace(detectFaceRequest);
    }*/

    public ResultBean detectImage(String imageUrl){


        String regionId = "cn-shanghai";
        //String file = "/home/admin/file/1.jpg"; //或者本地上传
        try (InputStream inputStream = ViapiFileUtilAdvance.buildInputStream(imageUrl)) {
            ViapiFileUtilAdvance fileUtils = ViapiFileUtilAdvance.getInstance(accessKeyId, accessKeySecret, regionId);
            String ossTempFileUrl = fileUtils.upload(inputStream);
            System.out.println(ossTempFileUrl);

            Client client = FaceDetectUtil.createClient(accessKeyId, accessKeySecret);
            DetectFaceRequest detectFaceRequest = new DetectFaceRequest()
                    .setImageURL(ossTempFileUrl);
            // 复制代码运行请自行打印 API 的返回值
            DetectFaceResponse detectFaceResponse = client.detectFace(detectFaceRequest);
            DetectFaceResponseBody.DetectFaceResponseBodyData data = detectFaceResponse.getBody().getData();
            if (!data.getFaceCount().equals(1)){
                //多个人脸，不合格
                return new ResultBean("500.1", "照片有多个人脸，不合格！");
            }else{
//                List<Float> poseList = data.getPoseList();
                List<Float> poseList = data.getPoseList();
                String aDouble = poseList.get(0) + "";
                String bDouble = poseList.get(1) + "";
                String cDouble = poseList.get(2) + "";

                if (Math.abs(Double.valueOf(aDouble)) > 10 || Math.abs(Double.valueOf(bDouble)) > 10 || Math.abs(Double.valueOf(cDouble)) > 10 ){
                    // 人脸姿态不正确，不合格
                    return new ResultBean("500.1", "照片人脸姿态不正确，不合格！");
                }else{
                    DetectFaceResponseBody.DetectFaceResponseBodyDataQualities qualities = data.getQualities();
                    Double point = 85.0D;

//                    Object[] objects = qualities.getBlurList().toArray()[0].toString();

                    if (Double.valueOf(qualities.getBlurList().toArray()[0].toString()) < point){
                        //人脸模糊度，不合格
                        return new ResultBean("500.1", "照片人脸太模糊，不合格！");
                    }
                    if (Double.valueOf(qualities.getFnfList().toArray()[0].toString()) < point){
                        //人脸正确度，不合格
                        return new ResultBean("500.1", "照片正确度不够，不合格！");
                    }
                    if (Double.valueOf(qualities.getGlassList().toArray()[0].toString()) < point){
                        //眼镜等上半脸遮挡对识别的影响分数，不合格
                        return new ResultBean("500.1", "照片眼镜等上半脸遮挡太多，不合格！");
                    }
                    if (Double.valueOf(qualities.getIlluList().toArray()[0].toString()) < point){
                        //光线不足，太暗，不合格
                        return new ResultBean("500.1", "照片光线不足，不合格！");
                    }
                    if (Double.valueOf(qualities.getMaskList().toArray()[0].toString()) < point){
                        //口罩等下半脸遮挡对识别的影响分数，不合格
                        return new ResultBean("500.1", "照片口罩等下半脸遮挡太多，不合格！");
                    }
                    if (Double.valueOf(qualities.getPoseList().toArray()[0].toString()) < point){
                        //姿态对识别的影响分，不合格
                        return new ResultBean("500.1", "照片质量不合格！");
                    }
                    if (Double.valueOf(qualities.getNoiseList().toArray()[0].toString()) < point){
                        //图片噪声对识别的影响分，不合格
                        return new ResultBean("500.1", "照片噪声太大，不合格！");
                    }

                    if (Double.valueOf(qualities.getScoreList().toArray()[0].toString()) < point){
                        //质量综合分数，不合格
                        return new ResultBean("500.1", "照片质量不合格！");
                    }

                }
            }
        }catch (Exception e){
            System.out.println("人脸质量检测异常-" + e.getMessage());
            return new ResultBean("500.1", "照片检测不到人脸！");
        }

        return null;
    }
}
