package nft.point;

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

public class Transfer {
    String platformPubKey;
    String fromPubKey;
    String toAddr;
    long count;
    String operateId;
    String signature;
    String platformSignature;

    public Transfer() {
    }

    public Transfer(String fromPubKey, String toAddr, long count, String operateId) {
        this.fromPubKey = fromPubKey;
        this.toAddr = toAddr;
        this.count = count;
        this.operateId = operateId;
    }

    public String transfer(String priKey) throws Exception {
        this.setPlatformPubKey(Config.platformPubKey);
        String signData = Tools.getSignData(
                this.platformPubKey,
                this.fromPubKey,
                this.toAddr,
                "transfer_point",
                this.count,
                this.operateId
        );
        this.setSignature(ZxlSDKUtil.signByPriKey(priKey, signData));
        this.setPlatformSignature(ZxlSDKUtil.signByPriKey(Config.platformPrivateKey, signData));

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

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

    public String getPlatformPubKey() {
        return platformPubKey;
    }

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

    public String getFromPubKey() {
        return fromPubKey;
    }

    public void setFromPubKey(String fromPubKey) {
        this.fromPubKey = fromPubKey;
    }

    public String getToAddr() {
        return toAddr;
    }

    public void setToAddr(String toAddr) {
        this.toAddr = toAddr;
    }

    public long getCount() {
        return count;
    }

    public void setCount(long count) {
        this.count = count;
    }

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