/*
 * This file is part of Bitsquare.
 *
 * Bitsquare is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 *
 * Bitsquare is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
 */

package io.bitsquare.btc;

import com.runjva.sourceforge.jsocks.protocol.Socks5Proxy;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.PeerGroup;
import org.bitcoinj.kits.WalletAppKit;
import org.bitcoinj.net.BlockingClientManager;

import java.io.File;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.concurrent.TimeoutException;

public class WalletAppKitBitSquare extends WalletAppKit {
    private Socks5Proxy socks5Proxy;

    /**
     * Creates a new WalletAppKit, with a newly created {@link Context}. Files will be stored in the given directory.
     */
    public WalletAppKitBitSquare(NetworkParameters params, Socks5Proxy socks5Proxy, File directory, String filePrefix) {
        super(params, directory, filePrefix);
        this.socks5Proxy = socks5Proxy;
    }

    public Socks5Proxy getProxy() {
        return socks5Proxy;
    }

    protected PeerGroup createPeerGroup() throws TimeoutException {

        // no proxy case.
        if (socks5Proxy == null) {
            return super.createPeerGroup();
        }

        // proxy case.
        Proxy proxy = new Proxy(Proxy.Type.SOCKS,
                new InetSocketAddress(socks5Proxy.getInetAddress().getHostName(),
                        socks5Proxy.getPort()));

        int CONNECT_TIMEOUT_MSEC = 60 * 1000;  // same value used in bitcoinj.
        ProxySocketFactory proxySocketFactory = new ProxySocketFactory(proxy);
        BlockingClientManager mgr = new BlockingClientManager(proxySocketFactory);
        PeerGroup peerGroup = new PeerGroup(params, vChain, mgr);

        mgr.setConnectTimeoutMillis(CONNECT_TIMEOUT_MSEC);
        peerGroup.setConnectTimeoutMillis(CONNECT_TIMEOUT_MSEC);

        return peerGroup;
    }
}

@@ -722,16 +722,60 @@
 e.btc;%0A%0A
+import com.google.common.net.InetAddresses;%0A
 import c
@@ -1013,16 +1013,44 @@
 o.File;%0A
+import java.io.IOException;%0A
 import j
@@ -1095,24 +1095,48 @@
 .net.Proxy;%0A
+import java.net.Socket;%0A
 import java.
@@ -1811,16 +1811,48 @@
  == null
+ %7C%7C isLocalHostFullNodeRunning()
 ) %7B%0A    
@@ -1896,26 +1896,36 @@
 );%0A        %7D
-%0A%0A
+ else %7B%0A    
         // p
@@ -1927,32 +1927,36 @@
  // proxy case.%0A
+    
         Proxy pr
@@ -2005,16 +2005,20 @@
         
+ 
+   
 new Inet
@@ -2099,16 +2099,20 @@
         
+   
+ 
 socks5Pr
@@ -2125,24 +2125,28 @@
 tPort()));%0A%0A
+    
         int 
@@ -2216,24 +2216,28 @@
 nj.%0A        
+ 
+   
 ProxySocketF
@@ -2303,16 +2303,20 @@
         
+   
+ 
 Blocking
@@ -2390,16 +2390,20 @@
         
+    
 PeerGrou
@@ -2461,16 +2461,20 @@
         
+ 
+   
 mgr.setC
@@ -2524,16 +2524,20 @@
         
+   
+ 
 peerGrou
@@ -2590,24 +2590,28 @@
 );%0A%0A        
+    
 return peerG
@@ -2616,16 +2616,703 @@
 rGroup;%0A
+        %7D%0A    %7D%0A%0A    private boolean isLocalHostFullNodeRunning() %7B%0A        // We check first if a local node is running, if so we connect direct without proxy.%0A        // Borrowed form PeerGroup.maybeCheckForLocalhostPeer()%0A        try %7B%0A            Socket socket = new Socket();%0A            socket.connect(new InetSocketAddress(InetAddresses.forString(%22127.0.0.1%22), params.getPort()), PeerGroup.DEFAULT_CONNECT_TIMEOUT_MILLIS);%0A            try %7B%0A                socket.close();%0A            %7D catch (IOException ignore) %7B%0A            %7D%0A            return true;%0A        %7D catch (IOException e) %7B%0A            log.debug(%22Localhost peer not detected.%22);%0A            return false;%0A        %7D%0A
     %7D%0A%7D%0A

Connect to local Bitcoin Core node if it is running (ignore use Tor flag)