package nft.publish_nft;

import common.Constant;
import common.Resp;
import config.Config;
import exception.ZXChainException;
import util.HttpUtil;
import util.Tools;
import util.ZxlSDKUtil;

public class NftPublish {
    String pubKey;
    String platformPubKey;
    String author;
    String name;
    String url;
    String displayUrl;
    String hash;
    String desc;
    String flag;
    long publishCount;
    String seriesId;
    long seriesBeginIndex;
    int sellStatus;
    int sellCount;
    String operateId;
    String metaData;
    String signature;
    String platformSignature;
    String packageType;

    public NftPublish() {
    }

    public NftPublish(String pubKey, String author, String name, String url, String displayUrl,
                      String hash, String desc, String flag, long publishCount, String seriesId,
                      long seriesBeginIndex, int sellStatus, int sellCount, String operateId, String metaData, String packageType) {
        this.pubKey = pubKey;
        this.author = author;
        this.name = name;
        this.url = url;
        this.displayUrl = displayUrl;
        this.hash = hash;
        this.desc = desc;
        this.flag = flag;
        this.publishCount = publishCount;
        this.seriesId = seriesId;
        this.seriesBeginIndex = seriesBeginIndex;
        this.sellStatus = sellStatus;
        this.sellCount = sellCount;
        this.operateId = operateId;
        this.metaData = metaData;
        this.packageType = packageType;
    }

    public String getPubKey() {
        return pubKey;
    }

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

    public String getPlatformPubKey() {
        return platformPubKey;
    }

    public void setPlatformPubKey(String platformPubKey) {
        this.platformPubKey = platformPubKey;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getDisplayUrl() {
        return displayUrl;
    }

    public void setDisplayUrl(String displayUrl) {
        this.displayUrl = displayUrl;
    }

    public String getHash() {
        return hash;
    }

    public void setHash(String hash) {
        this.hash = hash;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    public long getPublishCount() {
        return publishCount;
    }

    public void setPublishCount(long publishCount) {
        this.publishCount = publishCount;
    }

    public String getSeriesId() {
        return seriesId;
    }

    public void setSeriesId(String seriesId) {
        this.seriesId = seriesId;
    }

    public long getSeriesBeginIndex() {
        return seriesBeginIndex;
    }

    public void setSeriesBeginIndex(long seriesBeginIndex) {
        this.seriesBeginIndex = seriesBeginIndex;
    }

    public int getSellStatus() {
        return sellStatus;
    }

    public void setSellStatus(int sellStatus) {
        this.sellStatus = sellStatus;
    }

    public int getSellCount() {
        return sellCount;
    }

    public void setSellCount(int sellCount) {
        this.sellCount = sellCount;
    }

    public String getOperateId() {
        return operateId;
    }

    public void setOperateId(String operateId) {
        this.operateId = operateId;
    }

    public String getMetaData() {
        return metaData;
    }

    public void setMetaData(String metaData) {
        this.metaData = metaData;
    }

    public String getSignature() {
        return signature;
    }

    public void setSignature(String signature) {
        this.signature = signature;
    }

    public String getPlatformSignature() {
        return platformSignature;
    }

    public void setPlatformSignature(String platformSignature) {
        this.platformSignature = platformSignature;
    }

    public String getPackageType() {
        return packageType;
    }

    public void setPackageType(String packageType) {
        this.packageType = packageType;
    }

    public String publish(String priKey) throws Exception {
        //todo 参数校验
        this.setPlatformPubKey(Config.platformPubKey);
        //构造参数
        String publishSignData = Tools.getSignData(
                this.platformPubKey,
                this.pubKey,
                "publish_nft",
                this.author,
                this.name,
                this.url,
                this.displayUrl,
                this.hash,
                this.desc,
                this.flag,
                this.publishCount,
                this.seriesId,
                this.seriesBeginIndex,
                this.sellStatus,
                this.sellCount,
                this.metaData,
                this.operateId);

        //发行人私钥签名
        this.setSignature(ZxlSDKUtil.signByPriKey(priKey, publishSignData));
        this.setPlatformSignature(ZxlSDKUtil.signByPriKey(Config.platformPrivateKey, publishSignData));
        //调用接口
        Resp resp = HttpUtil.apiPostJsonX(Config.baseUrl + "/api/v1/nft/publish", Tools.getJson(this), ZxlSDKUtil.generateApiSign());
        if (resp.getRetCode() != Constant.ZXL_OK) {
            throw new ZXChainException(resp.getRetMsg());
        }

        return resp.getData().get("taskId").toString();
    }
}
