package com.tax.token.thirdService.impl;


import com.tax.token.model.SignDataReq;
import com.tax.token.model.TkServer;
import com.tax.token.model.TkToken;
import com.yxcheng.token.utils.HttpHelper;
import com.yxcheng.token.utils.JSONHelper;
import com.yxcheng.token.utils.MD5Util;
import com.yxcheng.token.utils.ResponseContent;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpException;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class SignThirdSJService {

    public Map<String, Object> signData(TkToken tkToken,SignDataReq signDataReq,TkServer tkServer,boolean isFirst) throws IOException, HttpException {
        String timeStamp = String.valueOf(new Date().getTime());
        String password = tkServer.getPassword();

        String sign = MD5Util.MD5Encode( signDataReq.getTaxNo() + tkToken.getKeyCode()+timeStamp+tkServer.getUserName()+password);
        String url = tkServer.getSignUrl() + "?cert="+signDataReq.getTaxNo()+ "&serNo=" + tkToken.getIpServerNo() +"&index="+tkToken.getKeyCode() +
                 "&time=" +timeStamp+"&user=" + tkServer.getUserName() + "&sign=" +sign + "&strData=" +signDataReq.getStrData();
        System.out.println("url=" + url);
        ResponseContent ret  = null;
        if(isFirst){
            ret = HttpHelper.getUrlRespContent(url,30000);
        }else{
            ret = HttpHelper.getUrlRespContent(url);
        }
        Map<String, Object> retMap = null;
        //{code=0, sign=3082014206092a864886f70d010702a08201333082012f020101310b300906052b0e03021a0500300b06092a864886f70d0107013182010e3082010a0201013068305e310b300906035504061302636e31153013060355040b1e0c56fd5bb67a0e52a1603b5c40311d301b06035504031e147a0e52a175355b508bc14e667ba174064e2d5fc331193017060355040d1e10006300610031003000300030003000320206010000030e01300906052b0e03021a0500300d06092a864886f70d01010105000481808e74824c2bf49de9469aab3e0feddea8d0d36a036008ff16af5b69765104267f6eb72a871facc6dbe006505bf33ab7d25be94a09618f71fcbabfbf28c8fa057c50e76ecd0abd3747497cb653418a1a876b7b9ad2328472f7b04d334b29a2a6585d23b724e9965cc69055205060c4b8690b60acba8eee68853f95e7b8db9505f6, message=}
        if(ret!=null){
            String retStr = null;
            try {
                retStr = ret.getContent();
            } catch (Exception e) {
                e.printStackTrace();
            }

            if(StringUtils.isNotBlank(retStr)){
                if(retStr.startsWith("<html>")){
                    return retMap;
                }
                retMap = JSONHelper.parserToMap(retStr);
            }
        }
        return retMap;
    }


    public static void main(String[] args) throws IOException, HttpException {
        SignThirdSJService service = new SignThirdSJService();
        TkToken tkToken = new TkToken();
        tkToken.setIpServerNo("T00006");
        tkToken.setKeyCode("258");

        SignDataReq signDataReq = new SignDataReq();
        signDataReq.setTaxNo("91370602312918881J");
        signDataReq.setStrData("1234");

        TkServer server = new TkServer();

        server.setSignUrl("http://39.105.68.123:8010/token/sign");
        server.setUserName("dx");
        server.setPassword("Dx@Pa33&*)o");
        Map<String, Object> res = service.signData(tkToken, signDataReq, server, false);
        System.out.println(res);

    }
}
