/*
 * This file is part of Bisq.
 *
 * Bisq 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.
 *
 * Bisq 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 Bisq. If not, see <http://www.gnu.org/licenses/>.
 */

package bisq.core.network.p2p.seed;

import bisq.network.p2p.NodeAddress;
import bisq.network.p2p.seed.SeedNodeRepository;

import bisq.common.config.BaseCurrencyNetwork;
import bisq.common.config.Config;

import javax.inject.Inject;
import javax.inject.Singleton;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;

// If a new BaseCurrencyNetwork type gets added we need to add the resource file for it as well!
@Slf4j
@Singleton
public class DefaultSeedNodeRepository implements SeedNodeRepository {
    //TODO add support for localhost addresses
    private static final Pattern pattern = Pattern.compile("^([a-z0-9]+\\.onion:\\d+)");
    private static final String ENDING = ".seednodes";
    private final Collection<NodeAddress> cache = new HashSet<>();
    private final Config config;

    @Inject
    public DefaultSeedNodeRepository(Config config) {
        this.config = config;
    }

    private void reload() {
        try {
            // see if there are any seed nodes configured manually
            if (!config.seedNodes.isEmpty()) {
                cache.clear();
                config.seedNodes.forEach(s -> cache.add(new NodeAddress(s)));

                return;
            }

            cache.clear();
            List<NodeAddress> result = getSeedNodeAddressesFromPropertyFile(config.baseCurrencyNetwork);
            cache.addAll(result);

            // filter
            cache.removeAll(
                    config.bannedSeedNodes.stream()
                            .filter(n -> !n.isEmpty())
                            .map(NodeAddress::new)
                            .collect(Collectors.toSet()));

            log.info("Seed nodes: {}", cache);
        } catch (Throwable t) {
            log.error("exception in DefaultSeedNodeRepository", t);
            t.printStackTrace();
            throw t;
        }
    }

    public static List<NodeAddress> getSeedNodeAddressesFromPropertyFile(BaseCurrencyNetwork baseCurrencyNetwork) {
        List<NodeAddress> list = new ArrayList<>();
        InputStream fileInputStream = DefaultSeedNodeRepository.class.getClassLoader().getResourceAsStream(
                baseCurrencyNetwork.name().toLowerCase() + ENDING);
        if (fileInputStream == null) {
            return new ArrayList<>();
        }
        BufferedReader seedNodeFile = new BufferedReader(new InputStreamReader(fileInputStream));


        seedNodeFile.lines().forEach(line -> {
            Matcher matcher = pattern.matcher(line);
            if (matcher.find())
                list.add(new NodeAddress(matcher.group(1)));

            // Maybe better include in regex...
            if (line.startsWith("localhost"))
                list.add(new NodeAddress(line));
        });
        return list;
    }

    public Collection<NodeAddress> getSeedNodeAddresses() {
        if (cache.isEmpty())
            reload();

        return cache;
    }

    public boolean isSeedNode(NodeAddress nodeAddress) {
        if (cache.isEmpty())
            reload();
        return cache.contains(nodeAddress);
    }
}

@@ -1137,24 +1137,51 @@
 .util.List;%0A
+import java.util.Optional;%0A
 import java.
@@ -2866,50 +2866,45 @@
 tic 
-List%3CNodeAddress%3E getSeedNodeAddressesFrom
+Optional%3CBufferedReader%3E readSeedNode
 Prop
@@ -2959,60 +2959,8 @@
 ) %7B%0A
-        List%3CNodeAddress%3E list = new ArrayList%3C%3E();%0A
     
@@ -3193,23 +3193,22 @@
 urn 
-new ArrayList%3C%3E
+Optional.empty
 ();%0A
@@ -3229,38 +3229,27 @@
     
-BufferedReader seedNodeFile = 
+return Optional.of(
 new 
@@ -3306,12 +3306,272 @@
 am))
+)
 ;%0A
+    %7D
 %0A%0A
+    public static List%3CNodeAddress%3E getSeedNodeAddressesFromPropertyFile(BaseCurrencyNetwork baseCurrencyNetwork) %7B%0A        List%3CNodeAddress%3E list = new ArrayList%3C%3E();%0A        readSeedNodePropertyFile(baseCurrencyNetwork).ifPresent(seedNodeFile -%3E %7B%0A    
     
@@ -3625,16 +3625,20 @@
         
+   
+ 
 Matcher 
@@ -3678,24 +3678,28 @@
             
+    
 if (matcher.
@@ -3714,32 +3714,36 @@
                 
+   
+ 
 list.add(new Nod
@@ -3784,16 +3784,20 @@
         
+    
 // Maybe
@@ -3832,24 +3832,28 @@
             
+  
+  
 if (line.sta
@@ -3873,16 +3873,18 @@
 lhost%22))
+ %7B
 %0A       
@@ -3896,38 +3896,182 @@
     
-list.add(new NodeAddress(line)
+    String%5B%5D strings = line.split(%22 %5C%5C(@%22);%0A                    String node = strings%5B0%5D;%0A                    list.add(new NodeAddress(node));%0A                %7D%0A            %7D
 );%0A 

Add readSeedNodePropertyFile method