package eu.darken.sdmse.systemcleaner.ui.customfilter.list.items

import androidx.compose.foundation.background
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.twotone.Edit
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import eu.darken.sdmse.systemcleaner.R
import eu.darken.sdmse.systemcleaner.ui.customfilter.list.CustomFilterListViewModel
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle

@Composable
fun CustomFilterRow(
    modifier: Modifier = Modifier,
    row: CustomFilterListViewModel.FilterRow,
    selected: Boolean,
    selectionActive: Boolean,
    onClick: () -> Unit,
    onLongClick: () -> Unit,
    onEditClick: () -> Unit,
    onToggle: () -> Unit,
) {
    val background = if (selected) {
        MaterialTheme.colorScheme.secondaryContainer
    } else {
        Color.Transparent
    }

    val formattedDate = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).format(
        row.config.modifiedAt.atZone(java.time.ZoneId.systemDefault()),
    )

    Row(
        modifier = modifier
            .fillMaxWidth()
            .background(background)
            .combinedClickable(
                onClick = onClick,
                onLongClick = onLongClick,
            )
            .padding(horizontal = 16.dp, vertical = 12.dp),
        verticalAlignment = Alignment.CenterVertically,
    ) {
        IconButton(onClick = onEditClick, enabled = !selectionActive) {
            Icon(
                imageVector = Icons.TwoTone.Edit,
                contentDescription = null,
                tint = MaterialTheme.colorScheme.onSurfaceVariant,
            )
        }
        Spacer(Modifier.width(8.dp))
        Column(modifier = Modifier.weight(1f)) {
            Text(
                text = row.config.label,
                style = MaterialTheme.typography.titleSmall,
            )
            Text(
                text = stringResource(R.string.systemcleaner_customfilter_last_edit, formattedDate),
                style = MaterialTheme.typography.bodySmall,
                color = MaterialTheme.colorScheme.onSurfaceVariant,
            )
        }
        Switch(
            checked = row.isEnabled,
            onCheckedChange = { onToggle() },
            enabled = !selectionActive,
        )
    }
}

