package com.ssj.conch.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ssj.weixin.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yangcan
 * 类描述:AI识别服务调用工具类
 * 创建时间:2018年12月26日 下午2:58:57

 */
public class AIServiceUtils {
	//aigpu服务器公网
	public static String AI_SERVICE_URL = "http://47.106.35.168:5000";
	//aigpu服务器内网
	//public static String AI_SERVICE_URL = "http://172.18.106.201:5000";
	private static Logger logger = LoggerFactory.getLogger(AIServiceUtils.class);


	/**
	 * @param filePath
	 * @return
	 * @throws Exception
	
	 * 方法说明:算数计算识别对错
	
	 * 创立日期:2018年12月26日 下午3:35:36
	 * 创建人:yangcan
	
	 */
	public static String calculateAI(String filePath, String ossPath) throws Exception {
		String result = null;
		Map<String,String> querys=new HashMap<String, String>();
    	querys.put("file_path", filePath);
    	querys.put("oss_path", ossPath);
    	HttpResponse response = HttpUtils.doPost(AI_SERVICE_URL, "/",
    			"POST", 
    			new HashMap<String, String>(), 
    			new HashMap<String, String>(), 
    			querys
    			);
		System.out.println(response);
    	if(response.getStatusLine().getStatusCode()==200){
    		result = EntityUtils.toString(response.getEntity());
    		if(!"Error".equals(result)){
    			return result;
    		}else{
    			return null;
    		}
    	}
    	return result;
	}
	
	/**
	 * @param filePath
	 * @return
	 * @throws Exception
	
	 * 方法说明:答题卡识别(废弃)  返参:{ “Wrong_No.”: [0. 0. 0. 0. 0. 1. 1. 1. 1. 0. 0. 0. 0. 0. 1.]}
	 * 创立日期:2018年12月26日 下午3:40:42
	 * 创建人:yangcan
	
	 */
//	public static String answerDetectAI(String filePath) throws Exception {
//		String result = null;
//		Map<String,String> querys=new HashMap<String, String>();
//    	querys.put("file_path", filePath);
//    	HttpResponse response = HttpUtils.doPost("http://47.112.205.102:8080", "/AnswerDetect",
//    			"POST", 
//    			new HashMap<String, String>(), 
//    			new HashMap<String, String>(), 
//    			querys
//    			);
//    	if(response.getStatusLine().getStatusCode()==200){
//    		result = EntityUtils.toString(response.getEntity());
//    		if(!"Error".equals(result)){
//    			return result;
//    		}else{
//    			return null;
//    		}
//    	}
//    	return result;
//	}

	/**
	 * @param querys
	 * @return
	 * @throws Exception

	 * 方法说明:以题搜题

	 * 创立日期:2018年12月26日 下午3:35:36
	 * 创建人:yangcan

	 */
	public static String recognitionExerciseBook (Map<String, String> querys) throws Exception {
		String result = null;
		HttpResponse response = HttpUtils.doPost(AI_SERVICE_URL, "/query",
				"POST",
				new HashMap<String, String>(),
				new HashMap<String, String>(),
				querys
		);

		logger.info("请求ai返回{}", JSON.toJSONString(response));
		if(response.getStatusLine().getStatusCode()==200){
			result = EntityUtils.toString(response.getEntity());
			if(!"Error".equals(result)){
				return result;
			}else{
				return null;
			}
		}
		return result;
	}
	/**
	 * @param querys
	 * @return
	 * @throws Exception

	 * 方法说明: 使用oss网络图片请求识别练习册的接口

	 * 创立日期:2019年10月22日
	 * 创建人:chchen

	 */
	public static String recognitionExerciseBookV1 (Map<String, String> querys) throws Exception {
		String result = null;
		HttpResponse response = HttpUtils.doPost(AI_SERVICE_URL, "/query_via_oss",
				"POST",
				new HashMap<String, String>(),
				new HashMap<String, String>(),
				querys
		);

		logger.info("请求ai返回{}", JSON.toJSONString(response));
		if(response.getStatusLine().getStatusCode()==200){
			result = EntityUtils.toString(response.getEntity());
			if(!"Error".equals(result)){
				return result;
			}else{
				return null;
			}
		}
		return result;
	}

	/**
	 * @param querys
	 * @return
	 * @throws Exception

	 * 方法说明: 查询相似题

	 * 创立日期:2019年10月22日
	 * 创建人:chchen

	 */
	public static String querySimilarQuestion (Map<String, String> querys) throws Exception {
		String result = null;
		HttpResponse response = HttpUtils.doPost(AI_SERVICE_URL, "/querySimilarQuestion",
				"POST",
				new HashMap<String, String>(),
				new HashMap<String, String>(),
				querys
		);

		logger.info("请求ai返回{}", JSON.toJSONString(response));
		if(response.getStatusLine().getStatusCode()==200){
			result = EntityUtils.toString(response.getEntity());
			if(!"Error".equals(result)){
				return result;
			}else{
				return null;
			}
		}
		return result;
	}

