package nft.series_claim;

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

public class SeriesClaim {
    String pubKey;
    String platformPubKey;
    String seriesName;
    long totalCount;
    // 幂等id
    String operateId;
    String coverUrl;
    String desc;
    boolean seriesBeginFromZero;
    String signature;
    String platformSignature;

    public SeriesClaim() {
    }

    public SeriesClaim(String pubKey, String seriesName, long totalCount, String operateId, String coverUrl, String desc, boolean seriesBeginFromZero) {
        this.pubKey = pubKey;
        this.seriesName = seriesName;
        this.totalCount = totalCount;
        this.operateId = operateId;
        this.coverUrl = coverUrl;
        this.desc = desc;
        this.seriesBeginFromZero = seriesBeginFromZero;
    }

    public String claim(String userPriKey) throws Exception {
        //todo 校验参数
        this.setPlatformPubKey(Config.platformPubKey);
        String signData = Tools.getSignData(
                this.platformPubKey,
                this.pubKey,
                "series_claim",
                this.seriesName,
                this.totalCount,
                this.coverUrl,
                this.desc,
                0,
                this.seriesBeginFromZero,
                this.operateId
        );
        this.setSignature(ZxlSDKUtil.signByPriKey(userPriKey, signData));
        this.setPlatformSignature(ZxlSDKUtil.signByPriKey(Config.platformPrivateKey, signData));

        Resp resp = HttpUtil.apiPostJsonX(Config.baseUrl + "/api/v1/nft/series/claim", Tools.getJson(this), ZxlSDKUtil.generateApiSign());
        if (resp.getRetCode() != Constant.ZXL_OK) {
            throw new ZXChainException(resp.getRetMsg());
        }

        return resp.getData().getString("taskId");
    }

    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 getSeriesName() {
        return seriesName;
    }

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

    public long getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(long totalCount) {
        this.totalCount = totalCount;
    }

    public String getOperateId() {
        return operateId;
    }

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

    public String getCoverUrl() {
        return coverUrl;
    }

    public void setCoverUrl(String coverUrl) {
        this.coverUrl = coverUrl;
    }

    public String getDesc() {
        return desc;
    }

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

    public boolean isSeriesBeginFromZero() {
        return seriesBeginFromZero;
    }

    public void setSeriesBeginFromZero(boolean seriesBeginFromZero) {
        this.seriesBeginFromZero = seriesBeginFromZero;
    }

    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;
    }
}
