package eu.darken.sdmse.common.compose.layout

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.RowScope
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.twotone.Close
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.pluralStringResource
import androidx.compose.ui.res.stringResource
import eu.darken.sdmse.common.R as CommonR
import eu.darken.sdmse.common.compose.preview.Preview2
import eu.darken.sdmse.common.compose.preview.PreviewWrapper

@Composable
fun SdmSelectionTopAppBar(
    selectedCount: Int,
    onClearSelection: () -> Unit,
    modifier: Modifier = Modifier,
    subtitle: String? = null,
    actions: @Composable RowScope.() -> Unit = {},
) {
    TopAppBar(
        modifier = modifier,
        colors = TopAppBarDefaults.topAppBarColors(
            containerColor = MaterialTheme.colorScheme.secondaryContainer,
            titleContentColor = MaterialTheme.colorScheme.onSecondaryContainer,
            navigationIconContentColor = MaterialTheme.colorScheme.onSecondaryContainer,
            actionIconContentColor = MaterialTheme.colorScheme.onSecondaryContainer,
        ),
        title = {
            Column {
                Text(
                    pluralStringResource(
                        CommonR.plurals.general_x_selected_count,
                        selectedCount,
                        selectedCount,
                    ),
                )
                if (subtitle != null) {
                    Text(
                        text = subtitle,
                        style = MaterialTheme.typography.labelMedium,
                    )
                }
            }
        },
        navigationIcon = {
            SdmTooltipIconButton(
                icon = Icons.TwoTone.Close,
                label = stringResource(CommonR.string.general_close_action),
                onClick = onClearSelection,
            )
        },
        actions = actions,
    )
}

@Preview2
@Composable
private fun SdmSelectionTopAppBarPreview() {
    PreviewWrapper {
        SdmSelectionTopAppBar(
            selectedCount = 3,
            subtitle = "42 MB",
            onClearSelection = {},
            actions = {
                SdmSelectAllAction(
                    visible = true,
                    onClick = {},
                )
                SdmExcludeAction(onClick = {})
                SdmDeleteAction(onClick = {})
            },
        )
    }
}
