#include "lite_wallet_server_selection_adapter.h"

namespace dragonx {
namespace wallet {

namespace {

LiteServerSelectionMode walletModeFromSettings(
    config::Settings::LiteServerSelectionPreferenceMode mode)
{
    switch (mode) {
        case config::Settings::LiteServerSelectionPreferenceMode::Sticky:
            return LiteServerSelectionMode::Sticky;
        case config::Settings::LiteServerSelectionPreferenceMode::Random:
            return LiteServerSelectionMode::Random;
    }
    return LiteServerSelectionMode::Sticky;
}

config::Settings::LiteServerSelectionPreferenceMode settingsModeFromWallet(
    LiteServerSelectionMode mode)
{
    switch (mode) {
        case LiteServerSelectionMode::Sticky:
            return config::Settings::LiteServerSelectionPreferenceMode::Sticky;
        case LiteServerSelectionMode::Random:
            return config::Settings::LiteServerSelectionPreferenceMode::Random;
    }
    return config::Settings::LiteServerSelectionPreferenceMode::Sticky;
}

} // namespace

LiteConnectionSettings liteConnectionSettingsFromAppSettings(const config::Settings& settings)
{
    LiteConnectionSettings connectionSettings;
    connectionSettings.servers.clear();
    for (const auto& server : settings.getLiteServers()) {
        connectionSettings.servers.push_back(LiteServerEndpoint{
            liteTrimCopy(server.url),
            server.label,
            server.enabled
        });
    }
    connectionSettings.selectionMode = walletModeFromSettings(settings.getLiteServerSelectionMode());
    connectionSettings.stickyServerUrl = liteTrimCopy(settings.getLiteStickyServerUrl());
    connectionSettings.chainName = liteTrimCopy(settings.getLiteChainName());
    if (connectionSettings.chainName.empty()) connectionSettings.chainName = kDragonXLiteChainName;
    connectionSettings.randomSelectionSeed = settings.getLiteRandomSelectionSeed();
    return connectionSettings;
}

void applyLiteConnectionSettingsToAppSettings(config::Settings& settings,
                                              const LiteConnectionSettings& connectionSettings)
{
    std::vector<config::Settings::LiteServerPreference> servers;
    servers.reserve(connectionSettings.servers.size());
    for (const auto& server : connectionSettings.servers) {
        servers.push_back(config::Settings::LiteServerPreference{
            liteTrimCopy(server.url),
            server.label,
            server.enabled
        });
    }

    settings.setLiteServerSelectionMode(settingsModeFromWallet(connectionSettings.selectionMode));
    settings.setLiteStickyServerUrl(liteTrimCopy(connectionSettings.stickyServerUrl));
    settings.setLiteChainName(liteTrimCopy(connectionSettings.chainName).empty()
        ? kDragonXLiteChainName
        : liteTrimCopy(connectionSettings.chainName));
    settings.setLiteRandomSelectionSeed(connectionSettings.randomSelectionSeed);
    settings.setLiteServers(servers);
}

std::string redactLiteServerSelectionValue(const std::string& value)
{
    const std::string trimmed = liteTrimCopy(value);
    if (trimmed.empty()) return "<empty>";
    const auto scheme = trimmed.find("://");
    if (scheme == std::string::npos) return "<redacted>";
    return trimmed.substr(0, scheme + 3) + "<redacted>";
}

} // namespace wallet
} // namespace dragonx
