package cn.hmsoft.ai.identity.baidu;

import java.io.File;
import java.io.FileInputStream;
import java.util.Base64;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.commons.io.IOUtils;

import com.baidu.aip.util.Base64Util;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import cn.hmsoft.ai.baidu.BaiduTokenService;
import cn.hmsoft.ai.identity.IdentifiedResult;
import cn.hmsoft.ai.identity.IdentityAI;
import cn.hmsoft.application.SpringHelper;
import cn.hmsoft.helper.FileHelper;
import cn.hmsoft.helper.http.HttpHelper;

public class BaiduIdentityAI extends IdentityAI {

	@Override
	protected IdentifiedResult verifyByFile(File file, String id_card, String std_name) {
		SpringHelper.getBean(BaiduTokenService.class).getBaiduToken();
		// 请求url
		String url = "https://aip.baidubce.com/rest/2.0/face/v3/person/verify?access_token="
				+ BaiduTokenService.ACCESS_TOKEN;
		IdentifiedResult vResult = new IdentifiedResult();
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("image", Base64Util.encode(FileHelper.readFileByBytes(file.getPath())));
			map.put("image_type", "BASE64");
			map.put("liveness_control", "NORMAL");
			map.put("name", std_name);
			map.put("id_card_number", id_card);

			String param = new GsonBuilder().create().toJson(map);

			String result = HttpHelper.post(url, "application/json", param);
			// System.out.println(result);
			JsonObject json = new JsonParser().parse(result).getAsJsonObject();

			// {"error_code":0,"error_msg":"SUCCESS","log_id":9465058979001,"timestamp":1574165567,"cached":0,"result":{"score":95.3545}}

			int error_code = json.get("error_code").getAsInt();
			if (error_code == 0) {
				vResult.setSuccess(true);
				double score = json.getAsJsonObject("result").get("score").getAsDouble();
				vResult.setPercent(score);
			} else  {
				vResult.setSuccess(false);
				vResult.setError_msg(json.getAsJsonObject("result").get("error_msg").getAsString());
			} 
		} catch (Exception e) {

		}
		return vResult;
	}

	@Override
	protected IdentifiedResult verifyFiles(File sourceFile, File destFile) {
		SpringHelper.getBean(BaiduTokenService.class).getBaiduToken();
		String url = "https://aip.baidubce.com/rest/2.0/face/v3/match?access_token=" + BaiduTokenService.ACCESS_TOKEN;
		IdentifiedResult vResult = new IdentifiedResult();
		try {
			//创建请求参数
			Set<Object> paramSet = new HashSet<Object>();
			Map<String, Object> sourceMap = new HashMap<String, Object>();
			sourceMap.put("image", Base64.getEncoder().encodeToString(IOUtils.toByteArray(new FileInputStream(sourceFile))));
			sourceMap.put("image_type", "BASE64");
			sourceMap.put("face_type", "LIVE");
			sourceMap.put("quality_control", "NONE");//质量要求要低
			sourceMap.put("liveness_control", "NORMAL");
			
			Map<String, Object> destMap = new HashMap<String, Object>();
			destMap.put("image", Base64.getEncoder().encodeToString(IOUtils.toByteArray(new FileInputStream(destFile))));
			destMap.put("image_type", "BASE64");
			destMap.put("face_type", "LIVE");
			destMap.put("quality_control", "NONE");//质量要求要低
			destMap.put("liveness_control", "NORMAL");
			
			paramSet.add(sourceMap);
			paramSet.add(destMap);
			String param = new GsonBuilder().create().toJson(paramSet);
			String result = HttpHelper.post(url, "application/json", param);
			JsonObject json = new JsonParser().parse(result).getAsJsonObject();
			int error_code = json.get("error_code").getAsInt();
			if (error_code == 0) {
				vResult.setSuccess(true);
				double score = json.getAsJsonObject("result").get("score").getAsDouble();
				vResult.setPercent(score);
			} else  {
				vResult.setSuccess(false);
				vResult.setError_msg(json.getAsJsonObject("result").get("error_msg").getAsString());
			} 
		} catch (Exception e) {

		}
		return vResult;
	}

	@Override
	protected IdentifiedResult verifyFiles(String url1, String url2) {
		SpringHelper.getBean(BaiduTokenService.class).getBaiduToken();
		String url = "https://aip.baidubce.com/rest/2.0/face/v3/match?access_token=" + BaiduTokenService.ACCESS_TOKEN;
		IdentifiedResult vResult = new IdentifiedResult();
		try {
			//创建请求参数
			Set<Object> paramSet = new HashSet<Object>();
			Map<String, Object> sourceMap = new HashMap<String, Object>();
			sourceMap.put("image", url1);
			sourceMap.put("image_type", "URL");
			sourceMap.put("face_type", "LIVE");
			sourceMap.put("quality_control", "NONE");//质量要求要低
			sourceMap.put("liveness_control", "NORMAL");
			
			Map<String, Object> destMap = new HashMap<String, Object>();
			destMap.put("image", url2);
			destMap.put("image_type", "URL");
			destMap.put("face_type", "LIVE");
			destMap.put("quality_control", "NONE");//质量要求要低
			destMap.put("liveness_control", "NORMAL");
			
			paramSet.add(sourceMap);
			paramSet.add(destMap);
			String param = new GsonBuilder().create().toJson(paramSet);
			String result = HttpHelper.post(url, "application/json", param);
			JsonObject json = new JsonParser().parse(result).getAsJsonObject();
			int error_code = json.get("error_code").getAsInt();
			if (error_code == 0) {
				vResult.setSuccess(true);
				double score = json.getAsJsonObject("result").get("score").getAsDouble();
				vResult.setPercent(score);
			} else  {
				vResult.setSuccess(false);
				vResult.setError_msg(json.getAsJsonObject("result").get("error_msg").getAsString());
			} 
		} catch (Exception e) {

		}
		return vResult;
	}

}
