package com.liquidnet.service.zxlnft;

import com.liquidnet.common.third.zxlnft.biz.ZxlnftBiz;
import com.liquidnet.common.third.zxlnft.constant.ZxlErrorEnum;
import com.liquidnet.common.third.zxlnft.constant.ZxlnftEnum;
import com.liquidnet.common.third.zxlnft.dto.*;
import com.liquidnet.common.third.zxlnft.dto.wallet.DeriveKeyPairReq;
import com.liquidnet.common.third.zxlnft.dto.wallet.DeriveKeyPairResp;
import com.liquidnet.common.third.zxlnft.exception.ZxlNftException;
import com.liquidnet.common.third.zxlnft.util.ZxlWalletSdkUtil;
import com.liquidnet.common.third.zxlnft.util.ZxlnftSdkUtil;
import com.liquidnet.commons.lang.util.BASE64Util;
import com.liquidnet.commons.lang.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.UnsupportedEncodingException;

/**
 * @author AnJiabin <anjiabin@zhengzai.tv>
 * @version V1.0
 * @Description: TODO
 * @class: TestZxlnftService
 * @Package com.liquidnet.service.zxlnft
 * @Copyright: LightNet @ Copyright (c) 2021
 * @date 2022/3/3 16:55
 */
@Slf4j
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class TestZxlnftService {
    @Autowired
    private ZxlnftSdkUtil zxlnftSdkUtil;

    @Autowired
    private ZxlWalletSdkUtil zxlWalletSdkUtil;

    @Autowired
    private ZxlnftBiz zxlnftBiz;
    /**
     * 1、NFT平台在至信链的注册、审核
     */
    @Test
    public void platformRegister(){

//        1.1.2调用下发查询用户信息验证码接口
//        1.1.3调用查询用户信息接口
//        1.1.4调用下发用户信息查询验证码接口
//        1.1.5调用NFT平台自身地址绑定接口
//        1.1.6调用绑定状态批量查询接口

    }

    /**
     * 2、自然人用户通过NFT平台注册绑定
     */
    @Test
    public void personRegisterBind(){
        String userId = "310508344264376327445470";
        String userName = "安家宾";
        String mobile = "13811314082";
        String idCardType = ZxlnftEnum.CardTypeEnum.ID_CARD.getCode();
        String idCard = "142323198302062216";

//        String userName = "周焕";
//        String mobile = "18548596019";
//        String idCardType = ZxlnftEnum.CardTypeEnum.ID_CARD.getCode();
//        String idCard = "150422199805206019";

        String mnemonic = "stuff name goat health siren dumb gorilla antique board tenant buffalo present"; //安家宾
//        String mnemonic = "economy cost balance weapon flight also nut biology very sun slight about"; //周焕
        Long index = 0L;
        String userIdentification = null;
        String address = null;
        String userPubKey = null;
        String userPriKey = null;

//        try{
//            //生成助记词
//            CreateMnemonicReq req = CreateMnemonicReq.getNew();
//            CreateMnemonicResp createMnemonicResp = zxlWalletSdkUtil.createMnemonic(req);
//            mnemonic = createMnemonicResp.getMnemonic();
//        }catch(Exception e){
//            throw new ZxlNftException(ZxlErrorEnum.FAILURE.getCode(),"生成助记词失败！");
//        }

        /**
         * todo 把助记词进行redis存储  key=userID  mnemonic/index/userIdentification/address
         */

        if(StringUtil.isNotEmpty(mnemonic)){
            //生成公私钥
            DeriveKeyPairReq deriveKeyPairReq = DeriveKeyPairReq.getNew();
            deriveKeyPairReq.setMnemonic(mnemonic);
//        deriveKeyPairReq.setMnemonic(createMnemonicResp.getMnemonic());
            deriveKeyPairReq.setIndex(index);
            try{
                DeriveKeyPairResp deriveKeyPairResp = zxlWalletSdkUtil.deriveKeyPair(deriveKeyPairReq);
                if(deriveKeyPairResp.getErr().equals("")) throw new Exception("生成公私钥失败！");
                userPubKey = BASE64Util.encoded(deriveKeyPairResp.getPubKey());
                userPriKey = BASE64Util.encoded(deriveKeyPairResp.getPriKey());
            }catch(Exception e){
                throw new ZxlNftException(ZxlErrorEnum.FAILURE.getCode(),e.getMessage());
            }
        }

        //1.2.1调用自然人注册实名(使用NFT平台签名)接口
        Nft003RegisterPersonPlatformReqDto nft003ReqDto = Nft003RegisterPersonPlatformReqDto.getNew();
        nft003ReqDto.setPersonName(userName);
//        reqDto.setEmail("");
        nft003ReqDto.setMobile(mobile);
        nft003ReqDto.setIdCard(idCard);

        nft003ReqDto.setCardType(Integer.valueOf(idCardType));
        ZxlnftResponseDto<Nft003RegisterPersonPlatformRespDto> nft003Resp = zxlnftSdkUtil.nft003RegisterPersonPlatform(nft003ReqDto);

        if(nft003Resp.isSuccess()){
            userIdentification = nft003Resp.getData().getUserIdentification();
        }

        //
        if(StringUtil.isNotEmpty(userPubKey)&&StringUtil.isNotEmpty(userPriKey)&&StringUtil.isNotEmpty(userIdentification)){
            //1.2.2调用授信平台NFT地址绑定接口
            Nft014IdentityBindSubmitByTrustedReqDto nft004ReqDto = Nft014IdentityBindSubmitByTrustedReqDto.getNew();

            try {
                nft004ReqDto.setUserPubKey(BASE64Util.decode(userPubKey));
                nft004ReqDto.setUserIdentification(nft003Resp.getData().getUserIdentification());

                String signature = zxlnftBiz.createSign(BASE64Util.decode(userPriKey),nft004ReqDto.getUserIdentification());
                nft004ReqDto.setUserSignData(signature);
            } catch (UnsupportedEncodingException e) {
                log.error("公私钥解密错误！");
            }

            ZxlnftResponseDto<Nft014IdentityBindSubmitByTrustedRespDto> nft004Resp = zxlnftSdkUtil.nft014IdentityBindSubmitByTrusted(nft004ReqDto);

            if(nft004Resp.isSuccess()){
                //        1.2.3调用绑定状态批量查询
                Nft016IdentityBindQueryReqDto nft016ReqDto = Nft016IdentityBindQueryReqDto.getNew();
                nft016ReqDto.setAddressList(nft004Resp.getData().getAddress());
                ZxlnftResponseDto<Nft016IdentityBindQueryRespDto> nft016Resp = zxlnftSdkUtil.nft016IdentityBindQuery(nft016ReqDto);

                if(nft016Resp.isSuccess()){
                    log.info("返回结果：{}",nft016Resp.toJson());
                }
            }else{
                log.info("返回结果：{}",nft004Resp.toJson());
            }
        }
    }

    /**
     * 3、素材上传操作
     */
    @Test
    public void nftUpload(){
//        1.4.1调用图片内容检测接口
//        1.4.2调用生成素材上传临时密钥接口
//        1.4.3调用sdk接口-上传cos接口
//        1.4.4调用查询素材地址接口 -- 非必需
    }

    /**
     * 4、发行NFT
     */
    @Test
    public void nftPublish(){
//        3.1.1调用系列声明
//        3.1.2调用NFT发行接口
//        3.1.3调用NFT查询接口
//        3.1.4查询 NFT发行结果
    }

    /**
     * 5、购买NFT
     */
    @Test
    public void nftBuy(){
//        3.2.1调用积分申请接口
//        3.2.2调用购买NFT接口
//        3.2.3调用NFT查询接口
//        3.2.4查询NFT购买支付结果
    }

    /**
     * 6、查询NFT相关信息
     */
    @Test
    public void nftQuery(){

    }

}
