package com.fzm.mall.server.admin.block_chain.nft;

import cn.fzm.chain.simplesdk.base.RpcApi;
import cn.fzm.chain.simplesdk.constant.EvmTokenEnum;
import cn.fzm.chain.simplesdk.constant.RpcConfig;
import cn.fzm.chain.simplesdk.constant.TxStatusEnum;
import cn.fzm.chain.simplesdk.model.TxResult;
import org.springframework.stereotype.Component;

/**
 * @author lyz
 * @mail lyz@disanbo.com
 * @create 2021/2/26 10:44
 * @description
 */
public class NftEvm {

    private static String withhold = "1Ae6FfgdYJn6LLaqDoRjwga3j4TTmMq3t7";
    private static String withholdKey = "0xa4c49dcb35e0032dc8db5891dc81481943b68fc558ce93ce74d0382c1d104934";
    private static String title = "user.p.nftChain.";

    //String rpcUrl = "http://146.56.218.121:12091";
    //String rpcUrl = "http://172.22.20.71:8801";
    private static String rpcUrl = "http://172.22.20.100:8901";
    //String rpcUrl = "http://116.63.172.191:8801";
    //String rpcUrl = "http://172.16.101.133:8801";
    //String tokenManager = "1KEBcdmMPfUV2suAqgkyDpdm4brZbYmdfF"; 71
    private static String tokenManager = "1N2ABERwHgxGhebVw6fVSwaQ5uLAysmGEu";
    //String tokenManagerKey = "0x5d872407fb2769854dcca6203d70696feced7099c588a6d9146a122025585e41"; 71
    private static String tokenManagerKey = "0x4e92bda2477ded0e7c07a9e3acd2370de8d7401c68cc83ee8376806db3121e77";

    String publisher = "1NtQGuQjeWrnYvnpVBDfEDwYQ4pw4oKYDS";
    String to = "1JQumha8Jv2LEruJvtGoTnDc5YyLvQd7ku";
    long tokenId = 1628752532282L;
    String tokenInfo = "7b22617574686f72223a22e98791e5bab8222c227075626c6973686572223a2231384c5771736d66517a784155784237515070646359545755336e654e6e39634c53222c2268617368223a5b223830373538363330653736653539393062346465306539313362613432633565222c223830373538363330653736653539393062346465306539313362613432633565225d7d00";

    private static RpcApi ra = new RpcApi(rpcUrl, withhold, withholdKey, title);
    private static String code = "[encoded data, 12918 characters]";
    public static String abi = "[{\"inputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"operator\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"bool\",\"name\":\"approved\",\"type\":\"bool\"}],\"name\":\"ApprovalForAll\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"operator\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256[]\",\"name\":\"ids\",\"type\":\"uint256[]\"},{\"indexed\":false,\"internalType\":\"uint256[]\",\"name\":\"values\",\"type\":\"uint256[]\"}],\"name\":\"TransferBatch\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"operator\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"id\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"TransferSingle\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"internalType\":\"string\",\"name\":\"value\",\"type\":\"string\"},{\"indexed\":true,\"internalType\":\"uint256\",\"name\":\"id\",\"type\":\"uint256\"}],\"name\":\"URI\",\"type\":\"event\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"id\",\"type\":\"uint256\"}],\"name\":\"balanceOf\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address[]\",\"name\":\"accounts\",\"type\":\"address[]\"},{\"internalType\":\"uint256[]\",\"name\":\"ids\",\"type\":\"uint256[]\"}],\"name\":\"balanceOfBatch\",\"outputs\":[{\"internalType\":\"uint256[]\",\"name\":\"\",\"type\":\"uint256[]\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"tokenId\",\"type\":\"uint256\"}],\"name\":\"getTokenInfo\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"operator\",\"type\":\"address\"}],\"name\":\"isApprovedForAll\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"tokenId\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"mintToken\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"internalType\":\"uint256[]\",\"name\":\"ids\",\"type\":\"uint256[]\"},{\"internalType\":\"uint256[]\",\"name\":\"amounts\",\"type\":\"uint256[]\"},{\"internalType\":\"bytes\",\"name\":\"data\",\"type\":\"bytes\"}],\"name\":\"safeBatchTransferFrom\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"id\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"},{\"internalType\":\"bytes\",\"name\":\"data\",\"type\":\"bytes\"}],\"name\":\"safeTransferFrom\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"operator\",\"type\":\"address\"},{\"internalType\":\"bool\",\"name\":\"approved\",\"type\":\"bool\"}],\"name\":\"setApprovalForAll\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"tokenId\",\"type\":\"uint256\"},{\"internalType\":\"string\",\"name\":\"tokenInfo\",\"type\":\"string\"}],\"name\":\"setTokenInfo\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"bytes4\",\"name\":\"interfaceId\",\"type\":\"bytes4\"}],\"name\":\"supportsInterface\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"uri\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"}]";
    //String cName = "user.evm.0xf4403b125a0d51cecf955aa7bd67ad17f153efdfff188cd2d31e2d44f6094996"; 71
    private static String cName = "user.evm.0x72b80fbf7312e4ddefbfb235754f11915d10b88cefd0e0e96b4d7f66b99cce05";
    //String cAddr = "17S4bk6RvqNUmwfzkhJVAzjKBGtGYqtKas"; 71
    public static String cAddr = "1BzfFBFf99DRhqfunEXhPsZRDgPwUWjab6";


