package com.tax.token.thirdService.impl;

import com.alibaba.fastjson.JSONObject;
import com.tax.token.model.TkServer;
import com.tax.token.model.TkToken;
import com.tax.token.model.TkZoneInfo;
import com.tax.token.thirdService.TokenThirdServiceI;
import com.tax.token.utils.MakeSignatureUtil;
import com.yxcheng.token.utils.HttpHelper;
import com.yxcheng.token.utils.JSONHelper;
import com.yxcheng.token.utils.ObjectUtil;
import com.yxcheng.token.utils.UUIDGenerator;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class GetTokenFromSzyhTax implements TokenThirdServiceI {

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

    @Override
    public Map<String, Object> invoke(String uuIdFlag, TkToken tokenEntity, TkZoneInfo zoneInfo, TkServer tkServer, boolean isFirst) throws IOException, HttpException {

        String uuidFlag = UUIDGenerator.generate();
        String taxno = tokenEntity.getTaxno();
        String username = tkServer.getUserName();
        String passWord = tkServer.getPassword();

        String timeStamp = MakeSignatureUtil.makeTimeStamp();
        String signatureNonce = MakeSignatureUtil.makeSignatureNonce();
        String version = "1.0";
        String makeSignature =
                MakeSignatureUtil.makeSignature(username, signatureNonce, timeStamp, version, passWord);
        String url = tkServer.getReqUrl() + "?AccessKeyID=" + username + "&SignatureNonce="
                + signatureNonce + "&TimeStamp=" + timeStamp + "&Version=" + version + "&Signature="
                + makeSignature;

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("OrgTaxNum",taxno);

        System.out.println("url=" + url);
        String ret = HttpHelper.postWithJson(url, jsonObject.toString());
        Map<String, Object> retMap = null;
        try {
            if(StringUtils.isNotBlank(ret)){
                retMap = JSONHelper.parserToMap(ret);
                String code = ObjectUtil.fixNull(retMap.get("Code"));
                String message = ObjectUtil.fixNull(retMap.get("Message"));
                if(code.equals("200")){
                    Map tokenMap = (Map)retMap.get("token");
                    retMap.put("token", ObjectUtil.fixNull(tokenMap.get("key2")));
                    retMap.put("code","0");
                    retMap.put("message", message);
                } else {
                    retMap.put("code",code);
                    retMap.put("message", message);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info(uuidFlag + " taxno is " + taxno + ",request is " + url + ",response is " + ret + ",retMap is " + retMap);
        return retMap;
    }


    public static void main(String[] args) throws IOException, HttpException {
        GetTokenFromSzyhTax utils = new GetTokenFromSzyhTax();
        TkToken tkToken = new TkToken();
        tkToken.setTaxno("91310000100019382F");

        TkServer server = new TkServer();
        server.setUserName("rzsUser");
        server.setPassword("882D8D19FD24DC5166C3E25259C0CE4E");
        server.setReqUrl("https://t-jxp-ports-fp.bigfintax.com/signServer/getLoginToken");

        utils.invoke("aaa", tkToken,null,server,true);

    }
}
