package com.huyi.service.common.cfcaCommon.impl;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.huyi.service.common.cfcaCommon.ICfcaFaceAuthService;
import com.tianhu.common.core.cfca.CfcaUtil;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Encoder;

import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.google.zxing.client.j2se.MatrixToImageWriter.writeToFile;

@Service
public class CfcaFaceAuthServiceImpl implements ICfcaFaceAuthService {


    @Override
    public Map<String,String> faceAuth(boolean isPro, String institutionid, String name, String returnImage, String identificationNumber, String frontUrl, String backUrl, String remark, String url) throws Exception {
        Map<String,String> map = CfcaUtil.faceAuth(isPro,institutionid,name,returnImage,identificationNumber,frontUrl,backUrl,remark);
        url = url+"?message="+map.get("reqMessage")
                 +"&signature="+map.get("reqSignature")
                 +"&dgtlenvlp="+map.get("requestDgtlEnvlp")
                 +"&signSN="+map.get("requestSignSN")
                 +"&encryptSN"+map.get("requestEncryptSN");
        String fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".jpg";
        String baseUrl = createQrCode(url,fileName);
        Map<String,String> reMap = new HashMap<>();
        reMap.put("originalTxSN",map.get("originalTxSN"));
        reMap.put("fileUrl",baseUrl);
        return reMap;
    }
    public static String createQrCode(String url,  String fileName) {
        try {
            Map<EncodeHintType, String> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 400, 400, hints);
            File file = new File(fileName);
            writeToFile(bitMatrix, "jpg", file);
            FileInputStream inputFile = new FileInputStream(file);
            byte[] buffer = new byte[(int)file.length()];
            inputFile.read(buffer);
            inputFile.close();
//            if (file.exists() || ((file.getParentFile().exists() || file.getParentFile().mkdirs()) && file.createNewFile())) {
//                writeToFile(bitMatrix, "jpg", file);
//                System.out.println("搞定：" + file);
//            }
            return new BASE64Encoder().encode(buffer);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

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

        Map<String,String> map = CfcaUtil.faceAuth(false,"100804","李三","","370724199512106110","http://localhost:8080/ReceiveNoticePage","http://localhost:8080/ReceiveNoticeBackground","");
        System.out.println(map);
        String url= "http://zcyl.keao.tech/#/";
        url = url+"?message="+map.get("reqMessage")
                +"&signature="+map.get("reqSignature")
                +"&dgtlenvlp="+map.get("requestDgtlEnvlp")
                +"&signSN="+map.get("requestSignSN")
                +"&encryptSN="+map.get("requestEncryptSN");
        System.out.println(url);
        String fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".jpg";
//        String fileUrl = createQrCode(url,"D://",fileName);
//        System.out.println(fileUrl);
    }
}
