package it.enea.cruise.service.blockchain;

import org.springframework.stereotype.Service;
/*import org.web3j.abi.FunctionEncoder;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.Utf8String;
import org.web3j.abi.datatypes.Uint;
import org.web3j.contracts.eip20.generated.ERC20;
import org.web3j.contracts.eip20.generated.*;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameter;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.protocol.core.methods.request.Transaction;
import org.web3j.protocol.core.methods.response.EthAccounts;
import org.web3j.protocol.core.methods.response.EthBlockNumber;
import org.web3j.protocol.core.methods.response.EthGetBalance;
import org.web3j.protocol.core.methods.response.EthGetTransactionCount;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.ReadonlyTransactionManager;
import org.web3j.tx.TransactionManager;
import org.web3j.tx.gas.DefaultGasProvider;

import it.enea.cruise.SC.Community_sol;

import org.web3j.tx.gas.ContractGasProvider;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;*/

@Service
public class Web3Service {
	/*@Value("${BCNetworkRPC.address}")
	private String bCNetworkRPCAdd;
	
	@Value("${BCNetworkRPC.port}")
	private String bCNetworkRPCPort;

    private Web3j web3j = Web3j.build(new HttpService("http://localhost:8545"));
      
    //Create and Init the default Web3J connection
    public void customInit(String provider) {
        this.web3j = Web3j.build(new HttpService(provider));
    }

    //Convert to and from supplied contract ABI bytecode
    public static String toBinary(String bytecode) {
        return bytecode.replaceFirst("^0x", "");
    }

    public static String toByteCode(String binary) {
        return "0x" + binary;
    }

    public EthBlockNumber getBlockNumber() {
        EthBlockNumber result = new EthBlockNumber();
        customInit(bCNetworkRPCAdd + ":" + bCNetworkRPCPort);
        try {
            result = this.web3j.ethBlockNumber().sendAsync().get();
        } catch (Exception ex) {
            System.out.println("Generic exception");
        }
        return result;
    }

    public EthAccounts getEthAccounts() {
        EthAccounts result = new EthAccounts();
        customInit(bCNetworkRPCAdd + ":" + bCNetworkRPCPort);
        try {
             result = this.web3j.ethAccounts().sendAsync().get();
        } catch (Exception ex) {
            System.out.println("Generic exception");
        }
        return result;
    }

    public EthGetTransactionCount getTransactionCount() {
    	 customInit(bCNetworkRPCAdd + ":" + bCNetworkRPCPort);
        EthGetTransactionCount result = new EthGetTransactionCount();
        try {
        	
             result = this.web3j.ethGetTransactionCount("0x911C991E68e8d48c8B86a82280977927B3D816Db", DefaultBlockParameter.valueOf("latest")).sendAsync().get();
        } catch (Exception ex) {
            System.out.println("Generic exception " + ex.getLocalizedMessage());
        }
        return result;
    }

    public EthGetBalance getEthBalance() {
    	 customInit(bCNetworkRPCAdd + ":" + bCNetworkRPCPort);
        EthGetBalance result = new EthGetBalance();
        try {
            result = this.web3j.ethGetBalance("0x911C991E68e8d48c8B86a82280977927B3D816Db", DefaultBlockParameter.valueOf("latest")).sendAsync().get();
        } catch (Exception ex) {
            System.out.println("Generic exception" + ex.getLocalizedMessage());
        }
        return result;
    }
    
    public BigInteger getTKBalance(String contractAddress, String walletAddress, Integer communityId) {
    	 customInit(bCNetworkRPCAdd + ":" + bCNetworkRPCPort);
    	final String FROM_ADDRESS = walletAddress; //"0x0000000000000000000000000000000000000000";
    	final ContractGasProvider contractGasProvider = new DefaultGasProvider();
    	final TransactionManager readOnlyTransactionManager = new ReadonlyTransactionManager(web3j, FROM_ADDRESS);
        	
        BigInteger result = null;
        try {
        	Community_sol tk = Community_sol.load(contractAddress, web3j, readOnlyTransactionManager, contractGasProvider);
           	result = tk.balanceOf(communityId, walletAddress).send();  
           	System.out.println(result.toString());
         } catch (Exception ex) {
            System.out.println("Generic exception " + ex.getLocalizedMessage());
        }
        return result;
    }

    @Async
    public String sendTx() {
        String transactionHash = "";

        try {
            List inputParams = new ArrayList();
            List outputParams = new ArrayList();
            Function function = new Function("fuctionName", inputParams, outputParams);

            String encodedFunction = FunctionEncoder.encode(function);

            BigInteger nonce = BigInteger.valueOf(100);
            BigInteger gasprice = BigInteger.valueOf(100);
            BigInteger gaslimit = BigInteger.valueOf(100);

            Transaction transaction = Transaction.createFunctionCallTransaction("FROM_ADDRESS", nonce, gasprice, gaslimit, "TO_ADDRESS", encodedFunction);

            org.web3j.protocol.core.methods.response.EthSendTransaction transactionResponse = web3j.ethSendTransaction(transaction).sendAsync().get();
            transactionHash = transactionResponse.getTransactionHash();

        } catch (Exception ex) {
            System.out.println("please supply real data");
            return "please supply real data";
        }

        return transactionHash;
    }*/
}