package com.tax.sign.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yxcheng.token.utils.HttpHelper;
import com.yxcheng.token.utils.MD5Util;
import com.yxcheng.token.utils.ResponseContent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class SignUtil {

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

    public static String getServerDate(String url, String syncTimeFrom){
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHH");
        String systemTime = format.format(new Date());
        String timeStamp = null;
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        try {
            if(syncTimeFrom.equals("0")) {
                url += ("?dd=08&systemTime="+systemTime + "&taxno=911101085622223311" + "&status=0&funType=01" );
                ResponseContent responseContent = HttpHelper.getUrlRespContent(url);
                String content = responseContent.getContent();
                logger.info("The sign time url is:" + url + ", the content is :" + content);
                JSONObject data = JSON.parseObject(content);
                timeStamp = data.getString("timestamp");
            } else {
                url += "?cert=911101085622223311&funType=01";
                ResponseContent responseContent = HttpHelper.getUrlRespContent(url);
                String ret = responseContent.getContent();
                if(ret.contains("null(")){
                    ret =ret.replace("null(","");
                    ret =   ret.substring(0,ret.length() - 1);
                }
                logger.info("The sign time url is:" + url + ", the content is :" + ret);
                if(ret!=null){
                    net.sf.json.JSONObject jo = net.sf.json.JSONObject.fromObject(ret);
                    if(jo.containsKey("ts")){
                        timeStamp = jo.getString("ts");
                    }

                }
            }
            return timeStamp;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("The sign time url is:" + url + ", the error is :" , e.getMessage());
        }
        return null;
    }

    //tn,ts,serverR
    public static String checkSign(String a, String b, String e) {
        String c2 = MD5Util.MD5Encode(EncryptCommonUtil.getXX(b) + e);
        c2 = MD5Util
                .MD5Encode(EncryptCommonUtil.gen(c2, EncryptCommonUtil.getYY(a)) + Base64Util
                        .encode((EncryptCommonUtil.getYY(EncryptCommonUtil.gen(b, a))).getBytes())
                        + EncryptCommonUtil.gen(Base64Util.encode(c2.getBytes()), EncryptCommonUtil.getXX(c2.length() + e)) + e)
                .toUpperCase();
        c2 = EncryptCommonUtil.encode(EncryptCommonUtil
                .encode(EncryptCommonUtil.encode(EncryptCommonUtil.gen(a, c2)) + EncryptCommonUtil.gen(b, c2))
                + EncryptCommonUtil.gen(EncryptCommonUtil.getXX(MD5Util.MD5Encode(EncryptCommonUtil.gen(a, c2))),
                EncryptCommonUtil.getXX(MD5Util.MD5Encode(EncryptCommonUtil.getXX(b)))))
                .toUpperCase();
        c2 = MD5Util.MD5Encode(c2);
        return c2;
    }

    public static String getSuperSign(String tn, String ts, String serverR) {
        return MD5Util.MD5Encode(MD5Util.MD5Encode(MD5Util.MD5Encode(tn) + ts + MD5Util.MD5Encode(ts) + serverR));
    }

    /**
     * @param a  cert
     * @param b  token
     * @param c  ts
     * @return
     */
    public static String getCertSign(String a, String b, String c) {
        return MD5Util.MD5Encode(EncryptCommonUtil
                .gen(EncryptCommonUtil.gen(a.toUpperCase(), EncryptCommonUtil.getYY(MD5Util.MD5Encode(b))),
                        b.toUpperCase() + EncryptCommonUtil.encode(a + MD5Util.MD5Encode(a + b)))
                .toUpperCase());
    }

    /**
     * 用于新版本普通签名
     * @param cert
     * @param token
     * @param ts
     * @return
     */
    public static String getPubSign(String cert, String token, String ts) {
        String txn = getCertSign(cert, token, ts);
        return  checkSign(txn, ts, token);
    }

    /**
     * 用于新版本超级签名
     * @param cert
     * @param token
     * @param ts
     * @return
     */
    public static String getSuperPubSign(String cert, String token, String ts) {
        String txn = getCertSign(cert, token, ts);
        return  getSuperSign(txn, ts, token);
    }


    public static void main(String[] args) {
        String serverDate = SignUtil.getServerDate("http://39.107.59.82:1111/commons-gencrypt-test/client/jscode.do", "0");
        System.out.println(serverDate);
    }


}
