package com.sifang.modules.sys.util;


import com.aliyun.ocr_api20210707.Client;
import com.aliyun.ocr_api20210707.models.RecognizeBankCardRequest;
import com.aliyun.ocr_api20210707.models.RecognizeBankCardResponse;
import com.aliyun.ocr_api20210707.models.RecognizeIdcardRequest;
import com.aliyun.ocr_api20210707.models.RecognizeIdcardResponse;
import com.aliyun.tea.*;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.Common;
import com.aliyun.teautil.models.RuntimeOptions;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.sifang.modules.alioss.OSSClientConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class OcrUtils {

    private static Logger logger = LoggerFactory.getLogger(OcrUtils.class);

    /**
     * 使用AK&SK初始化账号Client
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        return new Client(config);
    }

    public static String uploadImgIdentify(String urlImg) {
        RecognizeIdcardRequest recognizeIdcardRequest = new RecognizeIdcardRequest();
        RuntimeOptions runtime = new RuntimeOptions();
        String data = "";
        try {
            Client client = OcrUtils.createClient(OSSClientConstants.ACCESS_KEY_ID, OSSClientConstants.ACCESS_KEY_SECRET);
            recognizeIdcardRequest.setUrl(urlImg);
            RecognizeIdcardResponse response = client.recognizeIdcardWithOptions(recognizeIdcardRequest, runtime);
            data = response.getBody().getData();
            logger.info("data:{}", data);
            Gson gson = new Gson();
            Map<String, Object> map = gson.fromJson(data, Map.class);
            logger.info("map:{}",map);
            logger.info("map.data:{}",map.get("data"));

        } catch (TeaException error) {
            // 错误 message
            logger.info("error.getMessage:{}",error.getMessage());
            // 诊断地址
            logger.info("error.getData:{}", error.getData().get("Recommend"));
            logger.info("error.message:{}", Common.assertAsString(error.message));
            logger.error(error.getMessage(), error);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);

            logger.error(_error.getMessage(), _error);
            // 错误 message
            logger.info("error.getMessage:{}",error.getMessage());
            // 诊断地址
            logger.info("error.getData:{}", error.getData().get("Recommend"));
            logger.info("error.message:{}", Common.assertAsString(error.message));
        }
        return data;
    }

    public static String uploadImgBankCard(String urlImg) {
        RecognizeBankCardRequest recognizeBankCardRequest = new RecognizeBankCardRequest();
        String data = "";
        try {
            Client client = OcrUtils.createClient(OSSClientConstants.ACCESS_KEY_ID, OSSClientConstants.ACCESS_KEY_SECRET);
            recognizeBankCardRequest.setUrl(urlImg);
            RecognizeBankCardResponse response = client.recognizeBankCard(recognizeBankCardRequest);
            data = response.getBody().getData();
            logger.info("data:{}", data);
            Gson gson = new Gson();
            Map<String, Object> map = gson.fromJson(data, Map.class);
            logger.info("map:{}",map);
            logger.info("map.data:{}",map.get("data"));

        } catch (TeaException error) {
            // 错误 message
            logger.info("error.getMessage:{}",error.getMessage());
            // 诊断地址
            logger.info("error.getData:{}", error.getData().get("Recommend"));
            logger.info("error.message:{}", Common.assertAsString(error.message));
            logger.error(error.getMessage(), error);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);

            logger.error(_error.getMessage(), _error);
            // 错误 message
            logger.info("error.getMessage:{}",error.getMessage());
            // 诊断地址
            logger.info("error.getData:{}", error.getData().get("Recommend"));
            logger.info("error.message:{}", Common.assertAsString(error.message));
        }
        return data;
    }

    public static void main(String[] args_) throws Exception {
        String img = "https://obs-hncs2.cucloud.cn/entry-imgs/energy_img/png/20240112/54c0406c4a7848bfadc78eeddacef18a/478c44214cdb400ab3922c8e9f4a3436.png";
        uploadImgIdentify(img);
    }
}
