package edu.prlab.tyler.iotgateway.contract;


import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.core.methods.response.Web3ClientVersion;
import org.web3j.protocol.http.HttpService;
import org.web3j.quorum.Quorum;
import org.web3j.tx.ClientTransactionManager;
import org.web3j.tx.gas.ContractGasProvider;
import org.web3j.tx.gas.DefaultGasProvider;
import org.web3j.tx.gas.StaticGasProvider;

import java.math.BigInteger;

@FixMethodOrder(value = MethodSorters.NAME_ASCENDING)
public class ContractTester {

    private static final String rpcUrl = "http://localhost:22000";

//    private static final String privateKey = "7777777777777777777777777777777777777777777777777777777777777777";

    private static final ContractGasProvider DEFAULT_GAS_PROVIDER = new StaticGasProvider(
            BigInteger.ZERO,
            DefaultGasProvider.GAS_LIMIT
    );

    private String deviceContractAddress;
    private String gateWayContractAddress;

    @BeforeClass
    public static void setUp() {
    }

    @Test
    public void test1Contract() throws Exception {
//        Credentials credentials = Credentials.create(privateKey);
        Quorum quorum = Quorum.build(new HttpService(rpcUrl));

        Web3ClientVersion web3ClientVersion = quorum.web3ClientVersion().sendAsync().get();
        String clientVersion = web3ClientVersion.getWeb3ClientVersion();
        Assert.assertNotNull(clientVersion);

        String userAddress = quorum.ethAccounts().send().getAccounts().get(quorum.ethAccounts().send().getAccounts().size() - 1);
        Assert.assertNotNull(userAddress);

        ClientTransactionManager manager = new ClientTransactionManager(quorum, userAddress);

        //Device
        DeviceContract deviceContract = DeviceContract.deploy(quorum, manager, DEFAULT_GAS_PROVIDER).send();
        deviceContractAddress = deviceContract.getContractAddress();
        Assert.assertNotNull(deviceContract);
        Assert.assertNotNull(deviceContractAddress);

        TransactionReceipt receipt;
        receipt = deviceContract.setdeviceinfo("testdevice").send();
        Assert.assertNotNull(receipt);

        receipt = deviceContract.setpp("testpp").send();
        Assert.assertNotNull(receipt);

        String deviceInfo = deviceContract.deviceInfo().send();
        Assert.assertNotNull(deviceInfo);
        System.out.println(deviceInfo);

        String privacyPolicy = deviceContract.privacypolicy().send();
        Assert.assertNotNull(privacyPolicy);
        System.out.println(privacyPolicy);

        //TODO CANNOT BIND
        //Gateway
//        GatewayContract gatewayContract = GatewayContract.deploy(quorum, manager, DEFAULT_GAS_PROVIDER).send();
//        Assert.assertNotNull(gatewayContract);
//        gateWayContractAddress = gatewayContract.getContractAddress();
//        System.out.println("gatewayContract Address：" + gateWayContractAddress);
//
//        receipt = deviceContract.setBuyer(gatewayContract.getContractAddress()).send();
//        Assert.assertNotNull(receipt);
//
//        Quorum quorum1 = Quorum.build(new HttpService(rpcUrl));
//        String userAddress1 = quorum1.ethAccounts().send().getAccounts().get(quorum1.ethAccounts().send().getAccounts().size() - 1);
//        ClientTransactionManager manager1 = new ClientTransactionManager(quorum1, userAddress1);
//
//        System.out.println("deviceContractAddress" + deviceContractAddress);
//        DeviceContract deviceContract1 = DeviceContract.load(deviceContractAddress, quorum1, manager1, DEFAULT_GAS_PROVIDER);

//        System.out.println("deviceContractAddress" + gateWayContractAddress);
//        receipt = deviceContract1.bindReq(gateWayContractAddress).send();
//        Assert.assertNotNull(receipt);
//
//        String bindedString = deviceContract1.getBondDevice().send();
//        Assert.assertNotNull(bindedString);
//        System.out.println("binded : " + bindedString);

    }
}
