package com.utils;

import com.baidu.aip.face.AipFace;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import sun.misc.BASE64Encoder;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.HashMap;

public class FaceUtil {

    //设置APPID/AK/SK
    public static final String APP_ID = "16236487";
    public static final String API_KEY = "uDf53unPH9qimUSbl6hY9Z1r";
    public static final String SECRET_KEY = "i6S0SgwCvpLGpwj7c10Ta2amcD1vt4BC";


    public static AipFace getAipFace(){
        AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        return client;
    }

    public static String ImageToBase64ByLocal(String imgFile) {// 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        InputStream in = null;
        byte[] data = null;
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);// 返回Base64编码过的字节数组字符串

    }
    public static void main(String[] args) throws Exception {



     //   createUser(client,"1111","face",getBaseStr("e:/face.jpg"));
     //   createUser(client,"2222","face2",getBaseStr("e:/face2.jpg"));
    //  createUser(client,"3333","face3",getBaseStr("e:/face3.jpg"));
     //   createUser(client,"4444","face4",getBaseStr("e:/face4.jpg"));
        //faceGetlist(client);
     //  search(getBaseStr("e:/face.jpg"));

      //  faceDelete(client);
    }


    public static void createUser(String userId,String username,String groupId,String faceBase64Str)  {
        try{
            // 传入可选参数调用接口
            HashMap<String, String> options = new HashMap<String, String>();
            options.put("user_info", username);
            options.put("quality_control", "NONE");
            options.put("liveness_control", "NONE");

            String image = faceBase64Str;
            String imageType = "BASE64";

            // 人脸注册
            JSONObject res = getAipFace().addUser(image, imageType, groupId, userId, options);
            System.out.println(res.toString(2));
        }catch (Exception e){

        }
    }

    public static void updateUser(String userId,String username ,String groupId,String faceBase64Str) {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("user_info", username);
        options.put("quality_control", "NORMAL");
        options.put("liveness_control", "LOW");

        String image = faceBase64Str;
        String imageType = "BASE64";

        // 人脸更新
        JSONObject res = getAipFace().updateUser(image, imageType, groupId, userId, options);
        System.err.println(res.getString("error_msg").equals("user not exist"));
        if(res.getString("error_msg").equals("user not exist")){
            res = getAipFace().addUser(image, imageType, groupId, userId, options);
            System.out.println(res.toString(2));
        }

    }

    public static  void faceDelete() {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();

        String userId = "user1";
        String groupId = "group1";
        String faceToken = "7f30451705b78213a47b7307c9f7561c";

        // 人脸删除
        JSONObject res = getAipFace().faceDelete(userId, groupId, faceToken, options);
        System.out.println(res.toString(2));

    }

    public static  void faceGetlist(String groupId) {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        String userId = "user1";

        // 用户信息查询
        JSONObject res = getAipFace().faceGetlist(userId, groupId, options);
        System.out.println(res.toString(2));
    }

    public static String search(String faceBase64Str,String groupIdList){
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
   //     options.put("quality_control", "NORMAL");
     //   options.put("liveness_control", "LOW");
      //  options.put("user_id", "233451");
        options.put("max_user_num", "1");

        String image = faceBase64Str;
        String imageType = "BASE64";

        // 人脸搜索
        JSONObject res = getAipFace().search(image, imageType, groupIdList, options);
        System.out.println(res.toString(2));
        return res.toString(2);
    }

    public static String getBaseStr(String path){
        FileInputStream in  = null;
        try {
            in = new FileInputStream(new File(path));
            byte[] b = new byte[in.available()];
            in.read(b);
            String base64Str =Base64.getEncoder().encodeToString(b);
            return base64Str;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
