package alt.nainapps.sharepaste.common.units

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Check
import androidx.compose.material.icons.filled.Clear
import androidx.compose.material3.Icon
import androidx.compose.material3.Switch
import androidx.compose.material3.SwitchDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.tooling.preview.Preview

@Composable
fun SwitchWithOnOffIcons(
    label: String,
    checked: Boolean = false,
    onCheckedChange: (Boolean) -> Unit
) {
    // Icon isn't focusable, so no need for content description here
    val icon: (@Composable () -> Unit) = if (checked) {
        {
            Icon(
                imageVector = Icons.Filled.Check,
                contentDescription = null,
                modifier = Modifier.size(SwitchDefaults.IconSize),
            )
        }
    } else {
        {
            Icon(
                imageVector = Icons.Filled.Clear,
                contentDescription = null,
                modifier = Modifier.size(SwitchDefaults.IconSize),
            )
        }
    }
    Row (
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.SpaceEvenly,
        modifier = Modifier.fillMaxWidth()
    ) {
        Text(label)
        Switch(
            modifier = Modifier.semantics { contentDescription = "$label toggle switch" },
            checked = checked,
            onCheckedChange = onCheckedChange,
            thumbContent = icon
        )
    }

}

@Preview(showBackground = true)
@Composable
fun ToggleSwitchPreview() {
    SwitchWithOnOffIcons(label = "Let's do this?", checked = true) {}
}