package com.yxcheng.token.web.controller.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yxcheng.token.utils.*;
import org.junit.Test;

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

public class TokenUnitTest {

//    static String host = "http://47.94.208.133:8762";
//    static String host = "http://127.0.0.1:8762";
    static String host = "https://api.xfapiao.com";
    String accessKeyID = "eyongyou";
    static String accessKeySecret = "yongyou!@#566ccd";

//        String accessKeyID = "esscj";
//    static String accessKeySecret = "esscj#@rrab1s";

//    String accessKeyID = "echangchen";
//    static String accessKeySecret = "echangchen#@rrwea1";
//    String accessKeyID = "edonggang";
//    static String accessKeySecret = "donggang@PO!#N98";

//    String accessKeyID = "youqian";
//    static String accessKeySecret = "youqian!@#312bba";
    /**
     * zone   91430600081385810Q   -> 4300   特殊的是4403
     *
     * @throws Exception
     */
    @Test
    public void testPushToken() throws Exception{
        String url = host + "/client/pushToken?";
        String signStr = "accessKeyID=%s&signatureNonce=%s&timeStamp=%s&version=1.0";

        String signatureNonce = UUIDGenerator.generate();
        long time = System.currentTimeMillis();

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

        signStr = String.format(signStr,accessKeyID,signatureNonce, timeStamp);
        url += signStr + ("&signature=" + getSign(signStr));
        String sign = getSign(signStr);


        url += "&taxno=91130700550417847R&dqdm=3200&serverRandom=123";


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        Map map = new HashMap<>();
        map.put("accessKeyID",accessKeyID);
        map.put("signatureNonce",signatureNonce);
        map.put("timeStamp",timeStamp);
        map.put("version",1.0);
        map.put("signature",sign);
        map.put("taxno","91430600081385810Q");
        map.put("zoneCode","4300");
        map.put("token","987654321");
        map.put("recevDate",sdf.format(new Date()));


        System.out.println(url);
        ResponseContent ret = HttpHelper.postJsonEntity(url, JSON.toJSONString(map));
        System.out.println(ret.getContent());
    }




    @Test
    public void testQueryTokenErrorInfo() throws Exception{
        String url = host + "/client/queryTokenErrorInfo";

        JSONObject json = new JSONObject();
        json.put("taxno", "91511011MA6444HM2E");
        json.put("userCode", "qixiangyun");

        System.out.println("testQueryTokenErrorInfo request url:" + url + ",param:" + json.toJSONString());

        ResponseContent ret = HttpHelper.postJsonEntity(url, json.toString());
        System.out.println(ret.getContent());
    }


    @Test
    public void testGetCookie() throws Exception{
        String url = host + "/client/getCookie";
        String signStr = "AccessKeyID=%s&SignatureNonce=%s&TimeStamp=%s&Version=1.0";

        String signatureNonce = UUIDGenerator.generate();
        long time = System.currentTimeMillis();

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

        signStr = String.format(signStr,accessKeyID,signatureNonce, timeStamp);

        Map param = new HashMap<>();
        param.put("accessKeyID",accessKeyID);
        param.put("signatureNonce",signatureNonce);
        param.put("timeStamp",timeStamp);
        param.put("version","1.0");
        param.put("signature",getSign(signStr));
        param.put("taxno","91510100MA6A5JGH7B");
        param.put("zoneCode","5100");
        System.out.println(url);
        ResponseContent ret = HttpHelper.postJsonEntity(url,JSON.toJSONString(param));
        System.out.println(ret.getContent());
    }

    @Test
    public void testGetRealCookie() throws Exception{
        String url = host + "/client/getRealCookie";
        String signStr = "accessKeyID=%s&signatureNonce=%s&timeStamp=%s&version=1.0";

        String signatureNonce = UUIDGenerator.generate();
        long time = System.currentTimeMillis();

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

        signStr = String.format(signStr,accessKeyID,signatureNonce, timeStamp);

        Map param = new HashMap<>();
        param.put("accessKeyID",accessKeyID);
        param.put("signatureNonce",signatureNonce);
        param.put("timeStamp",timeStamp);
        param.put("version","1.0");
        param.put("signature",getSign(signStr));
        param.put("taxno","92370203MA3R2F3Q03");
        param.put("zoneCode","3702");
//        param.put("userInfo",Base64Util.encode("刘志怡021323[a%b@q]Lzy672672[a%b@q]"));

        Map userparam = new HashMap<>();
//        userparam.put("nsrsbh","914404003980391901");
//        userparam.put("username","刘志怡021323");
//        userparam.put("password","Lzy672672");
//
        userparam.put("loginType","b");
        userparam.put("username","ZkDxl0731");
        userparam.put("password","18561312788");
        userparam.put("phone","18561312788");

        param.put("userInfoJson",userparam);
        System.out.println(url);
        System.out.println(JSON.toJSONString(param));
        ResponseContent ret = HttpHelper.postJsonEntity(url,JSON.toJSONString(param));
        System.out.println(ret.getContent());
    }

