package com.tutpro.baresip.plus

import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity

class NetworkSettingActivity : AppCompatActivity() {

    private lateinit var radioDhcp: RadioButton
    private lateinit var radioStatic: RadioButton
    private lateinit var editIp: EditText
    private lateinit var editNetmask: EditText
    private lateinit var editGateway: EditText
    private lateinit var editDns: EditText
    private lateinit var btnApply: Button

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

        radioDhcp = findViewById(R.id.radioDhcp)
        radioStatic = findViewById(R.id.radioStatic)
        editIp = findViewById(R.id.editIp)
        editNetmask = findViewById(R.id.editNetmask)
        editGateway = findViewById(R.id.editGateway)
        editDns = findViewById(R.id.editDns)
        btnApply = findViewById(R.id.btnApply)

        updateFieldsVisibility()

        val radioGroup = findViewById<RadioGroup>(R.id.radioGroup)
        radioGroup.setOnCheckedChangeListener { _, _ ->
            updateFieldsVisibility()
        }

        btnApply.setOnClickListener {
            applyNetworkSettings()
        }
    }

    private fun updateFieldsVisibility() {
        val isStatic = radioStatic.isChecked
        val visibility = if (isStatic) View.VISIBLE else View.GONE
        editIp.visibility = visibility
        editNetmask.visibility = visibility
        editGateway.visibility = visibility
        editDns.visibility = visibility
    }

    private fun applyNetworkSettings() {
        if (radioDhcp.isChecked) {
            Toast.makeText(this, "DHCP 설정 적용", Toast.LENGTH_SHORT).show()
            // TODO: DHCP 설정 로직 추가
        } else {
            val ip = editIp.text.toString()
            val netmask = editNetmask.text.toString()
            val gateway = editGateway.text.toString()
            val dns = editDns.text.toString()

            if (ip.isBlank() || netmask.isBlank() || gateway.isBlank() || dns.isBlank()) {
                Toast.makeText(this, "모든 STATIC 필드를 입력해 주세요.", Toast.LENGTH_SHORT).show()
                return
            }

            Toast.makeText(this, "STATIC 설정 적용:\n$ip / $netmask / $gateway / $dns", Toast.LENGTH_SHORT).show()
            // TODO: Static 설정 로직 추가
        }
    }
}
