package com.tax.sign.utils;

import com.alibaba.fastjson.JSON;
import com.yxcheng.token.utils.*;
import net.sf.json.JSONObject;

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

public class IntegrationUtils {
    static String accessKeyID = "bwgf-limit";
    static String accessKeySecret = "bw@2u9%5Ts#2";

    static String host = "http://localhost:8763";

    public static void pushToken(String token,String taxno,String zoneCode) throws Exception{
        String url = "http://localhost:8762/client/pushToken?";
        String signStr = "accessKeyID=%s&signatureNonce=%s&timeStamp=%s&version=1.0";
        String signatureNonce = UUIDGenerator.generate();
        Map<String,Object> param = new HashMap<String,Object>();
        long time = System.currentTimeMillis();

        String timeStamp = DateUtils.formatDate(time,DateUtils.yyyymmddthhmmssz);

        signStr = String.format(signStr,accessKeyID,signatureNonce, timeStamp);
//        url += signStr + ("&signature=" + getSign(signStr));
        url += String.format("&token=%s&taxno=%s&zoneCode=%s&recevDate=%s",token,taxno,zoneCode,time);
        System.out.println("=====url =   " + url);
        param.put("accessKeyID",accessKeyID);
        param.put("signatureNonce",signatureNonce);
        param.put("timeStamp",timeStamp);
        param.put("version","1.0");
        param.put("signature",getSign(signStr));
        param.put("token",token);
        param.put("taxno",taxno);
        param.put("zoneCode",zoneCode);
        param.put("recevDate",time);
        String paramStr = JSON.toJSONString(param);
        System.out.println(paramStr);
        ResponseContent ret = HttpHelper.postJsonEntity(url, paramStr);
        System.out.println(ret.getContent());
    }

    public static String getSign(String data){
        try {
            return Security.getSignature(data.getBytes("UTF-8"),accessKeySecret.getBytes("UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static long getTime() throws Exception{
        String url = host + "/signService/getTime";
        ResponseContent ret = HttpHelper.getUrlRespContent(url);
        System.out.println(ret.getContent());
        JSONObject obj = JSONObject.fromObject(ret.getContent());
        long data = obj.getLong("data");
        return data;
    }

    public static void main(String[] args) throws Exception {
        IntegrationUtils.pushToken("123","23","1333");
    }
}