    @Test
    public void testGetEtaxCookie() throws Exception{
        String url = host + "/client/checkEtaxLoginInfo";
        String signStr = "AccessKeyID=%s&SignatureNonce=%s&TimeStamp=%s&Version=1.0";

        String signatureNonce = UUIDGenerator.generate();
        long time = System.currentTimeMillis();

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

        signStr = String.format(signStr,accessKeyID,signatureNonce, timeStamp);

        Map param = new HashMap<>();
        param.put("accessKeyID",accessKeyID);
        param.put("signatureNonce",signatureNonce);
        param.put("timeStamp",timeStamp);
        param.put("version","1.0");
        param.put("signature",getSign(signStr));
        param.put("taxno","91440101MA5CRHH33W");
        param.put("zoneCode","4400");

//        param.put("site","4400");
//        param.put("target","4400");

        Map userparam = new HashMap<>();

        userparam.put("loginType","b");
        userparam.put("nsrsbh","91440101MA5CRHH33W");
        userparam.put("username","Fanny123456");
        userparam.put("userType","办税人");
        userparam.put("password","Fanny123456");

        param.put("userInfoJson",userparam);
        System.out.println(url);
        System.out.println(JSON.toJSONString(param));
        ResponseContent ret = HttpHelper.postJsonEntity(url,JSON.toJSONString(param));
        System.out.println(ret.getContent());
    }

    @Test
    public void testgetToken() throws Exception{
        String url = host + "/client/getToken?";
        String signStr = "accessKeyID=%s&signatureNonce=%s&timeStamp=%s&version=1.0";

        String signatureNonce = UUIDGenerator.generate();
        long time = System.currentTimeMillis();

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

        signStr = String.format(signStr,accessKeyID,signatureNonce, timeStamp);
        url += signStr + ("&signature=" + getSign(signStr));


        url += "&taxno=91440101MA9Y85B1X6&zoneCode=4400&userCode=edaxiang";




        System.out.println(url);
        ResponseContent ret = HttpHelper.getUrlRespContent(url);
        System.out.println(ret.getContent());
    }

    @Test
    public void testCleanCookie() throws Exception{
        String url = host + "/client/cleanCookie";
        String signStr = "AccessKeyID=%s&SignatureNonce=%s&TimeStamp=%s&Version=1.0";

        String signatureNonce = UUIDGenerator.generate();
        long time = System.currentTimeMillis();

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

        signStr = String.format(signStr,accessKeyID,signatureNonce, timeStamp);

        Map param = new HashMap<>();
        param.put("accessKeyID",accessKeyID);
        param.put("signatureNonce",signatureNonce);
        param.put("timeStamp",timeStamp);
        param.put("version","1.0");
        param.put("signature",getSign(signStr));
//        param.put("taxno","91510100MA65M8928X");
//        param.put("zoneCode","5100");
//        param.put("userInfo",Base64Util.encode("18782909536[a%b@q]YH111000[a%b@q]18782909536[a%b@q]"));

        param.put("taxno","914404003980391901");
        param.put("zoneCode","4400");
//        param.put("userInfo",Base64Util.encode("刘志怡021323[a%b@q]Lzy672672[a%b@q]"));

        Map userparam = new HashMap<>();

        userparam.put("loginType","b");
        userparam.put("nsrsbh","91440101MA5CRHH33W");
        userparam.put("username","Fanny123456");
        userparam.put("userType","办税人");
        userparam.put("password","Fanny123456");

        param.put("userInfoJson",userparam);

        System.out.println(url);
        String ret = HttpHelper.postWithJson(url,JSON.toJSONString(param));
        System.out.println(ret);
    }

    @Test
    public void testUpdateErrorInfo() throws Exception{
        String url = host + "/client/updateErrorInfo";

        JSONObject json = new JSONObject();
        json.put("taxno", "91310000607266569Q");
        json.put("userCode", "daxiang");
        json.put("code", "4001");
        json.put("message", "用户名密码异常");

        System.out.println("testUpdateErrorInfo request url:" + url + ",param:" + json.toJSONString());

        ResponseContent ret = HttpHelper.postJsonEntity(url, json.toString());
        System.out.println(ret.getContent());
    }

    @Test
    public void testSelectErrorTokenList() throws Exception{
        String url = host + "/client/selectErrorTokenList";
        String signStr = "AccessKeyID=%s&SignatureNonce=%s&TimeStamp=%s&Version=1.0";

        String signatureNonce = UUIDGenerator.generate();
        long time = System.currentTimeMillis();

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

        signStr = String.format(signStr,accessKeyID,signatureNonce, timeStamp);

        Map param = new HashMap<>();
        param.put("accessKeyID",accessKeyID);
        param.put("signatureNonce",signatureNonce);
        param.put("timeStamp",timeStamp);
        param.put("version","1.0");
        param.put("signature",getSign(signStr));
        param.put("pageId",0);
        System.out.println(JSON.toJSONString(param));
        String ret = HttpHelper.postWithJson(url,JSON.toJSONString(param));
        System.out.println(ret);
    }

    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;
    }
}
