package com.sshproxy.vpn.ui

import android.view.View
import com.sshproxy.vpn.data.SshAuthMethod
import com.sshproxy.vpn.data.TunnelConfig
import com.sshproxy.vpn.databinding.ActivityTunnelSettingsBinding

object TunnelConfigForm {

    fun populate(binding: ActivityTunnelSettingsBinding, config: TunnelConfig) {
        binding.host.setText(config.host)
        binding.port.setText(config.port.toString())
        binding.username.setText(config.username)
        binding.authByPassword.isChecked = config.authMethod == SshAuthMethod.PASSWORD
        binding.keyPath.setText(config.privateKeyPath)
        binding.passphrase.setText(config.keyPassphrase)
        binding.sshPassword.setText(config.password)
        binding.socksPort.setText(config.socksPort.toString())
        binding.remoteSocksHost.setText(config.remoteSocksHost)
        binding.remoteSocksPort.setText(config.remoteSocksPort.toString())
        binding.remoteSocksUsername.setText(config.remoteSocksUsername)
        binding.remoteSocksPassword.setText(config.remoteSocksPassword)
        binding.acceptUnknownHost.isChecked = config.acceptUnknownHostKey
        binding.keepAliveInterval.setText(config.keepAliveIntervalSec.toString())
        binding.autoReconnect.isChecked = config.autoReconnect
        applyAuthSections(binding, config.authMethod)
    }

    fun read(binding: ActivityTunnelSettingsBinding): TunnelConfig {
        val authMethod = if (binding.authByPassword.isChecked) {
            SshAuthMethod.PASSWORD
        } else {
            SshAuthMethod.PUBLIC_KEY
        }
        return TunnelConfig(
            host = binding.host.text.toString().trim(),
            port = binding.port.text.toString().toIntOrNull() ?: 22,
            username = binding.username.text.toString().trim(),
            authMethod = authMethod,
            privateKeyPath = binding.keyPath.text.toString().trim(),
            keyPassphrase = binding.passphrase.text.toString(),
            password = binding.sshPassword.text.toString(),
            socksPort = binding.socksPort.text.toString().toIntOrNull()
                ?: TunnelConfig.DEFAULT_LOCAL_SOCKS_PORT,
            remoteSocksHost = binding.remoteSocksHost.text.toString().trim().ifBlank {
                TunnelConfig.DEFAULT_REMOTE_SOCKS_HOST
            },
            remoteSocksPort = binding.remoteSocksPort.text.toString().toIntOrNull()
                ?: TunnelConfig.DEFAULT_REMOTE_SOCKS_PORT,
            remoteSocksUsername = binding.remoteSocksUsername.text.toString().trim(),
            remoteSocksPassword = binding.remoteSocksPassword.text.toString(),
            acceptUnknownHostKey = binding.acceptUnknownHost.isChecked,
            keepAliveIntervalSec = binding.keepAliveInterval.text.toString().toIntOrNull()?.coerceIn(
                TunnelConfig.MIN_KEEP_ALIVE_INTERVAL_SEC,
                TunnelConfig.MAX_KEEP_ALIVE_INTERVAL_SEC
            ) ?: TunnelConfig.DEFAULT_KEEP_ALIVE_INTERVAL_SEC,
            autoReconnect = binding.autoReconnect.isChecked
        )
    }

    fun applyAuthSections(binding: ActivityTunnelSettingsBinding, authMethod: SshAuthMethod) {
        val usePassword = authMethod == SshAuthMethod.PASSWORD
        binding.passwordAuthSection.visibility = if (usePassword) View.VISIBLE else View.GONE
        binding.keyAuthSection.visibility = if (usePassword) View.GONE else View.VISIBLE
    }
}
