package com.aistudio.bonjuteri.jwlry.ui.screens
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.DeleteOutline
import androidx.compose.material.icons.filled.LocationOn
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.aistudio.bonjuteri.jwlry.model.Address
import com.aistudio.bonjuteri.jwlry.ui.components.EmptyStateView
import com.aistudio.bonjuteri.jwlry.ui.theme.ErrorRed
import com.aistudio.bonjuteri.jwlry.ui.theme.GoldPrimary
import com.aistudio.bonjuteri.jwlry.ui.theme.MutedGrey
import com.aistudio.bonjuteri.jwlry.ui.theme.OffWhite
import com.aistudio.bonjuteri.jwlry.ui.theme.PrimaryBlack
import com.aistudio.bonjuteri.jwlry.ui.theme.PureWhite
import kotlinx.coroutines.launch
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AddressesScreen(
addresses: List
,
onSaveAddress: suspend (Address) -> Result,
onDeleteAddress: suspend (String) -> Result,
onBackClick: () -> Unit,
snackbarHostState: SnackbarHostState,
modifier: Modifier = Modifier
) {
var showAddDialog by remember { mutableStateOf(false) }
var addressTitle by remember { mutableStateOf("Evim") }
var fullName by remember { mutableStateOf("") }
var phone by remember { mutableStateOf("") }
var city by remember { mutableStateOf("İstanbul") }
var district by remember { mutableStateOf("") }
var neighborhood by remember { mutableStateOf("") }
var fullAddress by remember { mutableStateOf("") }
var postalCode by remember { mutableStateOf("") }
var dialogError by remember { mutableStateOf(null) }
val scope = rememberCoroutineScope()
Scaffold(
topBar = {
TopAppBar(
title = { Text("Kayıtlı Adreslerim", fontWeight = FontWeight.Bold) },
navigationIcon = {
IconButton(onClick = onBackClick) {
Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = "Geri")
}
},
colors = TopAppBarDefaults.topAppBarColors(containerColor = PureWhite)
)
},
floatingActionButton = {
FloatingActionButton(
onClick = {
dialogError = null
showAddDialog = true
},
containerColor = PrimaryBlack,
contentColor = PureWhite
) {
Icon(Icons.Filled.Add, contentDescription = "Yeni Adres Ekle")
}
}
) { padding ->
if (addresses.isEmpty()) {
EmptyStateView(
icon = Icons.Filled.LocationOn,
title = "Kayıtlı Adresiniz Yok",
description = "Teslimat adreslerinizi kaydederek hızlıca sipariş verebilirsiniz.",
actionButtonText = "+ Yeni Adres Ekle",
onActionClick = { showAddDialog = true },
modifier = Modifier.padding(padding).fillMaxSize()
)
} else {
LazyColumn(
modifier = modifier
.fillMaxSize()
.padding(padding)
.background(OffWhite)
.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(12.dp)
) {
items(addresses, key = { it.id }) { addr ->
Card(
modifier = Modifier.fillMaxWidth(),
colors = CardDefaults.cardColors(containerColor = PureWhite),
shape = RoundedCornerShape(12.dp)
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
verticalAlignment = Alignment.Top
) {
Icon(Icons.Filled.LocationOn, contentDescription = null, tint = GoldPrimary, modifier = Modifier.size(24.dp))
Spacer(modifier = Modifier.size(12.dp))
Column(modifier = Modifier.weight(1f)) {
Text(
text = addr.title.ifBlank { "Adres" },
style = MaterialTheme.typography.titleMedium.copy(fontWeight = FontWeight.Bold)
)
Spacer(modifier = Modifier.height(2.dp))
Text(text = addr.fullName, fontWeight = FontWeight.SemiBold, fontSize = 13.sp)
Text(text = addr.fullDisplayAddress, color = MutedGrey, fontSize = 12.sp, lineHeight = 16.sp)
Text(text = addr.phone, color = MutedGrey, fontSize = 11.sp)
}
IconButton(
onClick = {
scope.launch {
val res = onDeleteAddress(addr.id)
if (res.isSuccess) {
snackbarHostState.showSnackbar("Adres silindi.")
}
}
}
) {
Icon(Icons.Filled.DeleteOutline, contentDescription = "Sil", tint = ErrorRed)
}
}
}
}
}
}
// Add Address Dialog
if (showAddDialog) {
AlertDialog(
onDismissRequest = { showAddDialog = false },
title = { Text("Yeni Adres Ekle", fontWeight = FontWeight.Bold) },
text = {
Column(
modifier = Modifier.verticalScroll(rememberScrollState()),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
if (dialogError != null) {
Text(text = dialogError!!, color = MaterialTheme.colorScheme.error, fontSize = 12.sp)
}
OutlinedTextField(value = addressTitle, onValueChange = { addressTitle = it }, label = { Text("Başlık (Ev, İş)") }, singleLine = true)
OutlinedTextField(value = fullName, onValueChange = { fullName = it }, label = { Text("Ad Soyad") }, singleLine = true)
OutlinedTextField(value = phone, onValueChange = { phone = it }, label = { Text("Telefon") }, singleLine = true)
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
OutlinedTextField(value = city, onValueChange = { city = it }, label = { Text("İl") }, modifier = Modifier.weight(1f), singleLine = true)
OutlinedTextField(value = district, onValueChange = { district = it }, label = { Text("İlçe") }, modifier = Modifier.weight(1f), singleLine = true)
}
OutlinedTextField(value = neighborhood, onValueChange = { neighborhood = it }, label = { Text("Mahalle") }, singleLine = true)
OutlinedTextField(value = fullAddress, onValueChange = { fullAddress = it }, label = { Text("Açık Adres") }, maxLines = 3)
OutlinedTextField(value = postalCode, onValueChange = { postalCode = it }, label = { Text("Posta Kodu") }, singleLine = true)
}
},
confirmButton = {
Button(
onClick = {
scope.launch {
val addr = Address(
title = addressTitle,
fullName = fullName,
phone = phone,
city = city,
district = district,
neighborhood = neighborhood,
fullAddress = fullAddress,
postalCode = postalCode
)
val res = onSaveAddress(addr)
if (res.isSuccess) {
showAddDialog = false
snackbarHostState.showSnackbar("Adres kaydedildi.")
} else {
dialogError = res.exceptionOrNull()?.message ?: "Adres kaydedilemedi."
}
}
},
colors = ButtonDefaults.buttonColors(containerColor = PrimaryBlack)
) {
Text("Kaydet")
}
},
dismissButton = {
OutlinedButton(onClick = { showAddDialog = false }) {
Text("İptal")
}
}
)
}
}
}