package com.yihu.wlyy.service.third.verified;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.wlyy.entity.dict.SystemDict;
import com.yihu.wlyy.service.system.SystemDictService;
import com.yihu.wlyy.service.third.ehr.EhrService;
import com.yihu.wlyy.util.DateUtil;
import com.yihu.wlyy.util.DesUtils;
import com.yihu.wlyy.util.HttpClientUtil;
import com.yihu.wlyy.util.SystemConf;
import org.apache.axis.encoding.Base64;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hzp on 2016/11/21.
 * 实名认证接口
 */
@Service
public class VerifiedService {

    //基卫服务地址
    private String jwUrl = SystemConf.getInstance().getJwUrl();

    //授权码
    private String appkey = "9683dbd88e0111e69beb0050569a506a";

    //对称加密
    private String desKey = "5063ebf54d76b16551e175fc71b0e5c7";

    @Autowired
    private ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 获取随机数
     * @return
     * @throws Exception
     */
    private JSONObject getRandomNumber()  throws Exception
    {
        JSONObject data =  new JSONObject();
        String url = "http://auth.xmca.com.cn:9846/identification/api/v1.0/getRandomNumber.json";

        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("sessionID", "1"));
        params.add(new BasicNameValuePair("appKey", appkey));

        String response = HttpClientUtil.post(url, params, "UTF-8");
        JSONObject responseJson = new JSONObject(response);

        if("200".equals(responseJson.optString("code")))
        {
            data = responseJson.getJSONObject("data");
        }
        else{
            throw new Exception("获取随机数失败："+responseJson.optString("message"));
        }
        return data;
    }


    /**
     * 对称加密
     */
    private String getDesString(String str)  throws Exception
    {
        return DesUtils.encode(str,desKey);
    }


    /********************************************************************************************************************/
    /**
     * 身份认证
     */
    public void faceVerified(String idcard, String username, String picData) throws Exception {
        JSONObject random = getRandomNumber();
        String businessSerial = random.optString("businessSerial");
        String randomNumber = random.optString("randomNumber");
        String sessionID = random.optString("sessionID");

        String url = "http://auth.xmca.com.cn:9846/identification/api/v1.0/authenticate.json";

        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("idcard", getDesString(idcard)));
        params.add(new BasicNameValuePair("username", getDesString(username)));
        //params.add(new BasicNameValuePair("validityStartDate", validityStartDate));
        //validityEndDate
        params.add(new BasicNameValuePair("authenMode", "8"));
        params.add(new BasicNameValuePair("picData", picData));
        //authenData
        //idData
        params.add(new BasicNameValuePair("businessSerial", businessSerial));
        params.add(new BasicNameValuePair("sessionID", sessionID));
        params.add(new BasicNameValuePair("appKey", appkey));

        String response = HttpClientUtil.post(url, params, "UTF-8");
        JSONObject responseJson = new JSONObject(response);

        if("200".equals(responseJson.optString("code")))
        {
            JSONObject data = responseJson.getJSONObject("data");
            String result = responseJson.optString("authenResult");
            if(!result.startsWith("00"))
            {
               if(result.startsWith("1"))
               {
                   throw new Exception("DN及保留数据匹配失败！");
               }
                else if(result.startsWith("01")){
                   throw new Exception("人像匹配失败！");
               }
            }
        }
        else{
            throw new Exception("身份认证失败："+responseJson.optString("message"));
        }
    }

}
