package nft.buy;

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

public class Buy {
    String nftId;
    int applyScore;
    String receiverPubKey;
    String pointReceiverAddr;
    String platformPubKey;
    long offerCount;
    String operateId;
    String signature;
    String platformSignature;

    public Buy() {
    }

    public Buy(String nftId, int applyScore, String receiverPubKey, String pointReceiverAddr, long offerCount, String operateId) {
        this.nftId = nftId;
        this.applyScore = applyScore;
        this.receiverPubKey = receiverPubKey;
        this.pointReceiverAddr = pointReceiverAddr;
        this.offerCount = offerCount;
        this.operateId = operateId;
    }

    public String buy(String priKey) throws Exception{
        this.setPlatformPubKey(Config.platformPubKey);
        String signData = Tools.getSignData(
                this.platformPubKey,
                this.receiverPubKey,
                this.pointReceiverAddr,
                this.applyScore,
                "buy_nft",
                this.nftId,
                this.offerCount,
                this.operateId
        );
        this.setSignature(ZxlSDKUtil.signByPriKey(priKey, signData));
        this.setPlatformSignature(ZxlSDKUtil.signByPriKey(Config.platformPrivateKey, signData));

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

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

    public String getNftId() {
        return nftId;
    }

    public void setNftId(String nftId) {
        this.nftId = nftId;
    }

    public int getApplyScore() {
        return applyScore;
    }

    public void setApplyScore(int applyScore) {
        this.applyScore = applyScore;
    }

    public String getReceiverPubKey() {
        return receiverPubKey;
    }

    public void setReceiverPubKey(String receiverPubKey) {
        this.receiverPubKey = receiverPubKey;
    }

    public String getPointReceiverAddr() {
        return pointReceiverAddr;
    }

    public void setPointReceiverAddr(String pointReceiverAddr) {
        this.pointReceiverAddr = pointReceiverAddr;
    }

    public String getPlatformPubKey() {
        return platformPubKey;
    }

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

    public long getOfferCount() {
        return offerCount;
    }

    public void setOfferCount(long offerCount) {
        this.offerCount = offerCount;
    }

    public String getOperateId() {
        return operateId;
    }

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

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