package com.tax.token.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tax.token.constants.TokenConstants;
import com.tax.token.model.TkServer;
import nuonuo.open.sdk.NNOpenSDK;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.UUID;

public class NUONUOUtil {

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

    public static JSONObject getClientHello(String uuidFlag, String taxNo){
        String method = "nuonuo.ElectronInvoice.clientHello";
        String content ="{\"taxNo\":\""+taxNo+"\"}";
        JSONObject result = invoke(uuidFlag, method,content);
        logger.info(uuidFlag + " taxno is " + taxNo + "NUO.getClientHello-------"+result);
        return result;
    }

    public static JSONObject getClientAuth(String uuidFlag, String taxNo,String clientHello){
        String method = "nuonuo.ElectronInvoice.clientAuth";
        String content ="{\"taxNo\":\""+taxNo+"\",\"helloStr\":\""+clientHello+"\"}";
        JSONObject result = invoke(uuidFlag, method,content);
        logger.info(uuidFlag + " taxno is " + taxNo + "NUO.getClientAuth-------"+result);
        return result;
    }

    public static JSONObject invoke(String uuidFlag, String method,String content){
        NNOpenSDK sdk = NNOpenSDK.getIntance();
        String senid = UUID.randomUUID().toString().replace("-", "");
        String result = sdk.sendPostSyncRequest(TokenConstants.NUONUO_URL, senid, TokenConstants.NUONUO_APPKEY,
                TokenConstants.NUONUO_APPSECRET, TokenConstants.NUONUO_TOKEN, TokenConstants.NUONUO_TAXNUM,
                method, content);
        logger.info(uuidFlag + " taxno is " + content + " NUO.getClientHello-------"+result);
        JSONObject jsonObject = JSON.parseObject(result);
        return jsonObject;
    }


    public static JSONObject getClientHello(String uuidFlag, String taxNo, TkServer tkServer){
        String method = "nuonuo.ElectronInvoice.clientHello";
        String content ="{\"taxNo\":\""+taxNo+"\"}";
        JSONObject result = invoke(uuidFlag, taxNo, method,content,tkServer);
        logger.info(uuidFlag + " taxno is " + taxNo + " NUO.getClientHello-------"+result);
        return result;
    }

    public static JSONObject getClientAuth(String uuidFlag, String taxNo,String clientHello, TkServer tkServer){
        String method = "nuonuo.ElectronInvoice.clientAuth";
        String content ="{\"taxNo\":\""+taxNo+"\",\"helloStr\":\""+clientHello+"\"}";
        JSONObject result = invoke(uuidFlag, taxNo, method,content,tkServer);
        logger.info(uuidFlag + " taxno is " + taxNo + " NUO.getClientAuth-------"+result);
        return result;
    }

    public static JSONObject invoke(String uuidFlag, String taxNo, String method,String content, TkServer tkServer){
        NNOpenSDK sdk = NNOpenSDK.getIntance();
        String senid = UUID.randomUUID().toString().replace("-", "");
        String[]strs = tkServer.getSignUrl().split(",");
        String taxNum = strs[0];
        String token = strs[1];
        String result = sdk.sendPostSyncRequest(tkServer.getReqUrl(), senid, tkServer.getUserName(),
                tkServer.getPassword(), token, taxNum,
                method, content);
        logger.info(uuidFlag + " taxno is " + taxNo + " NUO.getClientHello-------"+result);
        JSONObject jsonObject = JSON.parseObject(result);
        return jsonObject;
    }


    public static void main(String[] args) {
        String url = "https://fpdk.ningbo.chinatax.gov.cn/";
        String version = "4.0.15";
        String taxNo = "9133020157368346XG";
        String zoneCode = "3302";
        String uuidFlag = "test";
        JSONObject chJson = NUONUOUtil.getClientHello(uuidFlag, taxNo);
        String helloStr =  chJson.getJSONObject("result").getString("helloStr");
        JSONObject serverAuthJson = NationUtil.firstLogin(uuidFlag, taxNo,helloStr,zoneCode);
//        JSONObject serverAuthJson = NationUtil.serverAuth(url, helloStr, version);
//
        JSONObject clientAuthJson = NUONUOUtil.getClientAuth(uuidFlag, taxNo, serverAuthJson.getString("key2"));

//        JSONObject signJson = NationUtil.sign(taxNo, zoneCode, serverAuthJson.getString("key3"));
        String authStr = clientAuthJson.getJSONObject("result").getString("authStr");
        NationUtil.secondLogin(uuidFlag, taxNo,authStr,zoneCode,serverAuthJson.getString("key3"),null);
        //        String token = NationUtil.getToken(url,taxNo,authStr,serverAuthJson.getString("key3"),version,signJson.getString("sign"),signJson.getString("ts"));
//        logger.info("token:" + token);
    }
}