    //发行NFT,本次发行的是ERC1155
   /* public static String evmPublishToken(String publisher, long tokenId, long amount) {
        String hash = ra.evmPublishToken(abi, cAddr, EvmTokenEnum.ERC1155, 0L, publisher, null, tokenId, amount, "publish", true);
        System.err.println(hash);
        return hash;
    }*/

    //创建链上地址
    public static String evmCreateAddr(String label) {
        String addr = ra.walletCreateAccount(label);
        System.err.println(addr);
        return addr;
    }

    public static void main(String[] args) throws Exception {

//        String s = evmPublishToken("1GsU4gvB8rGCQjRn7HTtSaJmJ913UgcQvu", 1631778471259L, 1000L);
//        System.err.println(s);
        /*//String rpcUrl = "http://146.56.218.121:12091";
        String rpcUrl = "http://172.22.20.71:8801";
        //String rpcUrl = "http://116.63.172.191:8801";
        //String rpcUrl = "http://172.16.101.133:8801";
        String tokenManager = "1KEBcdmMPfUV2suAqgkyDpdm4brZbYmdfF";
        String tokenManagerKey = "0x5d872407fb2769854dcca6203d70696feced7099c588a6d9146a122025585e41";

        String publisher = "1NtQGuQjeWrnYvnpVBDfEDwYQ4pw4oKYDS";
        String to = "1JQumha8Jv2LEruJvtGoTnDc5YyLvQd7ku";
        long tokenId = 1628752532282L;
        String tokenInfo = "7b22617574686f72223a22e98791e5bab8222c227075626c6973686572223a2231384c5771736d66517a784155784237515070646359545755336e654e6e39634c53222c2268617368223a5b223830373538363330653736653539393062346465306539313362613432633565222c223830373538363330653736653539393062346465306539313362613432633565225d7d00";

        RpcApi ra = new RpcApi(rpcUrl);

        String code = "[encoded data, 12918 characters]";
        String abi = "[{\"inputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"operator\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"bool\",\"name\":\"approved\",\"type\":\"bool\"}],\"name\":\"ApprovalForAll\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"operator\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256[]\",\"name\":\"ids\",\"type\":\"uint256[]\"},{\"indexed\":false,\"internalType\":\"uint256[]\",\"name\":\"values\",\"type\":\"uint256[]\"}],\"name\":\"TransferBatch\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"operator\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"id\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"TransferSingle\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"internalType\":\"string\",\"name\":\"value\",\"type\":\"string\"},{\"indexed\":true,\"internalType\":\"uint256\",\"name\":\"id\",\"type\":\"uint256\"}],\"name\":\"URI\",\"type\":\"event\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"id\",\"type\":\"uint256\"}],\"name\":\"balanceOf\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address[]\",\"name\":\"accounts\",\"type\":\"address[]\"},{\"internalType\":\"uint256[]\",\"name\":\"ids\",\"type\":\"uint256[]\"}],\"name\":\"balanceOfBatch\",\"outputs\":[{\"internalType\":\"uint256[]\",\"name\":\"\",\"type\":\"uint256[]\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"tokenId\",\"type\":\"uint256\"}],\"name\":\"getTokenInfo\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"operator\",\"type\":\"address\"}],\"name\":\"isApprovedForAll\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"tokenId\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"mintToken\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"internalType\":\"uint256[]\",\"name\":\"ids\",\"type\":\"uint256[]\"},{\"internalType\":\"uint256[]\",\"name\":\"amounts\",\"type\":\"uint256[]\"},{\"internalType\":\"bytes\",\"name\":\"data\",\"type\":\"bytes\"}],\"name\":\"safeBatchTransferFrom\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"id\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"},{\"internalType\":\"bytes\",\"name\":\"data\",\"type\":\"bytes\"}],\"name\":\"safeTransferFrom\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"operator\",\"type\":\"address\"},{\"internalType\":\"bool\",\"name\":\"approved\",\"type\":\"bool\"}],\"name\":\"setApprovalForAll\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"tokenId\",\"type\":\"uint256\"},{\"internalType\":\"string\",\"name\":\"tokenInfo\",\"type\":\"string\"}],\"name\":\"setTokenInfo\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"bytes4\",\"name\":\"interfaceId\",\"type\":\"bytes4\"}],\"name\":\"supportsInterface\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"uri\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"}]";
        String cName = "user.evm.0xf4403b125a0d51cecf955aa7bd67ad17f153efdfff188cd2d31e2d44f6094996";
        String cAddr = "17S4bk6RvqNUmwfzkhJVAzjKBGtGYqtKas";

        //部署NFT合约，无需再次部署，已经部署
        String hash = ra.evmDeploy(code, abi, "MallNFT", RpcConfig.TX_EVM_DEPLOY_FEE, "MallNFT", null, tokenManager, tokenManagerKey, false);
        //发行NFT,本次发行的是ERC1155
        hash = ra.evmPublishToken(abi, cAddr, EvmTokenEnum.ERC1155, 0L, publisher, null, tokenId, 10000L, "publish", false);
        //设置NFT信息
        hash = ra.evmSetTokenInfo(abi, cAddr, 0L, tokenId, tokenInfo, publisher, null, false);
        //获取NFT信息
        hash = ra.evmGetTokenInfo(abi, cAddr, tokenId, publisher);
        //NFT转账
        hash = ra.evmTransfer(abi, cAddr, EvmTokenEnum.ERC1155, 0L, to, tokenId, 100L, "transfer", publisher, null, false);
        //查询NFT余额
        long balance = ra.evmBalance(abi, cAddr, EvmTokenEnum.ERC1155, publisher, tokenId);

//        String rawDeployTxHex = ra.evmGetDeployTxHex(code, abi, "MallNFT", RpcConfig.TX_EVM_DEPLOY_FEE,"MallNFT", null, tokenManager, tokenManagerKey, false);
//        System.out.println(ra.evmEstimateGasNew(rawDeployTxHex, tokenManager));
        System.out.println(hash);*/

        //1HBT7zyMYz7n9jz3JuYhEgFWuEtvpqxQaW

//        long[] tokenIds = {1, 2, 3};
//        long[] amounts = {1, 1, 1};

        //发行NFT,本次发行的是ERC1155
//        String hash = ra.evmPublishToken(abi, cAddr, EvmTokenEnum.ERC1155, 0L, "1HBT7zyMYz7n9jz3JuYhEgFWuEtvpqxQaW", "1HBT7zyMYz7n9jz3JuYhEgFWuEtvpqxQaW",null, 1L, 1L, "publish", true);
//        String hash = ra.evmPublishTokenBatch(abi, cAddr, 0L, "1HBT7zyMYz7n9jz3JuYhEgFWuEtvpqxQaW", "1HBT7zyMYz7n9jz3JuYhEgFWuEtvpqxQaW", null, tokenIds, amounts, "publish", true);
//        System.err.println(hash);
//        System.err.println(ra.cycleConfirmTxWithHash(hash, true, 1000).getStatus());




    }

}
