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

package haveno.monitor.metric;

import haveno.monitor.Metric;
import haveno.monitor.Reporter;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Uses the markets API to retrieve market volume data.
 *
 * @author Florian Reimair
 *
 */
@Slf4j
public class MarketStats extends Metric {
    private static final String MARKETS_HAVENO_NETWORK = "https://markets.bisq.network";
    // poor mans JSON parser
    private final Pattern marketPattern = Pattern.compile("\"market\" ?: ?\"([a-z_]+)\"");
    private final Pattern amountPattern = Pattern.compile("\"amount\" ?: ?\"([\\d\\.]+)\"");
    private final Pattern volumePattern = Pattern.compile("\"volume\" ?: ?\"([\\d\\.]+)\"");
    private final Pattern timestampPattern = Pattern.compile("\"trade_date\" ?: ?([\\d]+)");

    private Long lastRun = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(15));

    public MarketStats(Reporter reporter) {
        super(reporter);
    }

    @Override
    protected void execute() {
        try {
            // for each configured host
            Map<String, String> result = new HashMap<>();

            // assemble query
            long now = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
            String query = "/api/trades?format=json&market=all&timestamp_from=" + lastRun + "&timestamp_to=" + now;
            lastRun = now; // thought about adding 1 second but what if a trade is done exactly in this one second?

            // connect
            URLConnection connection = new URL(MARKETS_HAVENO_NETWORK + query).openConnection();

            // prepare to receive data
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

            String line, all = "";
            while ((line = in.readLine()) != null)
                all += ' ' + line;
            in.close();

            Arrays.stream(all.substring(0, all.length() - 2).split("}")).forEach(trade -> {
                Matcher market = marketPattern.matcher(trade);
                Matcher amount = amountPattern.matcher(trade);
                Matcher timestamp = timestampPattern.matcher(trade);
                market.find();
                if (market.group(1).endsWith("btc")) {
                    amount = volumePattern.matcher(trade);
                }
                amount.find();
                timestamp.find();
                reporter.report("volume." + market.group(1), amount.group(1), timestamp.group(1), getName());
            });
        } catch (IllegalStateException ignore) {
            // no match found
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
