package com.ccnet.acc

import android.content.Context
import com.github.shadowsocks.database.Profile
import com.github.shadowsocks.preference.DataStore
import timber.log.Timber
import java.io.File

object AccVpn {

    private const val configFilename = "config.conf"

    fun configFile(context: Context): File {
        return File(context.filesDir, configFilename)
    }

    fun writeConfig(context: Context, tunFd: Int, profile: Profile) {
        val proxyRules = DataStore.proxyRules
        val dnsServer = if (DataStore.dnsServer.isNotBlank()) {
            DataStore.dnsServer
        } else {
            "8.8.8.8"
        }
        val configContent = """
            [General]
            loglevel = trace
            dns-server = $dnsServer
            tun-fd = $tunFd
            [Proxy]
            Direct = direct
            SS = ss, ${profile.host}, ${profile.remotePort}, encrypt-method=chacha20-ietf-poly1305, password=123456, auth_timeout=${profile.authTimeout}, beats_interval=${profile.beatsInterval}, retry_limit=${profile.retryLimit}, token=${profile.token}
            [Rule]
            $proxyRules
            FINAL, Direct
            """
        val configFile = configFile(context)
        Timber.d("writeConfig ${configFile.absolutePath}")
        configFile.writeText(configContent)
        Timber.d("writeConfig readLines ${configContent}")
    }
}