package com.navi.client

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class SetupActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_setup)

        val prefs = getSharedPreferences("navi", Context.MODE_PRIVATE)
        val urlField = findViewById<EditText>(R.id.et_server_url)
        val saveButton = findViewById<Button>(R.id.btn_connect)

        urlField.setText(prefs.getString("server_url", "http://"))

        saveButton.setOnClickListener {
            val raw = urlField.text.toString().trim()
            if (raw.isBlank() || (!raw.startsWith("http://") && !raw.startsWith("https://"))) {
                Toast.makeText(this, getString(R.string.invalid_url), Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            val url = raw.trimEnd('/')
            prefs.edit().putString("server_url", url).apply()

            startActivity(
                Intent(this, MainActivity::class.java).apply {
                    flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
                }
            )
            finish()
        }
    }
}
