// ui/calllog/AddContactFromCallDialog.kt
package com.example.initiateaphonecallapp.ui.calllog

import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Person
import androidx.compose.material.icons.filled.Phone
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.*
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AddContactFromCallDialog(
    phoneNumber: String,
    onDismiss: () -> Unit,
    onConfirm: (name: String, phone: String) -> Unit,
    modifier: Modifier = Modifier
) {
    var contactName by remember { mutableStateOf("") }
    var contactPhone by remember { mutableStateOf(phoneNumber) }
    val focusRequester = remember { FocusRequester() }
    val coroutineScope = rememberCoroutineScope()

    // 自动聚焦到姓名输入框
    LaunchedEffect(Unit) {
        delay(100)
        focusRequester.requestFocus()
    }

    Dialog(
        onDismissRequest = onDismiss
    ) {
        Card(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp),
            shape = MaterialTheme.shapes.medium,
        ) {
            Column(
                modifier = Modifier.padding(16.dp)
            ) {
                Text(
                    text = "添加联系人",
                    style = MaterialTheme.typography.headlineSmall,
                    modifier = Modifier.padding(bottom = 16.dp)
                )

                // 姓名输入框
                TextField(
                    value = contactName,
                    onValueChange = { contactName = it },
                    label = { Text("姓名") },
                    placeholder = { Text("请输入联系人姓名") },
                    singleLine = true,
                    modifier = Modifier
                        .fillMaxWidth()
                        .focusRequester(focusRequester)
                        .padding(bottom = 16.dp)
                )

                // 电话输入框
                TextField(
                    value = contactPhone,
                    onValueChange = { contactPhone = it },
                    label = { Text("电话") },
                    placeholder = { Text("请输入联系人电话") },
                    singleLine = true,
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(bottom = 16.dp)
                )

                // 按钮区域
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.End
                ) {
                    TextButton(
                        onClick = onDismiss,
                        modifier = Modifier.padding(end = 8.dp)
                    ) {
                        Text("取消")
                    }
                    Button(
                        onClick = {
                            if (contactName.isNotBlank() && contactPhone.isNotBlank()) {
                                onConfirm(contactName, contactPhone)
                            }
                        },
                        enabled = contactName.isNotBlank() && contactPhone.isNotBlank()
                    ) {
                        Text("保存")
                    }
                }
            }
        }
    }
}