package zone.yue.jvtc.android.exp12

import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.room.Room
import zone.yue.jvtc.android.exp12.ui.theme.Exp12Theme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent { Exp12Theme { App() } }
    }
}

@Preview
@Composable
@OptIn(ExperimentalMaterial3Api::class)
fun App() {
    val context = LocalContext.current
    val db = Room
        .databaseBuilder(LocalContext.current, ContactDatabase::class.java, "contact")
        .allowMainThreadQueries()
        .build()
        .contactDao()

    var name by remember { mutableStateOf("") }
    var tel by remember { mutableStateOf("") }
    var log by remember { mutableStateOf("LOG") }
    var logColor by remember { mutableStateOf(Color.Black) }

    fun showInfo(text: String) {
        logColor = Color.Black
        log = text
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show()
    }

    fun showException(e: Exception) {
        logColor = Color.Red
        Toast.makeText(context, e.localizedMessage, Toast.LENGTH_SHORT).show()
        log = e.stackTraceToString()
        e.printStackTrace()
    }

    Scaffold(topBar = { TopAppBar(title = { Text("Ys0304") }) }) { paddingValues ->
        Column(
            modifier = Modifier
                .padding(paddingValues)
                .padding(8.dp)
                .fillMaxSize(),
        ) {
            TextField(
                value = name,
                onValueChange = { name = it },
                modifier = Modifier.fillMaxWidth(),
                label = { Text("姓名") },
            )
            TextField(value = tel,
                onValueChange = { tel = it },
                modifier = Modifier.fillMaxWidth(),
                label = { Text("电话") })
            Button(
                onClick = {
                    try {
                        db.insert(ContactEntity(name, tel))
                        showInfo("添加记录成功！")
                    } catch (e: Exception) {
                        showException(e)
                    }
                }, modifier = Modifier.fillMaxWidth()
            ) { Text("添加记录") }
            Button(
                onClick = {
                    try {
                        db.update(ContactEntity(name, tel))
                        showInfo("修改记录成功！")
                    } catch (e: Exception) {
                        showException(e)
                    }
                },
                modifier = Modifier.fillMaxWidth(),
            ) { Text("修改记录") }
            Button(
                onClick = {
                    try {
                        db.deleteByName(name)
                        showInfo("删除记录成功！")
                    } catch (e: Exception) {
                        showException(e)
                    }
                },
                modifier = Modifier.fillMaxWidth(),
            ) { Text("删除记录") }
            Button(
                onClick = {
                    try {
                        logColor = Color.Black
                        log = db.queryAll().toString().replace("),", "),\n")
                        Toast.makeText(context, "查询记录成功！", Toast.LENGTH_SHORT).show()
                    } catch (e: Exception) {
                        showException(e)
                    }
                },
                modifier = Modifier.fillMaxWidth(),
            ) { Text("查询记录") }
            SelectionContainer { Text(text = log, color = logColor) }
        }
    }
}
