/**
 * Copyright (C) 2004-2024 Jive Software. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package org.jivesoftware;

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.jivesoftware.spark.util.log.Log;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;


public class XmppProviders {
    /**
     * <a href="https://data.xmpp.net/providers/v2/providers-A.json">Providers A</a>
     */
    public static final List<String> PROVIDERS_A = List.of(
        "07f.de",
        "chalec.org",
        "chapril.org",
        "chatrix.one",
        "draugr.de",
        "hookipa.net",
        "jabber.fr",
        "macaw.me",
        "magicbroccoli.de",
        "nixnet.services",
        "projectsegfau.lt",
        "redlibre.es",
        "suchat.org",
        "sure.im",
        "trashserver.net",
        "xmpp.earth",
        "yax.im",
        "conversations.im"
    );

    public static List<String> getXmppProvidersModel() {
        List<String> providersList = downloadProvidersList();
        if (providersList != null) {
            return providersList;
        }
        // fallback to static list
        return PROVIDERS_A;
    }

    static List<String> downloadProvidersList() {
        Log.debug("Download providers");
        try (CloseableHttpClient httpClient = HttpClients.createSystem()) {
            HttpGet request = new HttpGet("https://data.xmpp.net/providers/v2/providers-As.json");
            try (ClassicHttpResponse httpResponse = httpClient.executeOpen(null, request, null)) {
                final int statusCode = httpResponse.getCode();
                if (statusCode == 200) {
                    String json = EntityUtils.toString(httpResponse.getEntity(), StandardCharsets.UTF_8);
                    return parseProvidersJson(json);
                }
                Log.error("Download providers: bad status " + statusCode);
                return null;
            }
        } catch (Exception e) {
            Log.error("Download providers: error", e);
            return null;
        }
    }

    static List<String> parseProvidersJson(String json) {
        // manually parse JSON array
        json = json.trim();
        if (json.charAt(0) != '[' || json.charAt(json.length() - 1) != ']') {
            return null;
        }
        String[] lines = json.substring(1, json.length() - 1).split(",");
        List<String> providers = new ArrayList<>(lines.length);
        for (String line : lines) {
            line = line.trim();
            if (line.charAt(0) != '"' || line.charAt(line.length() - 1) != '"') {
                continue;
            }
            String provider = line.substring(1, line.length() - 1);
            providers.add(provider);
        }
        return providers;
    }
}
