package com.tax.token.thirdService.impl;

import com.alibaba.fastjson.JSON;
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.yxcheng.token.utils.*;
import org.apache.http.HttpException;

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

public class GetTokenFromYunzf implements TokenThirdServiceI {
    @Override
    public Map<String, Object> invoke(String uuIdFlag, TkToken tkToken, TkZoneInfo tkZoneInfo, TkServer tkServer, boolean isFirst) throws IOException, HttpException{
        Map<String,Object>map = new HashMap<>();
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        Map<String, Object> contentMap = new HashMap<String, Object>();
        String reqId = UUIDGenerator.generate();
        long timestamp = new Date().getTime();
        String signStr = String.format("appId=%s&reqId=%s&timestamp=%s",tkServer.getUserName(),reqId,timestamp);
        String sign = this.getSign(signStr,tkServer.getPassword());


        paramsMap.put("appId",tkServer.getUserName());
        paramsMap.put("reqId",reqId);
        paramsMap.put("timestamp",String.valueOf(timestamp));
        paramsMap.put("sign",sign);
        contentMap.put("taxNo",tkToken.getTaxno());
        paramsMap.put("content",contentMap);
        System.out.println(paramsMap);

        String retStr = HttpHelper.postWithJson(tkServer.getReqUrl(), JSON.toJSONString(paramsMap));
        if(retStr==null){
            map.put("code","-1");
            map.put("message","获取信息失败");
            return map;
        }
        Map<String, Object> retMap = JSONHelper.parserToMap(retStr);
        //{result={status=true, token=2~0~0~~1~0~0~0~0~0~1~1c1c249c-c484-4cd1-b99c-46dfb1687d8b}, code=0, cause=null, message=成功}

        map.put("code", ObjectUtil.fixNull(retMap.get("code")));
        map.put("message", ObjectUtil.fixNull(retMap.get("message")));
        if("0".equals(retMap.get("code"))){
            Map data = (Map) retMap.get("result");
            if(data !=null && data.containsKey("token")){
                map.put("token",data.get("token"));
            }
        }

        return map;
    }

    public static void main(String[] args) throws Exception {
        String appId = "10000002";
        String appSecret = "65b0841b";

        TkToken tokenEntity = new TkToken();
        TkZoneInfo zoneInfo=new TkZoneInfo(); TkServer tkServer=new TkServer();boolean isFirst = true;
        tokenEntity.setTaxno("91429006MA492ACN52");
        tokenEntity.setIpServerNo("T00005");
        tkServer.setReqUrl("http://106.75.104.100:8111/invoice/getToken");
        tkServer.setUserName(appId);
        tkServer.setPassword(appSecret);
        GetTokenFromYunzf local = new GetTokenFromYunzf();

        Map<String, Object> map = local.invoke("aaa",tokenEntity, zoneInfo, tkServer, isFirst);

        System.out.println(map);



//        Map map1 = (Map) map.get("result");
//        System.out.println(map1.get("token"));



    }


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


}
