package nft.cos;

import common.Constant;
import common.Resp;
import config.Config;
import exception.ZXChainException;
import org.apache.commons.lang3.StringUtils;
import util.HttpUtil;
import util.Tools;
import util.ZxlSDKUtil;

public class CreateSecret {
    String seriesName;
    String timestamp;
    String pubKey;
    String pubSignedData;
    String userPubKey;
    String userSignedData;

    public CreateSecret() {
    }

    public CreateSecret(String seriesName, String userPubKey) {
        this.seriesName = seriesName;
        this.userPubKey = userPubKey;
    }

    public String getSeriesName() {
        return seriesName;
    }

    public void setSeriesName(String seriesName) {
        this.seriesName = seriesName;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public String getPubKey() {
        return pubKey;
    }

    public void setPubKey(String pubKey) {
        this.pubKey = pubKey;
    }

    public String getPubSignedData() {
        return pubSignedData;
    }

    public void setPubSignedData(String pubSignedData) {
        this.pubSignedData = pubSignedData;
    }

    public String getUserPubKey() {
        return userPubKey;
    }

    public void setUserPubKey(String userPubKey) {
        this.userPubKey = userPubKey;
    }

    public String getUserSignedData() {
        return userSignedData;
    }

    public void setUserSignedData(String userSignedData) {
        this.userSignedData = userSignedData;
    }

    public CreateSecretResp createSecret(String userPriKey) throws Exception {
        if (StringUtils.isEmpty(Config.platformPrivateKey)) {
            throw new IllegalArgumentException("平台私钥未配置");
        }

        if (StringUtils.isEmpty(Config.platformPubKey)) {
            throw new IllegalArgumentException("平台公钥未配置");
        }

        if (StringUtils.isEmpty(this.seriesName)) {
            throw new IllegalArgumentException("seriesName不能为空");
        }

        if (StringUtils.isEmpty(userPriKey)) {
            throw new IllegalArgumentException("userPriKey不能为空");
        }

        if (StringUtils.isEmpty(this.userPubKey)) {
            throw new IllegalArgumentException("userPubKey不能为空");
        }

        this.setTimestamp(System.currentTimeMillis()/1000+"");
        this.setPubKey(Config.platformPubKey);
        //系列声明人签名
        this.setUserSignedData(ZxlSDKUtil.signByPriKey(userPriKey, Tools.getSignData(this.timestamp, this.seriesName)));
        //平台方签名
        this.setPubSignedData(ZxlSDKUtil.signByPriKey(Config.platformPrivateKey, Tools.getSignData(this.timestamp, this.seriesName, this.userPubKey)));

        //调用接口
        Resp resp = HttpUtil.apiPostJsonX(Config.baseUrl + "/api/v1/user/upload/secret", Tools.getJson(this), ZxlSDKUtil.generateApiSign());
        if (resp.getRetCode() != Constant.ZXL_OK) {
            throw new ZXChainException(resp.getRetMsg());
        }

        return Tools.toJavaClass(resp.getData(), CreateSecretResp.class);
    }

    public static String getPlatformSignData(long timestamp, String seriesName, String userPubkey, String psk) throws Exception {
        String data = String.format("%d_%s_%s", timestamp, seriesName, userPubkey);
        return ZxlSDKUtil.signByPriKey(psk, data);
    }

    public static String getSignData(long timestamp, String seriesName, String usk) throws Exception {
        String data = String.format("%d_%s", timestamp, seriesName);
        return ZxlSDKUtil.signByPriKey(usk, data);
    }
}