	/**
	 * @param
	 * @return 
	 * 方法说明:ai图片识别文本内容
	 * 创建时间:2019/7/12 11:27
	 * @author yangcan
	 */
	public static String discernPicture(String filePath) throws Exception {
		String result = null;
		Map<String,String> querys = new HashMap<String, String>();
		querys.put("file_path", filePath);
		HttpResponse response = HttpUtils.doPost(AI_SERVICE_URL, "/discernPicture",
				"POST",
				new HashMap<String, String>(),
				new HashMap<String, String>(),
				querys
		);
		if(response.getStatusLine().getStatusCode()==200){
			result = EntityUtils.toString(response.getEntity());
			if(!"Error".equals(result)){
				JSONObject jsonObject = JSON.parseObject(result);
				if ("success".equals(jsonObject.getString("status"))){
					StringBuffer sb = new StringBuffer();
					JSONArray contentArray = jsonObject.getJSONObject("data").getJSONArray("content");
					for (int i = 0; i < contentArray.size(); i++) {
						sb.append(contentArray.get(i).toString());
					}
					return sb.toString();
				}
			}else{
				return null;
			}
		}
		return result;
	}

	
	public static void main(String[] args) {
		try {
			Map<String, String> querys = new HashMap<>();
			querys.put("path", "/www/files/data/upload/2020/04/13/e8656971b1634bad87354e9eaf4d4c90.png");
			querys.put("ossPath", "https://img-sharingschool.oss-cn-shenzhen-internal.aliyuncs.com/2020/04/13/e8656971b1634bad87354e9eaf4d4c90.png");
			querys.put("subject", "数学");
			querys.put("grade", "二年级下册");
			querys.put("version", "");
			querys.put("mode", "question");
			long begin = System.currentTimeMillis();
			String result = AIServiceUtils.recognitionExerciseBookV1(querys);
			long end = System.currentTimeMillis();
			System.out.println((end - begin)/1000);
			System.out.println(result);
		} catch (Exception e) {
			e.printStackTrace();
		}

		System.out.println("线程1开始");

//		try {
//			Map<String, String> querys = new HashMap<>();
//			querys.put("path", "/www/files/xtdata/upload/2019/12/11/57f77089f0a14404ab671934ac052b85.jpg");
//			querys.put("ossPath", "https://xt-sharingschool.oss-cn-shenzhen-internal.aliyuncs.com/2019/12/11/57f77089f0a14404ab671934ac052b85.jpg");
//			querys.put("subject", "");
//			querys.put("grade", "");
////			querys.put("version", "");
////			querys.put("mode", "question");
//			System.out.println(JSONObject.toJSON(querys));
//			long begin = System.currentTimeMillis();
//			String result = AIServiceUtils.querySimilarQuestion(querys);
//			long end = System.currentTimeMillis();
//			System.out.println((end - begin)/1000);
//			System.out.println(result);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}

//		try {
//			Map<String, String> querys = new HashMap<>();
//			querys.put("path", "/www/files/xtdata/upload/2019/12/11/57f77089f0a14404ab671934ac052b85.jpg");
//			querys.put("ossPath", "https://xt-sharingschool.oss-cn-shenzhen-internal.aliyuncs.com/2019/12/11/57f77089f0a14404ab671934ac052b85.jpg");
//			querys.put("subject", "");
//			querys.put("grade", "");
////			querys.put("version", "");
////			querys.put("mode", "question");
//			System.out.println(JSONObject.toJSON(querys));
//			long begin = System.currentTimeMillis();
//			String result = AIServiceUtils.querySimilarQuestion(querys);
//			long end = System.currentTimeMillis();
//			System.out.println((end - begin)/1000);
//			System.out.println(result);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}

//        try {
//            String url = "https://xt.sharingschool.com/sz/api/common/common/uploadForm";
//            String filePath = "/images/111.png";
//
//            RestTemplate rest = new RestTemplate();
//            FileSystemResource resource = new FileSystemResource(new File(filePath));
//            MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
//            param.add("files", resource);
//            param.add("accessToken", "FBA31BBCFBDA43ECA57317D384E2FFB9");
//
//            String string = rest.postForObject(url, param, String.class);
//
//            JSONObject jo = JSONObject.parseObject(string);
//
//            String str = jo.getJSONObject("data").getJSONArray("pics").getJSONObject(0).getString("filePath");
//            System.out.println(str);
//
//            Map<String, String> querys = new HashMap<>();
//			querys.put("path", "/www/files/xtdata/upload" + str);
//			querys.put("subject", "语文");
//			querys.put("grade", "六年级上册");
//			querys.put("version", "");
//			querys.put("mode", "question");
//			long begin = System.currentTimeMillis();
//			String result = AIServiceUtils.recognitionExerciseBook(querys);
//			long end = System.currentTimeMillis();
//			System.out.println((end - begin)/1000);
//			System.out.println(result);
//
//        } catch (Exception e){
//            e.printStackTrace();
//        }


//		System.out.println("----------------------------------------------------------------");
//		String path = "/www/files/data/upload/2020/01/16/231e263a201e4d5a81aa0a0deed8019a.jpg";
//        //String path = "";
//		String ossPath = "https://img.sharingschool.com/2020/01/16/231e263a201e4d5a81aa0a0deed8019a.jpg";
//		try{
//			long begin = System.currentTimeMillis();
//			String result = AIServiceUtils.calculateAI(path, ossPath);
//			long end = System.currentTimeMillis();
//			System.out.println((end - begin)/1000);
//			System.out.println("返回结果:"+result);
//		} catch (Exception e){
//			e.printStackTrace();
//		}
	}
}
