package com.example.lexilearn.ui.components

import android.widget.Toast
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp
import com.example.lexilearn.data.model.UserDataModel
import com.example.lexilearn.data.repository.UserRepository

@Composable
fun DialogProfile(showDialog: Boolean, repository: UserRepository, userData: UserDataModel, onDismiss: () -> Unit) {
    val context = LocalContext.current
    var name by remember { mutableStateOf(TextFieldValue(userData.name)) }
    var age by remember { mutableStateOf(TextFieldValue(userData.age.toString())) }

    if (showDialog) {
        AlertDialog(
            onDismissRequest = { onDismiss() },
            title = { Text("Isikan Data Diri") },
            text = {
                Column {
                    Text("Masukkan Nama")
                    TextField(
                        value = name,
                        onValueChange = { name = it },
                        modifier = Modifier.fillMaxWidth(),
                        shape = RoundedCornerShape(8.dp)
                    )
                    Spacer(modifier = Modifier.height(8.dp))
                    Text("Masukkan Usia")
                    TextField(
                        value = age,
                        onValueChange = { age = it },
                        modifier = Modifier.fillMaxWidth(),
                        shape = RoundedCornerShape(8.dp),
                        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
                    )
                }
            },
            confirmButton = {
                Button(onClick = {
                    repository.updateUser(UserDataModel(name.text, age.text.toInt(), userData.unlock_data)){
                        onDismiss()
                        Toast.makeText(context, "Data Pengguna Diperbarui", Toast.LENGTH_LONG).show()
                    }
                }) {
                    Text("Simpan")
                }
            },
            dismissButton = {
                Button(onClick = { onDismiss() }) {
                    Text("Batal")
                }
            }
        )
    }
}
