package nft.transfer;

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

public class TryTransfer {
    String pubKey;
    String receiverAddr;
    String nftId;
    String operateId;
    String signature;

    public TryTransfer() {
    }

    public TryTransfer(String pubKey, String receiverAddr, String nftId, String operateId) {
        this.pubKey = pubKey;
        this.receiverAddr = receiverAddr;
        this.nftId = nftId;
        this.operateId = operateId;
    }

    public String tryTransfer(String priKey) throws Exception {
        String publishSignData = Tools.getSignData(pubKey, receiverAddr, "nft_transfer", nftId, operateId);
        this.setSignature(ZxlSDKUtil.signByPriKey(priKey, publishSignData));

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

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

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

    public String getReceiverAddr() {
        return receiverAddr;
    }

    public void setReceiverAddr(String receiverAddr) {
        this.receiverAddr = receiverAddr;
    }

    public String getNftId() {
        return nftId;
    }

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

    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 getPubKey() {
        return pubKey;
    }
}
