@file:OptIn(androidx.compose.material.ExperimentalMaterialApi::class)

package com.example.kmptpc_compose_sample.demos.material

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.DismissDirection
import androidx.compose.material.DismissValue
import androidx.compose.material.Divider
import androidx.compose.material.ListItem
import androidx.compose.material.SwipeToDismiss
import androidx.compose.material.Text
import androidx.compose.material.rememberDismissState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp

@Composable
internal fun SwipeableSamples() {
    val mails = remember { mutableStateListOf("Mail 1", "Mail 2", "Mail 3") }

    LazyColumn(
        modifier = Modifier.fillMaxSize(),
        contentPadding = PaddingValues(vertical = 8.dp)
    ) {
        items(mails, key = { mail -> mail }) { mail ->
            val dismissState = rememberDismissState(
                confirmStateChange = { newValue: DismissValue ->
                    if (newValue == DismissValue.DismissedToEnd || newValue == DismissValue.DismissedToStart) {
                        mails.remove(mail)
                    }
                    true
                }
            )

            SwipeToDismiss(
                state = dismissState,
                directions = setOf(
                    DismissDirection.StartToEnd,
                    DismissDirection.EndToStart
                ),
                background = {
                    val bgColor = when (dismissState.targetValue) {
                        DismissValue.Default -> Color.Transparent
                        DismissValue.DismissedToEnd -> Color(0xFF2E7D32) // Archive
                        DismissValue.DismissedToStart -> Color(0xFFC62828) // Delete
                    }
                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(64.dp)
                            .background(bgColor),
                        contentAlignment = Alignment.CenterStart
                    ) {
                        Text(
                            text = if (dismissState.targetValue == DismissValue.DismissedToEnd) "Archive" else "Delete",
                            color = Color.White,
                            fontWeight = FontWeight.Bold,
                            modifier = Modifier.padding(start = 16.dp)
                        )
                    }
                },
                dismissContent = {
                    ListItem(
                        text = { Text(mail) },
                        secondaryText = { Text("Swipe me left or right") }
                    )
                }
            )
            Divider()
        }
    }
}
