package com.dragonssh.xhttpdemo;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.dragonssh.xhttpdemo.core.config.Settings;
import com.google.android.material.switchmaterial.SwitchMaterial;

public final class VpnSettingsActivity extends AppCompatActivity {
    private Settings settings;
    private SwitchMaterial customDnsSwitch;
    private EditText primaryDnsField;
    private EditText secondaryDnsField;
    private SwitchMaterial udpGatewaySwitch;
    private EditText udpGatewayField;
    private SwitchMaterial ipv6Switch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vpn_settings);
        WindowInsetsHelper.apply(this, R.id.vpn_settings_root);

        settings = new Settings(this);
        bindViews();
        loadConfiguration();

        customDnsSwitch.setOnCheckedChangeListener((button, checked) -> updateEnabledState());
        udpGatewaySwitch.setOnCheckedChangeListener((button, checked) -> updateEnabledState());
        findViewById(R.id.back).setOnClickListener(v -> finish());
        findViewById(R.id.save_vpn_settings).setOnClickListener(v -> saveConfiguration());
    }

    private void bindViews() {
        customDnsSwitch = findViewById(R.id.use_custom_dns);
        primaryDnsField = findViewById(R.id.dns_primary);
        secondaryDnsField = findViewById(R.id.dns_secondary);
        udpGatewaySwitch = findViewById(R.id.enable_udpgw);
        udpGatewayField = findViewById(R.id.udpgw_address);
        ipv6Switch = findViewById(R.id.enable_ipv6);
    }

    private void loadConfiguration() {
        customDnsSwitch.setChecked(settings.getVpnDnsForward());
        primaryDnsField.setText(settings.getVpnDnsResolver());
        secondaryDnsField.setText(settings.getVpnDnsResolverSecondary());
        udpGatewaySwitch.setChecked(settings.getVpnUdpForward());
        udpGatewayField.setText(settings.getVpnUdpResolver());
        // Switch shows IPv6 ENABLED; the stored flag is the inverse (disableIpv6Tunnel).
        ipv6Switch.setChecked(!settings.getDisableIpv6Tunnel());
        updateEnabledState();
    }

    private void updateEnabledState() {
        primaryDnsField.setEnabled(customDnsSwitch.isChecked());
        secondaryDnsField.setEnabled(customDnsSwitch.isChecked());
        udpGatewayField.setEnabled(udpGatewaySwitch.isChecked());
    }

    private void saveConfiguration() {
        String primaryDns = text(primaryDnsField);
        String secondaryDns = text(secondaryDnsField);
        String udpGateway = text(udpGatewayField);

        if (customDnsSwitch.isChecked() && primaryDns.isEmpty()) {
            Toast.makeText(this, R.string.error_dns_primary_required, Toast.LENGTH_LONG).show();
            primaryDnsField.requestFocus();
            return;
        }
        if (udpGatewaySwitch.isChecked() && !ConfigUtils.isHostPort(udpGateway)) {
            Toast.makeText(this, R.string.error_udpgw_invalid, Toast.LENGTH_LONG).show();
            udpGatewayField.requestFocus();
            return;
        }

        settings.getVpnPrefs().edit()
                .putBoolean(Settings.DNSFORWARD_KEY, customDnsSwitch.isChecked())
                .putString(Settings.DNSRESOLVER_KEY, primaryDns)
                .putString(Settings.DNSRESOLVER_SECONDARY_KEY, secondaryDns)
                .putBoolean(Settings.UDPFORWARD_KEY, udpGatewaySwitch.isChecked())
                .putString(Settings.UDPRESOLVER_KEY, udpGateway)
                .putBoolean(Settings.DISABLE_IPV6_TUNNEL_KEY, !ipv6Switch.isChecked())
                .apply();

        setResult(RESULT_OK);
        Toast.makeText(this, R.string.settings_saved, Toast.LENGTH_SHORT).show();
        finish();
    }

    private static String text(EditText view) {
        return view.getText() == null ? "" : view.getText().toString().trim();
    }
}
