package com.dragonssh.xhttpdemo.core.config;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Minimal local configuration store for the public sample.
 * No online panel, config encryption, import/export, HWID, OTP, or paid-app settings.
 */
public final class Settings implements SettingsConstants {
    private final SharedPreferences privatePrefs;
    private final SharedPreferences vpnPrefs;

    public Settings(Context context) {
        Context app = context.getApplicationContext();
        privatePrefs = app.getSharedPreferences("xhttp_demo_private", Context.MODE_PRIVATE);
        vpnPrefs = app.getSharedPreferences("xhttp_demo_vpn", Context.MODE_PRIVATE);
    }

    public String getPrivString(String key) {
        String fallback = "";
        if (PORTA_LOCAL_KEY.equals(key)) fallback = "1080";
        if (XHTTP_PATH_KEY.equals(key)) fallback = "/ssh";
        if (XHTTP_TLS_KEY.equals(key)) fallback = "1";
        return privatePrefs.getString(key, fallback);
    }

    public SharedPreferences getPrefsPrivate() {
        return privatePrefs;
    }

    public SharedPreferences getVpnPrefs() {
        return vpnPrefs;
    }

    public int getMaximoThreadsSocks() {
        return Math.max(1, vpnPrefs.getInt(MAXIMO_THREADS_KEY, 8));
    }

    public int getSSHPinger() {
        return Math.max(0, vpnPrefs.getInt(PINGER_KEY, 3));
    }

    public boolean ssh_compression() {
        return vpnPrefs.getBoolean(SSH_COMPRESSION, false);
    }


    public boolean getIsDisabledDelaySSH() {
        return true;
    }

    public boolean getDisableIpv6Tunnel() {
        return vpnPrefs.getBoolean(DISABLE_IPV6_TUNNEL_KEY, true);
    }

    public boolean getVpnDnsForward() {
        return vpnPrefs.getBoolean(DNSFORWARD_KEY, true);
    }

    public String getVpnDnsResolver() {
        return vpnPrefs.getString(DNSRESOLVER_KEY, "1.1.1.1");
    }

    public String getVpnDnsResolverSecondary() {
        return vpnPrefs.getString(DNSRESOLVER_SECONDARY_KEY, "1.0.0.1");
    }

    public boolean getVpnUdpForward() {
        return vpnPrefs.getBoolean(UDPFORWARD_KEY, true);
    }

    public String getVpnUdpResolver() {
        return vpnPrefs.getString(UDPRESOLVER_KEY, "127.0.0.1:7300");
    }

    public String[] getVpnBypassList() {
        return new String[0];
    }

    public boolean getIsFilterBypassMode() {
        return false;
    }

    public String[] getFilterApps() {
        return new String[0];
    }

    public boolean getIsTetheringSubnet() {
        return false;
    }

    public static void setDefaultConfig(Context context) {
        Settings settings = new Settings(context);
        settings.privatePrefs.edit()
                .putString(PORTA_LOCAL_KEY, "1080")
                .putString(XHTTP_PATH_KEY, "/ssh")
                .putString(XHTTP_TLS_KEY, "1")
                .apply();
        settings.vpnPrefs.edit()
                .putBoolean(UDPFORWARD_KEY, true)
                .putString(UDPRESOLVER_KEY, "127.0.0.1:7300")
                .putBoolean(DNSFORWARD_KEY, true)
                .putString(DNSRESOLVER_KEY, "1.1.1.1")
                .putString(DNSRESOLVER_SECONDARY_KEY, "1.0.0.1")
                .putBoolean(DISABLE_IPV6_TUNNEL_KEY, true)
                .putInt(MAXIMO_THREADS_KEY, 8)
                .putInt(PINGER_KEY, 3)
                .apply();
    }
}
