package de.usbserver.bridge

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import android.app.Activity

/**
 * Minimal setup screen: relay address, the three tokens, start and stop.
 *
 * Deliberately plain — the interesting part of this app is [UsbBridge], which
 * gets the file descriptor across to the client. Anything nicer belongs in a
 * proper UI layer and is not needed to make sharing work.
 */
class MainActivity : Activity() {

    private lateinit var relayField: EditText
    private lateinit var token1Field: EditText
    private lateinit var token2Field: EditText
    private lateinit var token3Field: EditText
    private lateinit var statusView: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val prefs = getSharedPreferences("settings", MODE_PRIVATE)

        val root = LinearLayout(this).apply {
            orientation = LinearLayout.VERTICAL
            setPadding(48, 48, 48, 48)
        }

        fun field(hint: String, key: String): EditText =
            EditText(this).apply {
                this.hint = hint
                setText(prefs.getString(key, ""))
                layoutParams = LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                )
            }.also { root.addView(it) }

        relayField = field("ws://relay:8443", "relay")
        token1Field = field("Token 1", "token1")
        token2Field = field("Token 2", "token2")
        token3Field = field("Token 3", "token3")

        statusView = TextView(this).apply { text = "Stopped" }

        root.addView(Button(this).apply {
            text = "Start sharing"
            setOnClickListener {
                prefs.edit()
                    .putString("relay", relayField.text.toString())
                    .putString("token1", token1Field.text.toString())
                    .putString("token2", token2Field.text.toString())
                    .putString("token3", token3Field.text.toString())
                    .apply()

                val intent = Intent(this@MainActivity, ShareService::class.java).apply {
                    putExtra(ShareService.EXTRA_RELAY, relayField.text.toString())
                    putExtra(ShareService.EXTRA_TOKEN1, token1Field.text.toString())
                    putExtra(ShareService.EXTRA_TOKEN2, token2Field.text.toString())
                    putExtra(ShareService.EXTRA_TOKEN3, token3Field.text.toString())
                    putExtra(ShareService.EXTRA_NAME, Build.MODEL)
                }
                startForegroundService(intent)
                statusView.text = "Running — see the notification"
            }
        })

        root.addView(Button(this).apply {
            text = "Stop"
            setOnClickListener {
                stopService(Intent(this@MainActivity, ShareService::class.java))
                statusView.text = "Stopped"
            }
        })

        root.addView(statusView)
        root.addView(TextView(this).apply {
            text = "\nThe same three tokens must be configured on every client " +
                "in the group. Without them traffic is neither encrypted nor " +
                "able to bypass the relay.\n\n" +
                "This device can only share its own USB devices. Receiving " +
                "remote ones needs the vhci-hcd kernel module, which Android " +
                "kernels do not include."
        })

        setContentView(root)
    }
}
