package ev;

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

public class EvSave {
    String publicKey;
    String hash;
    String sign;
    String extendInfo;

    public EvSave() {
    }

    public EvSave(String publicKey, String hash, String extendInfo) {
        this.publicKey = publicKey;
        this.hash = hash;
        this.extendInfo = extendInfo;
    }

    public EvSaveResp save(String priKey) throws Exception {
        String signData = Tools.getSignData(this.publicKey, this.hash, this.extendInfo);
        this.setSign(ZxlSDKUtil.signByPriKey(priKey, signData));

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

        return resp.getData(EvSaveResp.class);
    }

    public String getPublicKey() {
        return publicKey;
    }

    public void setPublicKey(String publicKey) {
        this.publicKey = publicKey;
    }

    public String getHash() {
        return hash;
    }

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

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getExtendInfo() {
        return extendInfo;
    }

    public void setExtendInfo(String extendInfo) {
        this.extendInfo = extendInfo;
    }

    @Override
    public String toString() {
        return "EvSaveReq{" +
                "publicKey='" + publicKey + '\'' +
                ", hash='" + hash + '\'' +
                ", sign='" + sign + '\'' +
                ", extendInfo='" + extendInfo + '\'' +
                '}';
    }
}
