package com.yh.saas.plugin.yiliangyiyun.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.ServiceException;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.yh.saas.plugin.yiliangyiyun.entity.view.ConfigDO;
import com.yh.saas.plugin.yiliangyiyun.entity.view.DistinguishView;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EntityAnalyse {

    public static DistinguishView bankShibie(String bankImg) throws ServiceException {
        String appcode = "2d59bfa794994f5f94d1d98b7b5bd102";
        String host = "https://api06.aliyun.venuscn.com";
        String path = "/ocr/bank-card";
        String method = "POST";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("pic", bankImg);
        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            String body = EntityUtils.toString(response.getEntity());
            System.out.println(body);
            JSONObject jsonObject = JSONObject.parseObject(body);
            if(jsonObject.getString("ret").equals("200")){
                JSONObject data = JSONObject.parseObject(jsonObject.getString("data"));
                DistinguishView distinguishView = new DistinguishView();
                distinguishView.setBankNo(data.getString("number"));
                distinguishView.setBankName(data.getString("bank"));

                host = "https://yhkgsd.market.alicloudapi.com";
                path = "/bankcard/region";
                method = "GET";
                headers = new HashMap<String, String>();
                //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
                headers.put("Authorization", "APPCODE " + appcode);
                querys = new HashMap<String, String>();
                querys.put("bankcard", distinguishView.getBankNo());
                try {
                    response = HttpUtils.doGet(host, path, method, headers, querys);
                    body = EntityUtils.toString(response.getEntity());
                    System.out.println(body);
                    jsonObject = JSONObject.parseObject(body);
                    if(jsonObject.getString("code").equals("0")){
                        data = JSONObject.parseObject(jsonObject.getString("data"));
                        JSONObject detail = JSONObject.parseObject(data.getString("detail"));
                        distinguishView.setBankName(detail.getString("bankName"));
                        String[] area = detail.getString("area").split(" - ");
                        String province = "";
                        String city = "";
                        if(area.length > 1){
                            province = area[0];
                            city = area[1];
                        }

                        distinguishView.setBankPhone(detail.getString("bankPhone"));
                        host = "https://cnaps.market.alicloudapi.com";
                        path = "/lianzhuo/querybankaps";
                        method = "GET";
                        headers = new HashMap<String, String>();
                        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
                        headers.put("Authorization", "APPCODE " + appcode);
                        querys = new HashMap<String, String>();
                        querys.put("card", distinguishView.getBankNo());
                        querys.put("province", province);
                        querys.put("city", city);
                        querys.put("bank", distinguishView.getBankName());
                        try {
                            response = HttpUtils.doGet(host, path, method, headers, querys);
                            System.out.println(response.toString());
                            body = EntityUtils.toString(response.getEntity());
                            System.out.println(body);
                            jsonObject = JSONObject.parseObject(body);
                            JSONObject jsonData = jsonObject.getJSONObject("data");
                            JSONObject res = jsonObject.getJSONObject("resp");
                            if(res.get("RespCode").equals("200")){
                                JSONArray jsonArray = jsonData.getJSONArray("record");
                                List<String> bankList = new ArrayList<>();
                                for(int i=0;i<jsonArray.size();i++) {
                                    bankList.add(jsonArray.getJSONObject(i).get("lName").toString());
                                }
                                distinguishView.setBankNameZhihang(bankList);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return distinguishView;
            }
            else{
                System.out.println(jsonObject.getString("msg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static DistinguishView personShibie(String personImg) throws ServiceException {
        String appcode = "2d59bfa794994f5f94d1d98b7b5bd102";
        //API产品路径
        String requestUrl = "https://personcard.market.alicloudapi.com/ai_market/ai_ocr_universal/shen_fen_zheng/ch/v1";
        //阿里云APPCODE
        DistinguishView distinguishView = new DistinguishView();
        CloseableHttpClient httpClient = null;
        try{
            httpClient = HttpClients.createDefault();
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            //启用URL方式进行识别
            //内容数据类型是图像文件URL链接
            params.add(new BasicNameValuePair("IMAGE", personImg));
            params.add(new BasicNameValuePair("IMAGE_TYPE", "1"));

            // 创建一个HttpPost实例
            HttpPost httpPost = new HttpPost(requestUrl);
            httpPost.addHeader("Authorization","APPCODE " + appcode);
            httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

            // 设置请求参数
            httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));

            // 发送POST请求
            HttpResponse execute = httpClient.execute(httpPost);

            // 获取状态码
            int statusCode = execute.getStatusLine().getStatusCode();
            System.out.println(statusCode);

            // 获取结果
            HttpEntity entity = execute.getEntity();
            String body = EntityUtils.toString(entity);
            System.out.println(body);
            JSONObject jsonObject = JSONObject.parseObject(body);
//            if(type.equals("recPerson"))
//            {
                distinguishView.setRecPerson(jsonObject.getJSONObject("身份证识别实体信息").getJSONObject("身份证人像面实体信息").getString("姓名"));
                distinguishView.setRecPersonNo(jsonObject.getJSONObject("身份证识别实体信息").getJSONObject("身份证人像面实体信息").getString("身份证号"));
                distinguishView.setRecPersonAddr(jsonObject.getJSONObject("身份证识别实体信息").getJSONObject("身份证人像面实体信息").getString("住址"));
//            }
//            else if(type.equals("driver"))
//            {
//                distinguishView.setDriver(jsonObject.getJSONObject("身份证识别实体信息").getJSONObject("身份证人像面实体信息").getString("姓名"));
//                distinguishView.setDriverNo(jsonObject.getJSONObject("身份证识别实体信息").getJSONObject("身份证人像面实体信息").getString("身份证号"));
//                distinguishView.setDriverAddress(jsonObject.getJSONObject("身份证识别实体信息").getJSONObject("身份证人像面实体信息").getString("住址"));
//            }
            return distinguishView;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
