package io.bisq.provider.fee.providers;

import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import io.bisq.network.http.HttpClient;
import io.bisq.provider.fee.FeeRequestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

//TODO use protobuffer instead of json
public class BtcFeesProvider {
    private static final Logger log = LoggerFactory.getLogger(BtcFeesProvider.class);

    private final HttpClient httpClient;

    // other: https://estimatefee.com/n/2
    public BtcFeesProvider() {
        this.httpClient = new HttpClient("https://bitcoinfees.21.co/api/v1/fees/");
    }

    public Long getFee() throws IOException {
        String response = httpClient.requestWithGET("recommended", "User-Agent", "");
        log.info("Get recommended fee response:  " + response);
        Map<String, Long> map = new HashMap<>();
        //noinspection unchecked
        LinkedTreeMap<String, Double> treeMap = new Gson().fromJson(response, LinkedTreeMap.class);
        treeMap.entrySet().stream().forEach(e -> map.put(e.getKey(), e.getValue().longValue()));

        if (map.get("fastestFee") < FeeRequestService.BTC_MAX_TX_FEE)
            return map.get("fastestFee");
        else if (map.get("halfHourFee") < FeeRequestService.BTC_MAX_TX_FEE)
            return map.get("halfHourFee");
        else if (map.get("hourFee") < FeeRequestService.BTC_MAX_TX_FEE)
            return map.get("hourFee");
        else
            return FeeRequestService.BTC_MAX_TX_FEE;
    }
}

@@ -106,24 +106,62 @@
 kedTreeMap;%0A
+import io.bisq.common.util.MathUtils;%0A
 import io.bi
@@ -342,37 +342,17 @@
 til.
-HashMap;%0Aimport java.util.Map
+ArrayList
 ;%0A%0A/
@@ -759,16 +759,176 @@
 ption %7B%0A
+        // prev. used:  https://bitcoinfees.21.co/api/v1/fees/recommended%0A        // but was way too high%0A%0A        //https://bitcoinfees.21.co/api/v1/fees/list%0A
         
@@ -972,27 +972,20 @@
 ithGET(%22
-recommended
+list
 %22, %22User
@@ -1062,24 +1062,25 @@
 ponse);%0A
+%0A
         
 Map%3CStri
@@ -1071,16 +1071,26 @@
         
+LinkedTree
 Map%3CStri
@@ -1097,78 +1097,18 @@
 ng, 
-Long%3E map = new HashMap%3C%3E();%0A        //noinspection unchecked%0A        
+ArrayList%3C
 Link
@@ -1132,16 +1132,18 @@
  Double%3E
+%3E%3E
  treeMap
@@ -1209,327 +1209,529 @@
     
-treeMap.entrySet().stream().forEach(e -%3E map.put(e.getKey(), e.getValue().longValue()));%0A%0A        if (map.get(%22fastestFee%22) %3C FeeRequestService.BTC_MAX_TX_FEE)%0A            return map.get(%22fastestFee%22);%0A        else if (map.get(%22halfHourFee%22) %3C FeeRequestService.BTC_MAX_TX_FEE)%0A            return map.get(%22halfHour
+final long%5B%5D fee = new long%5B1%5D;%0A        // we want a fee which is at least in 10 blocks in (21.co estimation seem to be way too high, so we get %0A        // prob much faster in%0A        int maxBlocks = 10;%0A        treeMap.entrySet().stream()%0A                .flatMap(e -%3E e.getValue().stream())%0A                .forEach(e -%3E %7B%0A                    Double maxDelay = e.get(%22maxDelay%22);%0A                    if (maxDelay %3C= maxBlocks && fee%5B0%5D == 0)%0A                        fee%5B0%5D = MathUtils.roundDoubleToLong(e.get(%22max
 Fee%22)
+)
 ;%0A  
@@ -1740,37 +1740,97 @@
     
-else if (map.get(%22hourFee%22) %3C
+        %7D);%0A        fee%5B0%5D = Math.min(Math.max(fee%5B0%5D, FeeRequestService.BTC_MIN_TX_FEE),
  Fee
@@ -1859,16 +1859,17 @@
 _TX_FEE)
+;
 %0A       
@@ -1873,56 +1873,35 @@
     
-    return map.get(%22hourFee%22);%0A        else%0A    
+log.info(%22fee %22 + fee%5B0%5D);%0A
     
@@ -1915,40 +1915,14 @@
 urn 
-FeeRequestService.BTC_MAX_TX_FEE
+fee%5B0%5D
 ;%0A  

Change tx fee policy in provider